Merge branch 'master' of http://khuhub.khu.ac.kr/2018104006/movie-reco-chatbot
Merge branch 'feature/DailyBoxOfficeList'(for version 1.0)
Showing
6 changed files
with
238 additions
and
0 deletions
DailyBoxOfficeList.js
0 → 100644
1 | +//============================================================= | ||
2 | +var express = require('express'); | ||
3 | +const request = require('request'); | ||
4 | +const config = require('./config.json'); | ||
5 | +//============================================================= | ||
6 | +const LINE_URL = 'https://api.line.me/v2/bot/message/reply'; | ||
7 | +const TOKEN = config.TOKEN; | ||
8 | +const KOFIC_URL = 'http://www.kobis.or.kr/kobisopenapi/webservice/rest'; | ||
9 | +const KOFIC_KEY = config.KOFIC_KEY; | ||
10 | +//============================================================= | ||
11 | + | ||
12 | +// 어제 기준 영화 순위(1위 ~ 5위) 출력 | ||
13 | +exports.ShowYesterdayRank = function(replyToken) { | ||
14 | + | ||
15 | + var yesterday = exports.GetYesterday(); | ||
16 | + | ||
17 | + request.get( | ||
18 | + { | ||
19 | + url: KOFIC_URL+`/boxoffice/searchDailyBoxOfficeList.json?key=${KOFIC_KEY}&targetDt=${yesterday}`, | ||
20 | + json:true | ||
21 | + },(error, response, body) => { | ||
22 | + if(!error && response.statusCode == 200) { | ||
23 | + console.log(body.boxOfficeResult); | ||
24 | + | ||
25 | + var movieName = []; | ||
26 | + movieName[0] = body.boxOfficeResult.dailyBoxOfficeList[0].movieNm; | ||
27 | + movieName[1] = body.boxOfficeResult.dailyBoxOfficeList[1].movieNm; | ||
28 | + movieName[2] = body.boxOfficeResult.dailyBoxOfficeList[2].movieNm; | ||
29 | + movieName[3] = body.boxOfficeResult.dailyBoxOfficeList[3].movieNm; | ||
30 | + movieName[4] = body.boxOfficeResult.dailyBoxOfficeList[4].movieNm; | ||
31 | + | ||
32 | + var movieOpenDt = []; | ||
33 | + movieOpenDt[0] = body.boxOfficeResult.dailyBoxOfficeList[0].openDt; | ||
34 | + movieOpenDt[1] = body.boxOfficeResult.dailyBoxOfficeList[1].openDt; | ||
35 | + movieOpenDt[2] = body.boxOfficeResult.dailyBoxOfficeList[2].openDt; | ||
36 | + movieOpenDt[3] = body.boxOfficeResult.dailyBoxOfficeList[3].openDt; | ||
37 | + movieOpenDt[4] = body.boxOfficeResult.dailyBoxOfficeList[4].openDt; | ||
38 | + | ||
39 | + var movieAudiAcc = []; | ||
40 | + movieAudiAcc[0] = exports.numberWithCommas(body.boxOfficeResult.dailyBoxOfficeList[0].audiAcc); | ||
41 | + movieAudiAcc[1] = exports.numberWithCommas(body.boxOfficeResult.dailyBoxOfficeList[1].audiAcc); | ||
42 | + movieAudiAcc[2] = exports.numberWithCommas(body.boxOfficeResult.dailyBoxOfficeList[2].audiAcc); | ||
43 | + movieAudiAcc[3] = exports.numberWithCommas(body.boxOfficeResult.dailyBoxOfficeList[3].audiAcc); | ||
44 | + movieAudiAcc[4] = exports.numberWithCommas(body.boxOfficeResult.dailyBoxOfficeList[4].audiAcc); | ||
45 | + | ||
46 | + request.post( | ||
47 | + { | ||
48 | + url: LINE_URL, | ||
49 | + headers: { | ||
50 | + 'Authorization': `Bearer ${TOKEN}` | ||
51 | + }, | ||
52 | + json: { | ||
53 | + "replyToken":replyToken, | ||
54 | + "messages":[ | ||
55 | + { | ||
56 | + "type":"text", | ||
57 | + "text": | ||
58 | + `[1위]\n영화제목 : ${movieName[0]}\n개봉일 : ${movieOpenDt[0]}\n누적 관객 수 : ${movieAudiAcc[0]}명\n\n`+ | ||
59 | + `[2위]\n영화제목 : ${movieName[1]}\n개봉일 : ${movieOpenDt[1]}\n누적 관객 수 : ${movieAudiAcc[1]}명\n\n`+ | ||
60 | + `[3위]\n영화제목 : ${movieName[2]}\n개봉일 : ${movieOpenDt[2]}\n누적 관객 수 : ${movieAudiAcc[2]}명\n\n`+ | ||
61 | + `[4위]\n영화제목 : ${movieName[3]}\n개봉일 : ${movieOpenDt[3]}\n누적 관객 수 : ${movieAudiAcc[3]}명\n\n`+ | ||
62 | + `[5위]\n영화제목 : ${movieName[4]}\n개봉일 : ${movieOpenDt[4]}\n누적 관객 수 : ${movieAudiAcc[4]}명\n\n` | ||
63 | + } | ||
64 | + ] | ||
65 | + } | ||
66 | + },(error, response, body) => { | ||
67 | + console.log(body) | ||
68 | + }); | ||
69 | + } | ||
70 | + }); | ||
71 | +} | ||
72 | + | ||
73 | + | ||
74 | +// 어제 날짜를 YYYYMMDD 형식(type: string)으로 반환하는 함수 | ||
75 | +exports.GetYesterday = function() { | ||
76 | + | ||
77 | + var today = new Date(); | ||
78 | + var yesterday = new Date(today.setDate(today.getDate() - 1)); | ||
79 | + | ||
80 | + var year = yesterday.getFullYear(); | ||
81 | + var month = ('0' + (yesterday.getMonth() + 1)).slice(-2); | ||
82 | + var day = ('0' + yesterday.getDate()).slice(-2); | ||
83 | + | ||
84 | + return (year + month + day); | ||
85 | +} | ||
86 | + | ||
87 | + | ||
88 | +// 숫자 사이에 콤마(,) 찍고 반환하는 함수(입력, 출력 모두 문자열) | ||
89 | +exports.numberWithCommas = function(x) { | ||
90 | + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | ||
91 | +} |
MovieInfo.js
0 → 100644
1 | +//============================================================= | ||
2 | +var express = require('express'); | ||
3 | +const request = require('request'); | ||
4 | +const config = require('./config.json'); | ||
5 | +//============================================================= | ||
6 | +const LINE_URL = 'https://api.line.me/v2/bot/message/reply' | ||
7 | +const TOKEN = config.TOKEN; | ||
8 | +const KOFIC_URL = 'http://www.kobis.or.kr/kobisopenapi/webservice/rest' | ||
9 | +const KOFIC_KEY = config.KOFIC_KEY; | ||
10 | +//============================================================= | ||
11 | + | ||
12 | +export function MovieInfo(replyToken, message) { | ||
13 | + request.get( | ||
14 | + { | ||
15 | + url: KOFIC_URL+`/movie/searchMovieInfo.json?key=${KOFIC_KEY}&movieCd=${message}`, | ||
16 | + json:true | ||
17 | + },(error, response, body) => { | ||
18 | + if(!error && response.statusCode == 200) { | ||
19 | + console.log(body.movieInfoResult); | ||
20 | + var MovieInfo = body.movieInfoResult.movieInfo; | ||
21 | + var MovieName = MovieInfo.movieNm; | ||
22 | + var MovieDate = `${MovieInfo.openDt.slice(0,4)}년 ${MovieInfo.openDt.slice(4,6)}월 ${MovieInfo.openDt.slice(6,8)}일`; | ||
23 | + var MovieTime = MovieInfo.showTm; | ||
24 | + var MovieGenres = []; | ||
25 | + for(var i in MovieInfo.genres) | ||
26 | + { | ||
27 | + MovieGenres.push(MovieInfo.genres[i].genreNm); | ||
28 | + } | ||
29 | + var MovieDirec = MovieInfo.directors[0].peopleNm; | ||
30 | + var MovieActors = []; | ||
31 | + for(var i=0; i<MovieInfo.actors.length && i<5; i++) | ||
32 | + { | ||
33 | + MovieActors[i] = MovieInfo.actors[i].peopleNm; | ||
34 | + } | ||
35 | + | ||
36 | + request.post( | ||
37 | + { | ||
38 | + url: TARGET_URL, | ||
39 | + headers: { | ||
40 | + 'Authorization': `Bearer ${TOKEN}` | ||
41 | + }, | ||
42 | + json: { | ||
43 | + "replyToken":replyToken, | ||
44 | + "messages":[ | ||
45 | + { | ||
46 | + "type":"text", | ||
47 | + "text":`영화명: ${MovieName}\n개봉날짜: ${MovieDate}\n상영시간: ${MovieTime}분\n장르: ${MovieGenres}\n감독: ${MovieDirec}\n출연배우: ${MovieActors}` | ||
48 | + } | ||
49 | + ] | ||
50 | + } | ||
51 | + },(error, response, body) => { | ||
52 | + console.log(body) | ||
53 | + }); | ||
54 | + } | ||
55 | + }); | ||
56 | +} |
config.json
0 → 100644
main.js
0 → 100644
1 | +//============================================================= | ||
2 | +var express = require('express'); | ||
3 | +const request = require('request'); | ||
4 | +const config = require('./config.json'); | ||
5 | +const dailyBoxOfficeList = require('./DailyBoxOfficeList.js'); | ||
6 | +//============================================================= | ||
7 | +const LINE_URL = 'https://api.line.me/v2/bot/message/reply' | ||
8 | +const TOKEN = config.TOKEN; | ||
9 | +const KOFIC_URL = 'http://www.kobis.or.kr/kobisopenapi/webservice/rest'; | ||
10 | +const KOFIC_KEY = config.KOFIC_KEY; | ||
11 | +//============================================================= | ||
12 | +const fs = require('fs'); | ||
13 | +const path = require('path'); | ||
14 | +const HTTPS = require('https'); | ||
15 | +const domain = config.domain; | ||
16 | +const sslport = 23023; | ||
17 | +const bodyParser = require('body-parser'); | ||
18 | +var app = express(); | ||
19 | +app.use(bodyParser.json()); | ||
20 | +//============================================================= | ||
21 | + | ||
22 | + | ||
23 | +// RECEIVE MESSAGE | ||
24 | +app.post('/hook', function (req, res) { | ||
25 | + | ||
26 | + var eventObj = req.body.events[0]; | ||
27 | + | ||
28 | + // console.log for debugging | ||
29 | + console.log('======================', new Date() ,'======================'); | ||
30 | + console.log('[request]', req.body); | ||
31 | + console.log('[request source] ', eventObj.source); | ||
32 | + console.log('[request message]', eventObj.message); | ||
33 | + | ||
34 | + ClassifyMessage(eventObj.replyToken, eventObj.message.text); | ||
35 | + | ||
36 | + res.sendStatus(200); | ||
37 | +}); | ||
38 | + | ||
39 | + | ||
40 | +// CLASSIFY MESSAGE | ||
41 | +function ClassifyMessage(replyToken, imessage){ | ||
42 | + | ||
43 | + var message = String(imessage); | ||
44 | + | ||
45 | + // 사용자가 보낸 라인 메시지 문자열 안에 특정 문자열이 있으면, 특정 함수 실행 | ||
46 | + if(message.includes('최신') || message.includes('순위') || message.includes('오늘') || message.includes('추천')) { | ||
47 | + dailyBoxOfficeList.ShowYesterdayRank(replyToken); | ||
48 | + } else if (message.includes('줄거리')) { | ||
49 | + // (예시) 영화 줄거리 출력 | ||
50 | + } | ||
51 | + else if (message.includes('목록')) { | ||
52 | + // (예시) 영화 목록 출력 | ||
53 | + } | ||
54 | +} | ||
55 | + | ||
56 | + | ||
57 | +// ※ WARNING: DO NOT TOUCH THIS CODE SECTION ※ | ||
58 | +try { | ||
59 | + const option = { | ||
60 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
61 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
62 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
63 | + }; | ||
64 | + | ||
65 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
66 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
67 | + }); | ||
68 | +} catch (error) { | ||
69 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
70 | + console.log(error); | ||
71 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
package.json
0 → 100644
1 | +{ | ||
2 | + "name": "trans", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "main": "app.js", | ||
6 | + "scripts": { | ||
7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | + }, | ||
9 | + "author": "", | ||
10 | + "license": "ISC", | ||
11 | + "dependencies": { | ||
12 | + "express": "^4.17.1", | ||
13 | + "request": "^2.88.2" | ||
14 | + } | ||
15 | +} |
-
Please register or login to post a comment