김연수

Merge branch 'ocr' into 'master'

Ocr

Change the category of help function from example to help

See merge request !3
......@@ -69,7 +69,7 @@ exports.run = async (client, msg, args, prefix) => {
exports.config = {
name: '도움말',
aliases: ['도움', '명령어', 'commands', 'help'],
category: ['example'],
category: ['Help'],
des: ['봇에 대한 명령어 리스트들을 불러옵니다.'],
use: ['!도움말 <명령어>']
};
\ No newline at end of file
......
......@@ -75,14 +75,11 @@ exports.run = async (client, msg, args, prefix) => {
}
};
function kor(attachment) {
}
exports.config = {
name: 'ocr',
aliases: ['ㅐㅊㄱ'],
category: ['ocr'],
category: ['Ocr'],
des: ['이미지를 첨부하고 명령어를 입력하면, 이미지에 있는 텍스트를 추출합니다.'],
use: ['!ocr <사용 언어>']
};
\ No newline at end of file
......
......@@ -12,13 +12,13 @@ exports.run = async (client, msg, args, prefix) => {
const puppeteer = require('puppeteer');
const withBrowser = async (fn) => {
const browser = await puppeteer.launch({headless:true});
const browser = await puppeteer.launch({ headless: true });
try {
return await fn(browser);
} finally {
await browser.close();
}
}
};
const withPage = (browser) => async (fn) => {
const page = await browser.newPage();
......@@ -27,7 +27,7 @@ exports.run = async (client, msg, args, prefix) => {
} finally {
await page.close();
}
}
};
const urls = ['http://www.bestpen.kr/shop/shopbrand.html?&search=', 'http://www.pencafe.co.kr/shop/shopbrand.html?&search=', 'https://blueblack.co.kr/product/search.html?&keyword=', 'http://japan9.co.kr/shop/shopbrand.html?&search='];
const results = await withBrowser(async (browser) => {
......@@ -35,11 +35,11 @@ exports.run = async (client, msg, args, prefix) => {
return withPage(browser)(async (page) => {
// 각 사이트별 제품 검색
var result = '';
site : switch(urls.indexOf(url)) {
site: switch (urls.indexOf(url)) {
case 0:
// 베스트펜
try {
var bestpen_url = url + search + "&sort=price"
var bestpen_url = url + search + "&sort=price";
await page.goto(bestpen_url);
// console.log("bestpen crawling");
......@@ -48,14 +48,14 @@ exports.run = async (client, msg, args, prefix) => {
} catch { ; } // 사이트 링크에 이상이 생겼거나 검색에 문제가 생겼을 경우 프로그램이 종료되는 것을 방지
// 검색 결과 가져오기 (최대 4개)
searchLoop : for (var i = 1; i <= 4; i++) {
searchLoop: for (var i = 1; i <= 4; i++) {
try {
var title = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('+i+') > dd > ul > li.prd-name > a', element => {
var title = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(' + i + ') > dd > ul > li.prd-name > a', element => {
return element.textContent;
});
// console.log("베스트펜 검색 결과 제품명 : ", title);
var link = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('+i+') > dt > a', element => {
var link = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(' + i + ') > dt > a', element => {
return element.href;
});
// console.log("베스트펜 검색 링크 : ", link);
......@@ -65,7 +65,7 @@ exports.run = async (client, msg, args, prefix) => {
link = link.slice(0, link.indexOf('&search'));
try {
var price = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('+i+') > dd > ul > li.prd-price > p:nth-child(2) > span.price', element => {
var price = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(' + i + ') > dd > ul > li.prd-price > p:nth-child(2) > span.price', element => {
return element.textContent;
});
// console.log("베스트펜 검색 결과 가격 : ", price);
......@@ -90,7 +90,7 @@ exports.run = async (client, msg, args, prefix) => {
case 1:
// 펜카페
try {
var pencafe_url = url + search + "&sort=price"
var pencafe_url = url + search + "&sort=price";
await page.goto(pencafe_url);
// console.log("pencafe crawling");
......@@ -99,19 +99,19 @@ exports.run = async (client, msg, args, prefix) => {
} catch { ; } // 사이트 링크에 이상이 생겼거나 검색에 문제가 생겼을 경우 프로그램이 종료되는 것을 방지
// 검색 결과 가져오기 (최대 4개)
searchLoop : for (var i = 1; i <= 4; i++) {
searchLoop: for (var i = 1; i <= 4; i++) {
try {
var title = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('+i+') > dd > ul > li.prd-name > a', element => {
var title = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(' + i + ') > dd > ul > li.prd-name > a', element => {
return element.textContent;
});
// console.log("펜카페 검색 결과 제품명 : ", title);
// 펜카페 구조상 제품명 앞에 할인률(ex. 17%)이 붙는 경우가 많음 -> 제거
if (title.indexOf('%') != -1) { title = title.slice(title.indexOf('%')+1); }
if (title.indexOf('%') != -1) { title = title.slice(title.indexOf('%') + 1); }
// 펜카페 구조상 제품명 뒤에 사족(ex. (색상선택/금장~~~))이 붙는 경우가 많음 -> 제거
if (title.lastIndexOf(')') == title.length-1) { title = title.slice(0, title.lastIndexOf('(')); }
if (title.lastIndexOf(')') == title.length - 1) { title = title.slice(0, title.lastIndexOf('(')); }
var link = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('+i+') > dd > ul > li.prd-name > a', element => {
var link = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(' + i + ') > dd > ul > li.prd-name > a', element => {
return element.href;
});
// console.log("펜카페 검색 링크 : ", link);
......@@ -121,7 +121,7 @@ exports.run = async (client, msg, args, prefix) => {
link = link.slice(0, link.indexOf('&search'));
try {
var price = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('+i+') > dd > ul > li.prd-price > span', element => {
var price = await page.$eval('#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(' + i + ') > dd > ul > li.prd-price > span', element => {
return element.textContent;
});
// console.log("펜카페 검색 결과 가격 : ", price);
......@@ -146,7 +146,7 @@ exports.run = async (client, msg, args, prefix) => {
case 2:
// 블루블랙
try {
var blueblack_url = url + search + "&order_by=priceasc"
var blueblack_url = url + search + "&order_by=priceasc";
await page.goto(blueblack_url);
// console.log("blublack crawling");
......@@ -155,14 +155,14 @@ exports.run = async (client, msg, args, prefix) => {
} catch { ; } // 사이트 링크에 이상이 생겼거나 검색에 문제가 생겼을 경우 프로그램이 종료되는 것을 방지
// 검색 결과 가져오기 (최대 4개)
searchLoop : for (var i = 1; i <= 4; i++) {
searchLoop: for (var i = 1; i <= 4; i++) {
try {
var title = await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child('+i+') > div.description > p.name > a > span:nth-child(2)', element => {
var title = await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child(' + i + ') > div.description > p.name > a > span:nth-child(2)', element => {
return element.textContent;
});
// console.log("블루블랙 검색 결과 제품명 : ", title);
var link = await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child('+i+') > div.description > p.name > a', element => {
var link = await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child(' + i + ') > div.description > p.name > a', element => {
return element.href;
});
// console.log("블루블랙 검색 링크 : ", link);
......@@ -173,7 +173,7 @@ exports.run = async (client, msg, args, prefix) => {
try {
// 품절 아이콘이 있는지 확인
await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child('+i+') > div.description > div.status > div > img', element => {
await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child(' + i + ') > div.description > div.status > div > img', element => {
return element.getAttribute("src");
});
......@@ -181,7 +181,7 @@ exports.run = async (client, msg, args, prefix) => {
} catch {
try {
// 품절 아이콘이 없을 경우
var price = await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child('+i+') > div.description > ul > li:nth-child(3) > span:nth-child(2)', element => {
var price = await page.$eval('#contents > div:nth-child(4) > ul > li:nth-child(' + i + ') > div.description > ul > li:nth-child(3) > span:nth-child(2)', element => {
return element.textContent;
});
} catch {
......@@ -209,7 +209,7 @@ exports.run = async (client, msg, args, prefix) => {
await page.goto(url);
// console.log("japannine crawling");
var japannine_url = url + search + "&sort=price"
var japannine_url = url + search + "&sort=price";
await page.goto(japannine_url);
// console.log("japannine crawling");
......@@ -218,9 +218,9 @@ exports.run = async (client, msg, args, prefix) => {
} catch { ; } // 사이트 링크에 이상이 생겼거나 검색에 문제가 생겼을 경우 프로그램이 종료되는 것을 방지
// 검색 결과 가져오기 (최대 4개)
searchLoop : for (var i = 1; i <= 4; i++) {
searchLoop: for (var i = 1; i <= 4; i++) {
try {
var title = await page.$eval('#mk_search_production > tbody > tr:nth-child('+(5+((i-1)*4))+') > td:nth-child(3) > a', element => {
var title = await page.$eval('#mk_search_production > tbody > tr:nth-child(' + (5 + ((i - 1) * 4)) + ') > td:nth-child(3) > a', element => {
return element.textContent;
});
// console.log("재팬나인 검색 결과 제품명 : ", title);
......@@ -228,12 +228,12 @@ exports.run = async (client, msg, args, prefix) => {
// 재팬나인 구조상 제품명 앞에 항상 '/n'이 붙는다 -> 제거
title = title.slice(1);
// 재팬나인 구조상 제품명 앞과 뒤에 [쿠폰적용 || 주문예약상품]이 붙는 경우가 많음 -> 제거
if (title.indexOf('[') == 0) { title = title.slice(title.indexOf(']')+1); }
if (title.lastIndexOf(']') == title.length-1) { title = title.slice(0, title.lastIndexOf('[')); }
if (title.indexOf('{') == 0) { title = title.slice(title.indexOf('}')+1); }
if (title.lastIndexOf('}') == title.length-1) { title = title.slice(0, title.lastIndexOf('{')); }
if (title.indexOf('[') == 0) { title = title.slice(title.indexOf(']') + 1); }
if (title.lastIndexOf(']') == title.length - 1) { title = title.slice(0, title.lastIndexOf('[')); }
if (title.indexOf('{') == 0) { title = title.slice(title.indexOf('}') + 1); }
if (title.lastIndexOf('}') == title.length - 1) { title = title.slice(0, title.lastIndexOf('{')); }
var link = await page.$eval('#mk_search_production > tbody > tr:nth-child('+(5+((i-1)*4))+') > td:nth-child(3) > a', element => {
var link = await page.$eval('#mk_search_production > tbody > tr:nth-child(' + (5 + ((i - 1) * 4)) + ') > td:nth-child(3) > a', element => {
return element.href;
});
// console.log("재팬나인 검색 링크 : ", link);
......@@ -244,19 +244,19 @@ exports.run = async (client, msg, args, prefix) => {
try {
// 품절 아이콘이 있는지 확인
var img_src = await page.$eval('#mk_search_production > tbody > tr:nth-child('+(5+((i-1)*4))+') > td:nth-child(6) > img', element => {
var img_src = await page.$eval('#mk_search_production > tbody > tr:nth-child(' + (5 + ((i - 1) * 4)) + ') > td:nth-child(6) > img', element => {
return element.getAttribute("src");
});
if (img_src.indexOf('no_amount0') != -1) { var price = 'SOLD OUT'; }
} catch {
// 품절 아이콘이 없을 경우
var price = await page.$eval('#mk_search_production > tbody > tr:nth-child('+(5+((i-1)*4))+') > td.brandprice > span', element => {
var price = await page.$eval('#mk_search_production > tbody > tr:nth-child(' + (5 + ((i - 1) * 4)) + ') > td.brandprice > span', element => {
return element.textContent;
});
// 재팬나인 구조상 금액 뒤에 (옵션에 따라 변동)이 붙는 경우가 있다 -> 제거
if (price.indexOf('(') != -1) { price = price.slice(0, price.indexOf('원')+1); }
if (price.indexOf('(') != -1) { price = price.slice(0, price.indexOf('원') + 1); }
}
// console.log("재팬나인 검색 결과 가격 : ", price);
......@@ -277,11 +277,12 @@ exports.run = async (client, msg, args, prefix) => {
return result;
});
}))
}));
});
// 검색 결과 챗봇에 출력
let Commands = new Discord.MessageEmbed()
.setTitle(`${search}에 대한 검색 결과`)
.setColor("E5D49A")
// 베스트펜 검색 결과 (ex. 제품명(링크) - 금액)
......@@ -291,7 +292,8 @@ exports.run = async (client, msg, args, prefix) => {
// 블루블랙 검색 결과
.addField('블루블랙', `${results[2].slice(0, 1023)}`)
// 재팬나인 검색 결과
.addField('재팬나인', `${results[3].slice(0, 1023)}`)
.addField('재팬나인', `${results[3].slice(0, 1023)}`);
msg.reply({ embeds: [Commands] });
} else {
msg.reply("검색어가 없습니다. 검색어를 추가해서 다시 입력해주세요.");
......@@ -300,8 +302,9 @@ exports.run = async (client, msg, args, prefix) => {
exports.config = {
name: '문구',
aliases: ['문구류', '가격비교', 'stationery', 'search'],
category: ['stationery'],
category: ['Stationery'],
des: ['채팅 내용에 대한 검색결과를 보여줍니다.'],
use: ['!문구 <채팅>']
};
\ No newline at end of file
......
......@@ -11,7 +11,7 @@ client.once('ready', () => {
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.category = ['example', 'stationery', 'translate', 'ocr'];
client.category = ['Help', 'Stationery', 'Translate', 'Ocr'];
fs.readdirSync("./Commands/").forEach(dir => {
const Filter = fs.readdirSync(`./Commands/${dir}`).filter(f => f.endsWith(".js"));
......