Toggle navigation
Toggle navigation
This project
Loading...
Sign in
이승윤
/
OpenSource-MyCookBook
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
이승윤
2021-05-23 20:47:22 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b5e84415796e74799dbba9112b7f629b9b46da38
b5e84415
1 parent
a58d646b
feat: Search.js 연동
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
32 deletions
Search.js
app.js
routes/Search.js
Search.js
deleted
100644 → 0
View file @
a58d646
var
Youtube
=
require
(
'youtube-node'
);
var
youtube
=
new
Youtube
();
var
word
=
'백종원 레시피'
;
// 검색어 지정
var
limit
=
10
;
// 출력 갯수
youtube
.
setKey
(
'AIzaSyAsKr_oWGZIBbL5tLdIl98Lf9Pzqj8jX4o'
);
// API 키 입력
youtube
.
addParam
(
'order'
,
'rating'
);
// 평점 순으로 정렬
youtube
.
addParam
(
'type'
,
'video'
);
// 타입 지정
youtube
.
addParam
(
'videoLicense'
,
'creativeCommon'
);
// 크리에이티브 커먼즈 아이템만 불러옴
youtube
.
search
(
word
,
limit
,
function
(
err
,
result
)
{
// 검색 실행
if
(
err
)
{
console
.
log
(
err
);
return
;
}
// 에러일 경우 에러공지하고 빠져나감
console
.
log
(
JSON
.
stringify
(
result
,
null
,
2
));
// 받아온 전체 리스트 출력
var
items
=
result
[
"items"
];
// 결과 중 items 항목만 가져옴
for
(
var
i
in
items
)
{
var
it
=
items
[
i
];
var
title
=
it
[
"snippet"
][
"title"
];
var
video_id
=
it
[
"id"
][
"videoId"
];
var
url
=
"https://www.youtube.com/watch?v="
+
video_id
;
console
.
log
(
"제목 : "
+
title
);
console
.
log
(
"URL : "
+
url
);
console
.
log
(
"-----------"
);
}
});
\ No newline at end of file
app.js
View file @
b5e8441
...
...
@@ -27,9 +27,8 @@ var connect = mongoose.connect('mongodb://127.0.0.1:27017/cookBook', {
var
admin
=
require
(
'./routes/admin'
);
var
accounts
=
require
(
'./routes/accounts'
);
var
auth
=
require
(
'./routes/auth'
);
var
home
=
require
(
'./routes/home.js'
);
var
chat
=
require
(
'./routes/chat'
);
var
connectMongo
=
require
(
'connect-mongo'
);
var
Search
=
require
(
'./routes/Search'
);
var
MongoStore
=
connectMongo
(
session
);
app
.
set
(
'views'
,
path
.
join
(
__dirname
,
'views'
));
...
...
@@ -69,11 +68,10 @@ app.use(function (req, res, next) {
});
//routes add
app
.
use
(
'/'
,
home
);
app
.
use
(
'/'
,
Search
);
app
.
use
(
'/admin'
,
admin
);
app
.
use
(
'/accounts'
,
accounts
);
app
.
use
(
'/auth'
,
auth
);
app
.
use
(
'/chat'
,
chat
);
var
server
=
app
.
listen
(
port
,
function
()
{
console
.
log
(
'Express listening on port'
,
port
);
...
...
routes/Search.js
0 → 100644
View file @
b5e8441
var
Youtube
=
require
(
'youtube-node'
);
var
youtube
=
new
Youtube
();
var
express
=
require
(
'express'
);
var
router
=
express
.
Router
();
var
word
=
'백종원 레시피'
;
// 검색어 지정
var
limit
=
10
;
// 출력 갯수
var
video
=
[];
var
test
=
'test'
;
var
count
=
0
;
youtube
.
setKey
(
'AIzaSyAsKr_oWGZIBbL5tLdIl98Lf9Pzqj8jX4o'
);
// API 키 입력
youtube
.
addParam
(
'order'
,
'rating'
);
// 평점 순으로 정렬
youtube
.
addParam
(
'type'
,
'video'
);
// 타입 지정
youtube
.
addParam
(
'videoLicense'
,
'creativeCommon'
);
// 크리에이티브 커먼즈 아이템만 불러옴
youtube
.
search
(
word
,
limit
,
function
(
err
,
result
)
{
// 검색 실행
if
(
err
)
{
console
.
log
(
err
);
}
// 에러일 경우 에러공지하고 빠져나감
//console.log(JSON.stringify(result, null, 2)); // 받아온 전체 리스트 출력
var
items
=
result
[
'items'
];
// 결과 중 items 항목만 가져옴
for
(
var
i
in
items
)
{
var
it
=
items
[
i
];
for
(
var
j
in
it
)
{
if
(
it
[
j
][
'title'
]
!=
null
)
{
var
title
=
it
[
j
][
'title'
];
}
if
(
it
[
j
][
'videoId'
]
!=
null
)
{
var
video_id
=
it
[
j
][
'videoId'
];
}
var
urls
=
'https://www.youtube.com/watch?v='
+
video_id
;
}
var
item
=
{
id
:
count
,
title
:
title
,
video_id
:
video_id
,
urls
:
urls
,
};
count
++
;
video
.
push
(
item
);
}
});
router
.
get
(
'/'
,
function
(
req
,
res
)
{
res
.
render
(
'home'
,
{
videos
:
video
}
// DB에서 받은 videos를 videos변수명으로 내보냄
);
});
module
.
exports
=
router
;
Please
register
or
login
to post a comment