app.js
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 네이버 검색 API예제는 블로그를 비롯 전문자료까지 호출방법이 동일하므로 blog검색만 대표로 예제를 올렸습니다.
// 네이버 검색 Open API 예제 - 블로그 검색
// -> 이걸 영화로 바꿀라고 blog를 다 movie로 바꿈
var express = require('express');
var app = express();
app.set('view engine','ejs');
var fs = require('fs');
var client_id = 'Lqxd8LWDGMYTe9YWJPBb';
var client_secret = '9HrBpH8oy1';
app.get('/', function(req, res){
fs.readFile('fb_login.ejs', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/html '});
res.end(data);
});
});
app.get('/imgs/megabox',function (req,res) {
fs.readFile('mega.png', function (error, data) {
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end(data);
});
})
app.get('/main',function (req,res) {
fs.readFile('main.ejs', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/html '});
res.end(data);
});
})
app.get('/serach',function (req,res) {
var query= req.query;
var genre = query['genre'];
if(genre != ""){
var code = get_genre_code(genre);
get_info_using_genre(code,res);
}
})
function get_genre_code(genre_str) {
var genre_num;
switch (genre_str) {
case "Action":
genre_num = 19;
break;
case "Romance":
genre_num = 5;
break;
case "Horror":
genre_num = 4;
break;
case "Drama":
genre_num = 1;
break;
case "Fantasy":
genre_num = 2;
break;
case "Adventure":
genre_num = 6;
break;
case "Thriller":
genre_num = 7;
break;
case "Comedy":
genre_num = 11;
break;
case "War":
genre_num = 14;
break;
default:
}
return genre_num;
}
function get_info_using_genre(code,res) {
var test_search = "다크";
var en = encodeURI(test_search);
var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&genre=' + code; // json 결과
var request = require('request');
var options = {
url: api_url,
//headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
var total = data['total'];
var items = data['items'];
console.log(data);
for(var i=0; i < total; i++){
res.render('movie_info',{title:items[i]['title']});
}
//res.render('movie_info',{title:items[1]['title']});
}
});
}
app.get('/search/movie', function (req, res) {
var api_url = 'https://openapi.naver.com/v1/search/movie.json?query=%EC%A3%BC%EC%8B%9D&display=10&start=1&genre=1' + encodeURI(req.query.query); // json 결과
var request = require('request');
var options = {
url: api_url,
//headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
var total = data['total'];
var items = data['items'];
res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
// res.end(JSON.stringify(items));
res.end(body);
} else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
});
app.listen(3000, function () {
//console.log('http://127.0.0.1:3000/search/blog?query=검색어 app listening on port 3000!');
console.log('http://127.0.0.1:3000/search/movie?query=검색어 app listening on port 3000!');
});