권은령

apply lyrics translation

...@@ -158,14 +158,86 @@ function handleEvent(event) ...@@ -158,14 +158,86 @@ function handleEvent(event)
158 }); 158 });
159 }); 159 });
160 } 160 }
161 - else if(event.message.text.substring(0, 7)=='가사 검색 1') 161 + else if(event.message.text.substring(0, 5)=='가사 검색')
162 { 162 {
163 - return new Promise(function(resolve, reject) 163 + var userNum=event.message.text[6]
164 + var newUrl=songList[userNum].url;
165 + var lyric='';
166 +
167 + request(newUrl, function(error, response, html)
164 { 168 {
165 - var result = { type: 'text', text:''}; 169 + var $ = cheerio.load(html);
166 - result.text=songList[1].url;
167 170
168 - client.replyMessage(event.replyToken, result).then(resolve).catch(reject); 171 + lyric=$('#pLyrics > p').text();
172 + lyric=lyric.substring(0, 150);
173 +
174 + console.log(songList[userNum].song);
175 + console.log(lyric);
176 + });
177 +
178 + return new Promise(function(resolve, reject)
179 + {
180 + //언어 감지 option
181 + var detect_options =
182 + {
183 + url : languagedetect_api_url,
184 + form : {'query': lyric},
185 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
186 + };
187 +
188 + //papago 언어 감지
189 + request.post(detect_options, (error,response,body)=>
190 + {
191 + if(!error && response.statusCode == 200)
192 + {
193 + var detect_body = JSON.parse(response.body);
194 + var source = '';
195 + var target = '';
196 + var result = { type: 'text', text:''};
197 +
198 + //언어 감지가 제대로 됐는지 확인
199 + console.log(detect_body.langCode);
200 +
201 + //번역은 한국어->영어 / 영어->한국어만 지원
202 + if(detect_body.langCode == 'ko'||detect_body.langCode == 'en')
203 + {
204 + source = detect_body.langCode == 'ko' ? 'ko':'en';
205 + target = source == 'ko' ? 'en':'ko';
206 + //papago 번역 option
207 + var options = {
208 + url: translate_api_url,
209 + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
210 + form: {'source':source, 'target':target, 'text': lyric},
211 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
212 + };
213 +
214 + // Naver Post API
215 + request.post(options, function(error, response, body){
216 + // Translate API Sucess
217 + if(!error && response.statusCode == 200){
218 + // JSON
219 + var objBody = JSON.parse(response.body);
220 + // Message 잘 찍히는지 확인
221 +
222 + result.text = objBody.message.result.translatedText;
223 + console.log("result: "+result.text);
224 + //번역된 문장 보내기
225 + client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
226 + }
227 + });
228 + }
229 + // 메시지의 언어가 영어 또는 한국어가 아닐 경우
230 + else
231 + {
232 + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
233 + client.replyMessage(event.replyToken, result).then(resolve).catch(reject);
234 + }
235 + }
236 + else
237 + {
238 + console.log("status code is not 200");
239 + }
240 + });
169 }); 241 });
170 } 242 }
171 else 243 else
......
...@@ -35,7 +35,7 @@ request(url, function(error, response, html) ...@@ -35,7 +35,7 @@ request(url, function(error, response, html)
35 //console.log(i+1 + ". "+ songList[i].singer+" - "+songList[i].song); 35 //console.log(i+1 + ". "+ songList[i].singer+" - "+songList[i].song);
36 } 36 }
37 37
38 - console.log(result); 38 + //console.log(result);
39 var randnum=function getRandomInt(min, max) 39 var randnum=function getRandomInt(min, max)
40 { 40 {
41 min = Math.ceil(min); 41 min = Math.ceil(min);
...@@ -49,11 +49,12 @@ request(url, function(error, response, html) ...@@ -49,11 +49,12 @@ request(url, function(error, response, html)
49 request(newUrl, function(error, response, html) 49 request(newUrl, function(error, response, html)
50 { 50 {
51 var $ = cheerio.load(html); 51 var $ = cheerio.load(html);
52 +
52 53
53 var lyric=$('#pLyrics > p').text(); 54 var lyric=$('#pLyrics > p').text();
54 - var result=lyric.substring(0, 150); 55 + lyric=lyric.substring(0, 150);
55 56
56 console.log(songList[randnum].song); 57 console.log(songList[randnum].song);
57 - console.log(result); 58 + console.log(lyric);
58 }); 59 });
59 }) 60 })
...\ No newline at end of file ...\ No newline at end of file
......