MovieRecommend.js
5.04 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
const request = require('request');
var config = require('../config.json');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = config.TOKEN;
const KOFIC_URL = 'http://www.kobis.or.kr/kobisopenapi/webservice/rest'
//Return random integer.
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //Exclude maximum value, include minimum value
}
//Get weekend boxoffice movie code.
//Then return list fill with movie code.
function weekendBoxOfficeMovie() {
var year = String(getRandomInt(2010,2023));
var date = String(getRandomInt(1,13));
if(year == 2022){
date = String(getRandomInt(1,6));
}
if(date < 10){
date = '0'+date;
}
date += '15';
return new Promise((resolve) => {
var moviecode = [];
request.get(
{
url: KOFIC_URL+`/boxoffice/searchWeeklyBoxOfficeList.json?key=${config.KOFIC_KEY}&targetDt=${year + date}&itemPerPage=7`,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
for(let i = 0; i < body.boxOfficeResult.weeklyBoxOfficeList.length; i++){
moviecode.push(body.boxOfficeResult.weeklyBoxOfficeList[i].movieCd)
}
resolve(moviecode)
}
});
});
}
//Returns the title, year of release, names of directors and actors.
//The return format is array and index is as follows:
//[title, year of release, director, actor1, actor2, moviecode]
async function movieinfo(message){
moviecdlist = await weekendBoxOfficeMovie();
return new Promise((resolve) => {
movieresult = [];
for(let i = 0; i < moviecdlist.length; i++){
request.get(
{
url: KOFIC_URL + `/movie/searchMovieInfo.json?key=${config.KOFIC_KEY}&movieCd=${moviecdlist[i]}`,
json:true
}
,(error,response, body) => {
if(!error && response.statusCode == 200) {
for(let j = 0; j < body.movieInfoResult.movieInfo.genres.length; j++){
if(body.movieInfoResult.movieInfo.genres[j].genreNm == message){
var title = body.movieInfoResult.movieInfo.movieNm;
var openyear = body.movieInfoResult.movieInfo.prdtYear;
if(body.movieInfoResult.movieInfo.directors.length == 0){
var director = "감독정보없음"
}
else{
var director = body.movieInfoResult.movieInfo.directors[0].peopleNm
}
if(body.movieInfoResult.movieInfo.actors.length == 0){
var actor_1 = "배우정보없음"
var actor_2 = "배우정보없음"
}
else if(body.movieInfoResult.movieInfo.actors.length == 1){
var actor_1 = body.movieInfoResult.movieInfo.actors[0].peopleNm
var actor_2 = "배우정보없음"
}
else{
var actor_1 = body.movieInfoResult.movieInfo.actors[0].peopleNm
var actor_2 = body.movieInfoResult.movieInfo.actors[1].peopleNm
}
var moviecode_rc = body.movieInfoResult.movieInfo.movieCd
movieresult.push([title, openyear, director, actor_1, actor_2, moviecode_rc])
} //제목, 개봉년도, 감독, 배우1, 배우2, 영화코드
}
resolve(movieresult);
}
});
}
});
}
//Enter a movie genre in the message variable.
//It will then return movie title, year of release, director, actor and moviecode information to Line Messenger.
exports.movieRecommend = async function(replyToken, message){
var movieresult = [];
while(1){
movielist = await movieinfo(message);
for(let i = 0; i < movielist.length; i ++){
movieresult.push(movielist[i]);
}
if(movieresult.length > 1){
break;
}
}
var movierecommend_output = '';
for(let i = 0; i < movieresult.length; i++){
movierecommend_output += `제목: ${movieresult[i][0]}(${movieresult[i][1]})\n감독: ${movieresult[i][2]}\n배우: ${movieresult[i][3]}, ${movieresult[i][4]}\n영화코드: ${movieresult[i][5]}\n\n`
}
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":movierecommend_output
}
]
}
},(error, response, body) => {
console.log(body)
});
}