나혜원

update app.js

Showing 1 changed file with 69 additions and 1 deletions
......@@ -5,6 +5,7 @@ const line = require('@line/bot-sdk');
var request = require('request');
var https=require('https');
var http=require('http');
/*
const lex = require('greenlock-express').create({
version: 'draft-11', // 버전2
store: require('greenlock-store-fs'),
......@@ -29,6 +30,7 @@ https.createServer(lex.httpsOptions, lex.middleware(app)).listen((process.env.SS
http.createServer(lex.middleware(require('redirect-https')())).listen(process.env.PORT || 80,()=>{
console.log("server on 80");
});
*/
//번역 api_url
var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt';
......@@ -54,6 +56,23 @@ const client = new line.Client(config);
// register a webhook handler with middleware
// about the middleware, please refer to doc
var cheerio = require('cheerio');
var data = require('./event.json');
//json 불러오기
var data_list = data["DATA"]
var concert_list = [];
for(var i = 0; i < data_list.length; i++){
if (data_list[i].codename=="콘서트"){
concert_list.push(data_list[i].title);
}
}
for(var i = 0; i < concert_list.length; i++){
console.log(i+1,') ',concert_list[i],'\n');
}
//콘서트 목록
app.post('/webhook', line.middleware(config), (req, res) => {
console.log(res.statusCode);
......@@ -61,9 +80,9 @@ console.log(res.statusCode);
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err)=>{console.log(err);
console.log(err.originalError.response)
})
});
// event handler
function handleEvent(event) {
console.log(event.message);
......@@ -71,6 +90,54 @@ function handleEvent(event) {
// ignore non-text-message event
return Promise.resolve(null);
}
else if(event.message.text.substring(0,6)=='콘서트 보기'){
return new Promise(function(resolve, reject)
{
var showlist = { type: 'text', text:''};
for(var i = 0; i < concert_list.length; i++)
{
showlist.text+=i+1 + ". " + concert_list[i] + "\n";
}
console.log(showlist.text);
client.replyMessage(event.replyToken, showlist).then(resolve).catch(reject);
});
}
var selectnum = 1;
var concert_name = concert_list[selectnum];
var $ = cheerio.load(concert_name);
var keyword = $.text();
console.log(keyword);
//키워드 추출
var options = { method: 'GET',
url: 'https://www.googleapis.com/youtube/v3/search',
qs:
{ key: 'AIzaSyB4b-n8SSv73CLDKvFigpLPYA6yWG2JQ9A',
part: 'id',
maxResults: '3',
order: 'viewCount',
q: keyword,
type: 'video'},
};
//youtube api
request(options, function(error,result,body){
if(error) throw new Error(error);
//var temp = body.replace('{','');
//var temp = temp.replace('}','');
var temp1 = body.split(':');
for(var i = 0; i < temp1.length; i++){
console.log(i,temp1[i],'\n');
}
//console.log(temp1);
});
//search on youtube
return new Promise(function(resolve, reject) {
//언어 감지 option
var detect_options = {
......@@ -79,6 +146,7 @@ function handleEvent(event) {
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
//papago 언어 감지
request.post(detect_options,async (error,response,body)=>{
console.log(response.statusCode);
if(!error && response.statusCode == 200){
......