Showing
1 changed file
with
126 additions
and
0 deletions
MovieRecommend.js
0 → 100644
1 | +const request = require('request'); | ||
2 | +var config = require('./config.json'); | ||
3 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
4 | +const TOKEN = config.TOKEN; | ||
5 | +const KOFIC_URL = 'http://www.kobis.or.kr/kobisopenapi/webservice/rest' | ||
6 | + | ||
7 | +//Return random integer. | ||
8 | +function getRandomInt(min, max) { | ||
9 | + min = Math.ceil(min); | ||
10 | + max = Math.floor(max); | ||
11 | + return Math.floor(Math.random() * (max - min)) + min; //Exclude maximum value, include minimum value | ||
12 | +} | ||
13 | + | ||
14 | +//Get weekend boxoffice movie code. | ||
15 | +//Then return list fill with movie code. | ||
16 | +function weekendBoxOfficeMovie() { | ||
17 | + var year = String(getRandomInt(2010,2023)); | ||
18 | + var date = String(getRandomInt(1,13)); | ||
19 | + if(year == 2022){ | ||
20 | + date = String(getRandomInt(1,6)); | ||
21 | + } | ||
22 | + if(date < 10){ | ||
23 | + date = '0'+date; | ||
24 | + } | ||
25 | + date += '15'; | ||
26 | + return new Promise((resolve) => { | ||
27 | + var moviecode = []; | ||
28 | + request.get( | ||
29 | + { | ||
30 | + url: KOFIC_URL+`/boxoffice/searchWeeklyBoxOfficeList.json?key=${config.KOFIC_KEY}&targetDt=${year + date}&itemPerPage=7`, | ||
31 | + json:true | ||
32 | + },(error, response, body) => { | ||
33 | + if(!error && response.statusCode == 200) { | ||
34 | + for(let i = 0; i < body.boxOfficeResult.weeklyBoxOfficeList.length; i++){ | ||
35 | + moviecode.push(body.boxOfficeResult.weeklyBoxOfficeList[i].movieCd) | ||
36 | + } | ||
37 | + resolve(moviecode) | ||
38 | + } | ||
39 | + }); | ||
40 | + }); | ||
41 | +} | ||
42 | +//Returns the title, year of release, names of directors and actors. | ||
43 | +//The return format is array and index is as follows: | ||
44 | +//[title, year of release, director, actor1, actor2] | ||
45 | +async function movieinfo(message){ | ||
46 | + moviecdlist = await weekendBoxOfficeMovie(); | ||
47 | + | ||
48 | + return new Promise((resolve) => { | ||
49 | + movieresult = []; | ||
50 | + for(let i = 0; i < moviecdlist.length; i++){ | ||
51 | + request.get( | ||
52 | + { | ||
53 | + url: KOFIC_URL + `/movie/searchMovieInfo.json?key=${config.KOFIC_KEY}&movieCd=${moviecdlist[i]}`, | ||
54 | + json:true | ||
55 | + } | ||
56 | + ,(error,response, body) => { | ||
57 | + if(!error && response.statusCode == 200) { | ||
58 | + for(let j = 0; j < body.movieInfoResult.movieInfo.genres.length; j++){ | ||
59 | + if(body.movieInfoResult.movieInfo.genres[j].genreNm == message){ | ||
60 | + var title = body.movieInfoResult.movieInfo.movieNm; | ||
61 | + var openyear = body.movieInfoResult.movieInfo.prdtYear; | ||
62 | + if(body.movieInfoResult.movieInfo.directors.length == 0){ | ||
63 | + var director = "감독정보없음" | ||
64 | + } | ||
65 | + else{ | ||
66 | + var director = body.movieInfoResult.movieInfo.directors[0].peopleNm | ||
67 | + } | ||
68 | + if(body.movieInfoResult.movieInfo.actors.length == 0){ | ||
69 | + var actor_1 = "배우정보없음" | ||
70 | + var actor_2 = "배우정보없음" | ||
71 | + } | ||
72 | + else if(body.movieInfoResult.movieInfo.actors.length == 1){ | ||
73 | + var actor_1 = body.movieInfoResult.movieInfo.actors[0].peopleNm | ||
74 | + var actor_2 = "배우정보없음" | ||
75 | + } | ||
76 | + else{ | ||
77 | + var actor_1 = body.movieInfoResult.movieInfo.actors[0].peopleNm | ||
78 | + var actor_2 = body.movieInfoResult.movieInfo.actors[1].peopleNm | ||
79 | + } | ||
80 | + movieresult.push([title, openyear, director, actor_1, actor_2]) | ||
81 | + | ||
82 | + } //제목, 개봉년도, 감독, 배우1, 배우2 | ||
83 | + } | ||
84 | + resolve(movieresult); | ||
85 | + } | ||
86 | + }); | ||
87 | + } | ||
88 | + }); | ||
89 | +} | ||
90 | + | ||
91 | +//Enter a movie genre in the message variable. | ||
92 | +//It will then return movie title, year of release, director and actor information to Line Messenger. | ||
93 | +exports.movieRecommend = async function(replyToken, message){ | ||
94 | + var movieresult = []; | ||
95 | + while(1){ | ||
96 | + movielist = await movieinfo(message); | ||
97 | + for(let i = 0; i < movielist.length; i ++){ | ||
98 | + movieresult.push(movielist[i]); | ||
99 | + } | ||
100 | + if(movieresult.length > 1){ | ||
101 | + break; | ||
102 | + } | ||
103 | + } | ||
104 | + var movierecommend_output = ''; | ||
105 | + for(let i = 0; i < movieresult.length; i++){ | ||
106 | + movierecommend_output += `제목: ${movieresult[i][0]}(${movieresult[i][1]})\n감독: ${movieresult[i][2]}\n배우: ${movieresult[i][3]}, ${movieresult[i][4]}\n` | ||
107 | + } | ||
108 | + request.post( | ||
109 | + { | ||
110 | + url: TARGET_URL, | ||
111 | + headers: { | ||
112 | + 'Authorization': `Bearer ${TOKEN}` | ||
113 | + }, | ||
114 | + json: { | ||
115 | + "replyToken":replyToken, | ||
116 | + "messages":[ | ||
117 | + { | ||
118 | + "type":"text", | ||
119 | + "text":movierecommend_output | ||
120 | + } | ||
121 | + ] | ||
122 | + } | ||
123 | + },(error, response, body) => { | ||
124 | + console.log(body) | ||
125 | + }); | ||
126 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment