app.js
9.96 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// 네이버 검색 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.use('/', express.static(__dirname + '/../public'));
app.get('/errata', function (req, res) {
var api_url = 'https://openapi.naver.com/v1/search/errata.json?query=' + encodeURI(req.query.query); // json 결과
var request = require('request');
console.log(req.query.query);
console.log(req.query);
var options = {
url: api_url,
headers: {'X-Naver-Client-Id':'3VmmpKXfOwySxf5ONdKM', 'X-Naver-Client-Secret': 'q0xaSGu8Vl'}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
console.log(body);
res.end(body);
} else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
});
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('/css/style.css', function(req, res){
fs.readFile('css/style.css', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/css '});
res.end(data);
});
});
app.get('/css/bootstrap.min.css', function(req, res){
fs.readFile('/css/bootstrap.min.css', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/css '});
res.end(data);
});
});
app.get('/css/bootstrap.css', function(req, res){
fs.readFile('/css/bootstrap.css', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/css '});
res.end(data);
});
});
app.get('/css/bootstrap-theme.css', function(req, res){
fs.readFile('/css/bootstrap-theme.css', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/css '});
res.end(data);
});
});
app.get('/css/bootstrap-theme.min.css', function(req, res){
fs.readFile('/css/bootstrap-theme.min.css', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/css '});
res.end(data);
});
});
app.get('/css/bootstrap-theme.css.map', function(req, res){
fs.readFile('/css/bootstrap-theme.css.map', function(error, data){
res.writeHead(200, {'Content-Type' : 'text/css '});
res.end(data);
});
});
app.get('/js/bootstrap.js', function(req, res){
fs.readFile('/js/bootstrap.js', function(error, data){
res.writeHead(200, {'Content-Type' : 'Application/javascript'});
res.end(data);
});
});
app.get('/js/bootstrap.min.js', function(req, res){
fs.readFile('/js/bootstrap.min.js', function(error, data){
res.writeHead(200, {'Content-Type' : 'Application/javascript'});
res.end(data);
});
});
app.get('/js/npm.js', function(req, res){
fs.readFile('/js/npm.js', function(error, data){
res.writeHead(200, {'Content-Type' : 'Application/javascript'});
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'];
var year = query['year'];
var country = query['country'];
var search = query['search'];
var searchAll = query['searchAll'];
if(genre != null){
var code = get_genre_code(genre);
get_info_using_genre(code,search, res);
}
else if(year != null){
var year_num = get_year_num(year);
get_info_using_year(year_num,search, res);
}
else if(country != null){
var country_real = makeETC(country);
get_info_using_country(country_real,search, res);
}
else if(searchAll != null){
get_info_all(searchAll, res);
}
})
function makeETC(country_input){
var country_input;
if(country_input == "ET"){
country_input = "ETC"
}
return country_input;
}
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_year_num(year){
var return_year;
switch (year) {
case "1980년대":
return_year = 1980;
break;
case "1990년대":
return_year = 1990;
break;
case "2000년대":
return_year = 2000;
break;
case "2010년대":
return_year = 2010;
break;
}
return return_year;
}
//장르 검색
function get_info_using_genre(code,search, res) {
var en = encodeURI(search);
var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'+'&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);
res.render('movie_info',{item:items});
}
});
}
//연도 검색
function get_info_using_year(year_num,search, res) {
var en = encodeURI(search);
var yearto = year_num+10;
var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+ '&display=100' + '&yearfrom=' + year_num + '&yearto=' + yearto // json 결과
//var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'; // 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'];
// var temps = items;
// var j = 0;
// for(var i = 0 ; i < total; i++){
// if(items[i]['pubDate'] >= year && items[i]['pubDate'] <= year+10 ){
// temps[j] = items[i];
// }
// }
console.log(data);
res.render('movie_info',{item:items});
}
});
}
//국가 검색
function get_info_using_country(country,search, res) {
var en = encodeURI(search);
var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'+'&country=' + country; // 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);
res.render('movie_info',{item:items});
}
});
}
//전체 검색
// 미정 /미확인 이딴거 두개만 뜸
function get_info_all(searchAll, res) {
var en = encodeURI(searchAll);
var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'; // 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);
res.render('movie_info',{item:items});
}
});
}
// 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!');
});