은승우

ocr com?

Showing 1 changed file with 53 additions and 2 deletions
......@@ -80,8 +80,59 @@ function handleEvent(event) {
}
else if (event.type == 'message'&& event.message.type == 'image') {
return new Promise((resolve,reject)=>{
var upload = multer({ dest: 'photo/' })
upload.single(event.message.image);
var cheerio = require('cheerio');
let subscriptionKey = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'];
let endpoint = process.env['COMPUTER_VISION_ENDPOINT'];
var uriBase = endpoint + 'vision/v2.1/ocr';
var options = {
uri: uriBase,
qs: {
visualFeatures: 'Categories',
details: '',
language: 'en'
},
headers: {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscriptionKey
},
body: fs.readFileSync(event.message.image)
};
request.post(options, function (error, response, body) {
var data=JSON.stringify(body);
var text='';
while(data.indexOf('text\\')!=-1)
{
data=data.substring(data.indexOf('text\\')+9);
text+=data.substring(0,data.indexOf("\\"))+" ";
}
console.log(text);
text="나의모든날들";
console.log(text);
var url="https://www.genie.co.kr/search/searchLyrics?query="+ encodeURI(text);
request(url, function(error, response, html){
var $ = cheerio.load(html);
const $bodyList= $('#body-content > div.search_lyrics > div.music-list-wrap.type-lyrics > table > tbody > tr');
var songList=[];
$bodyList.each(function(i, elem){
if(i<20){
songList.push({
singer: $(this).find("td.info").find("a.artist.ellipsis").text().trim(),
song: $(this).find("td.info").find("a.title.ellipsis").text().trim(),
});
}
})
var result='';
for(var i=0;i<songList.length;i++){
if(songList[i].singer!=''){
result+=songList[i].singer+", "+songList[i].song+"\n";
}
}
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
});
});
})
}
......