나혜원

add concert function

Showing 1 changed file with 48 additions and 34 deletions
...@@ -5,7 +5,7 @@ const line = require('@line/bot-sdk'); ...@@ -5,7 +5,7 @@ const line = require('@line/bot-sdk');
5 var request = require('request'); 5 var request = require('request');
6 var https=require('https'); 6 var https=require('https');
7 var http=require('http'); 7 var http=require('http');
8 -/* 8 +
9 const lex = require('greenlock-express').create({ 9 const lex = require('greenlock-express').create({
10 version: 'draft-11', // 버전2 10 version: 'draft-11', // 버전2
11 store: require('greenlock-store-fs'), 11 store: require('greenlock-store-fs'),
...@@ -30,7 +30,7 @@ https.createServer(lex.httpsOptions, lex.middleware(app)).listen((process.env.SS ...@@ -30,7 +30,7 @@ https.createServer(lex.httpsOptions, lex.middleware(app)).listen((process.env.SS
30 http.createServer(lex.middleware(require('redirect-https')())).listen(process.env.PORT || 80,()=>{ 30 http.createServer(lex.middleware(require('redirect-https')())).listen(process.env.PORT || 80,()=>{
31 console.log("server on 80"); 31 console.log("server on 80");
32 }); 32 });
33 -*/ 33 +
34 //번역 api_url 34 //번역 api_url
35 var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; 35 var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt';
36 36
...@@ -70,7 +70,7 @@ for(var i = 0; i < data_list.length; i++){ ...@@ -70,7 +70,7 @@ for(var i = 0; i < data_list.length; i++){
70 } 70 }
71 71
72 for(var i = 0; i < concert_list.length; i++){ 72 for(var i = 0; i < concert_list.length; i++){
73 - console.log(i+1,') ',concert_list[i],'\n'); 73 + console.log(i+1,'. ',concert_list[i],'\n');
74 } 74 }
75 //콘서트 목록 75 //콘서트 목록
76 76
...@@ -102,41 +102,55 @@ function handleEvent(event) { ...@@ -102,41 +102,55 @@ function handleEvent(event) {
102 console.log(showlist.text); 102 console.log(showlist.text);
103 client.replyMessage(event.replyToken, showlist).then(resolve).catch(reject); 103 client.replyMessage(event.replyToken, showlist).then(resolve).catch(reject);
104 }); 104 });
105 +
105 } 106 }
107 + else if(isNaN(event.message.text)){
106 108
107 - var selectnum = 1; 109 + var selectnum = (event.message.text*1);
108 - var concert_name = concert_list[selectnum]; 110 +
109 - var $ = cheerio.load(concert_name); 111 + if(selectnum >=0 || selectnum <=27){
110 - var keyword = $.text(); 112 + result.text = '목록에 존재하지 않는 콘서트입니다.';
111 - console.log(keyword); 113 + client.replyMessage(event.replyToken, result).then(resolve).catch(reject);
112 - //키워드 추출
113 -
114 - var options = { method: 'GET',
115 - url: 'https://www.googleapis.com/youtube/v3/search',
116 - qs:
117 - { key: 'AIzaSyB4b-n8SSv73CLDKvFigpLPYA6yWG2JQ9A',
118 - part: 'id',
119 - maxResults: '3',
120 - order: 'viewCount',
121 - q: keyword,
122 - type: 'video'},
123 - };
124 - //youtube api
125 -
126 - request(options, function(error,result,body){
127 - if(error) throw new Error(error);
128 -
129 - //var temp = body.replace('{','');
130 - //var temp = temp.replace('}','');
131 - var temp1 = body.split(':');
132 -
133 - for(var i = 0; i < temp1.length; i++){
134 - console.log(i,temp1[i],'\n');
135 } 114 }
115 +
116 + return new Promise(function(resolve, reject){
117 + var concert_name = concert_list[selectnum-1];
118 + var $ = cheerio.load(concert_name);
119 + var keyword = $.text();
120 + console.log(keyword);
136 121
137 - //console.log(temp1); 122 + var options = {method: 'GET',
138 - }); 123 + url: 'https://www.googleapis.com/youtube/v3/search',
139 - //search on youtube 124 + qs: { key: 'AIzaSyB4b-n8SSv73CLDKvFigpLPYA6yWG2JQ9A',
125 + part: 'id',
126 + maxResults: '1',
127 + order: 'relevance',
128 + q: keyword,
129 + type: 'video'}
130 + };
131 +
132 + var videourl = { type: 'url', label: '관련 영상', url:''};
133 +
134 + request(options, function(error,result,body){
135 + if(error) throw new Error(error);
136 +
137 + var temp = body.split(':');
138 + videourl.url += "https://www.youtube.com/watch?v=" + temp1[13].substr(2,11);
139 + });
140 +
141 + console.log(videourl.url);
142 +
143 + client.replyMessage(event.replyToken, videourl).then(resolve).catch(reject);
144 + })};
145 +
146 + app.post('/webhook', line.middleware(config), (req, res) => {
147 + console.log(res.statusCode);
148 + Promise
149 + .all(req.body.events.map(handleEvent2))
150 + .then((result) => res.json(result))
151 + .catch((err)=>{console.log(err);
152 + })
153 + });
140 154
141 return new Promise(function(resolve, reject) { 155 return new Promise(function(resolve, reject) {
142 //언어 감지 option 156 //언어 감지 option
......