권은령

Add displaying music chart and lyrics translation

Showing 1 changed file with 122 additions and 35 deletions
...@@ -18,33 +18,6 @@ var httpsOptions = { ...@@ -18,33 +18,6 @@ var httpsOptions = {
18 http.createServer(app).listen(80); 18 http.createServer(app).listen(80);
19 https.createServer(httpsOptions, app).listen(443); 19 https.createServer(httpsOptions, app).listen(443);
20 20
21 -/*
22 -const lex= require('greenlock-express').create({
23 - version: 'draft-11', // 버전2
24 - store: require('greenlock-store-fs'),
25 - configDir: '/etc/letsencrypt', // 또는 ~/letsencrypt/etc
26 - approveDomains: (opts, certs, cb) => {
27 - if (certs) {
28 - opts.domains = ['oss.chatbot.bu.to', 'www.oss.chatbot.bu.to'];
29 - } else {
30 - opts.email = 'sweun1@naver.com';
31 - opts.agreeTos = true;
32 - }
33 - cb(null, { options: opts, certs });
34 -
35 - },
36 - renewWithin: 81 * 24 * 60 * 60 * 1000,
37 - renewBy: 80 * 24 * 60 * 60 * 1000,
38 -});
39 -https.createServer(lex.httpsOptions, lex.middleware(app)).listen((process.env.SSL_PORT || 443),()=>{
40 - console.log("server on 443");
41 -});
42 -http.createServer(lex.middleware(require('redirect-https')())).listen(process.env.PORT || 80,()=>{
43 - console.log("server on 80");
44 -});
45 -*/
46 -//papago api
47 -
48 21
49 //번역 api_url 22 //번역 api_url
50 var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; 23 var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt';
...@@ -86,7 +59,127 @@ function handleEvent(event) { ...@@ -86,7 +59,127 @@ function handleEvent(event) {
86 // ignore non-text-message event 59 // ignore non-text-message event
87 return Promise.resolve(null); 60 return Promise.resolve(null);
88 } 61 }
89 - else if (event.type == 'message'&& event.message.type == "text"&&event.message.text.indexOf('http')!=-1) { 62 + else if(event.message.text.substring(0, 5)=='차트 보기')
63 + {
64 + // music list 출력
65 + var url="https://www.genie.co.kr/chart/top200";
66 +
67 + request(url, function(error, response, html){
68 + var $ = cheerio.load(html);
69 + const $bodyList= $('#body-content > div.newest-list > div > table > tbody > tr');
70 +
71 + $bodyList.each(function(i, elem){
72 + if(i<20)
73 + {
74 + songList.push(
75 + {
76 + singer: $(this).find("td.info").find("a.artist.ellipsis").text().trim(),
77 + song: $(this).find("td.info").find("a.title.ellipsis").text().trim(),
78 + url: 'https://www.genie.co.kr/detail/songInfo?xgnm='+$(this).attr("songid")
79 + });
80 + }
81 + else
82 + {
83 + return;
84 + }
85 + });
86 +
87 + return new Promise(function(resolve, reject)
88 + {
89 + var result = { type: 'text', text:''};
90 +
91 + for(var i=0; i<songList.length; i++)
92 + {
93 + result.text+=i+1 + ". "+ songList[i].singer+" - "+songList[i].song+"\n";
94 + }
95 + console.log(result.text);
96 + client.replyMessage(event.replyToken, result).then(resolve).catch(reject);
97 + });
98 + });
99 + }
100 + else if(event.message.text.substring(0, 5)=='가사 검색')
101 + {
102 + var userNum=event.message.text[6]
103 + var newUrl=songList[userNum-1].url;
104 + var lyric='';
105 +
106 + request(newUrl, function(error, response, html)
107 + {
108 + var $ = cheerio.load(html);
109 +
110 + lyric=$('#pLyrics > p').text();
111 + lyric=lyric.substring(0, 150);
112 +
113 + return new Promise(function(resolve, reject)
114 + {
115 + //언어 감지 option
116 + var detect_options =
117 + {
118 + url : languagedetect_api_url,
119 + form : {'query': lyric},
120 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
121 + };
122 +
123 + console.log(songList[userNum].song);
124 + console.log(lyric);
125 +
126 + //papago 언어 감지
127 + request.post(detect_options, (error,response,body)=>
128 + {
129 + if(!error && response.statusCode == 200)
130 + {
131 + var detect_body = JSON.parse(response.body);
132 + var source = '';
133 + var target = '';
134 + var result = { type: 'text', text:''};
135 +
136 + //언어 감지가 제대로 됐는지 확인
137 + console.log(detect_body.langCode);
138 +
139 + //번역은 한국어->영어 / 영어->한국어만 지원
140 + if(detect_body.langCode == 'ko'||detect_body.langCode == 'en')
141 + {
142 + source = detect_body.langCode == 'ko' ? 'ko':'en';
143 + target = source == 'ko' ? 'en':'ko';
144 + //papago 번역 option
145 + var options = {
146 + url: translate_api_url,
147 + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
148 + form: {'source':source, 'target':target, 'text': lyric},
149 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
150 + };
151 +
152 + // Naver Post API
153 + request.post(options, function(error, response, body){
154 + // Translate API Sucess
155 + if(!error && response.statusCode == 200){
156 + // JSON
157 + var objBody = JSON.parse(response.body);
158 + // Message 잘 찍히는지 확인
159 +
160 + result.text = objBody.message.result.translatedText;
161 + console.log("result: "+result.text);
162 + //번역된 문장 보내기
163 + client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
164 + }
165 + });
166 + }
167 + // 메시지의 언어가 영어 또는 한국어가 아닐 경우
168 + else
169 + {
170 + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
171 + client.replyMessage(event.replyToken, result).then(resolve).catch(reject);
172 + }
173 + }
174 + else
175 + {
176 + console.log("status code is not 200");
177 + }
178 + });
179 + });
180 + });
181 + }
182 + else if (event.type == 'message' && event.message.type == "text" && event.message.text.indexOf('http')!=-1) {
90 return new Promise(async(resolve,reject)=>{ 183 return new Promise(async(resolve,reject)=>{
91 var cheerio = require('cheerio'); 184 var cheerio = require('cheerio');
92 var uriBase = 'https://koreacentral.api.cognitive.microsoft.com/vision/v2.1/ocr'; 185 var uriBase = 'https://koreacentral.api.cognitive.microsoft.com/vision/v2.1/ocr';
...@@ -143,7 +236,6 @@ function handleEvent(event) { ...@@ -143,7 +236,6 @@ function handleEvent(event) {
143 }); 236 });
144 }); 237 });
145 }); 238 });
146 -
147 } 239 }
148 return new Promise(function(resolve, reject) { 240 return new Promise(function(resolve, reject) {
149 //언어 감지 option 241 //언어 감지 option
...@@ -199,18 +291,13 @@ function handleEvent(event) { ...@@ -199,18 +291,13 @@ function handleEvent(event) {
199 result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; 291 result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
200 client.replyMessage(event.replyToken,result).then(resolve).catch(reject); 292 client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
201 } 293 }
202 -
203 } 294 }
204 else{ 295 else{
205 console.log("status code is not 200"); 296 console.log("status code is not 200");
206 } 297 }
207 -
208 -
209 -
210 }); 298 });
211 -
212 }); 299 });
213 } 300 }
214 app.get('/',(req,res)=>{ 301 app.get('/',(req,res)=>{
215 res.send("hellow"); 302 res.send("hellow");
216 -}) 303 +})
...\ No newline at end of file ...\ No newline at end of file
......