신일섭

크롤링 챗봇 연동중_1

var express = require('express');
var express = require('express');
const request = require('request');
//--------------- Line messanger modules -------------------
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
var USER_ID = ' ';
const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
//----------------------------------------------------------
......@@ -20,8 +22,10 @@ var puppeteer = require('puppeteer');
var gmarket = require('./crawling/gmarket');
var coupang = require('./crawling/coupang');
var auction = require('./crawling/auction');
var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete
var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete, 3 = shop method selecting
var shop_select = undefined // shopping-site identifier
var method_action = 0; // typing count;
var user_info = new Array(); // 유저 정보 저장용
//------------------------------------------
const fs = require('fs');
......@@ -40,7 +44,7 @@ app.use(bodyParser.json());
//============== Utility Selector =========================================
app.post('/hook', function (req, res) {
app.post('/hook', async function (req, res) {
//-------- request values ---------
var eventObj = req.body.events[0];
......@@ -48,7 +52,7 @@ app.post('/hook', function (req, res) {
var message = eventObj.message;
//---------------------------------
//---------------- request log------------------------------------------------
//---------------- request log------------------------------------------------ // 개인정보 보호를 위해 완성후에는 삭제될 예정
console.log('======================', new Date() ,'======================');
console.log('[request]', req.body);
console.log('[request source] ', eventObj.source);
......@@ -61,6 +65,8 @@ app.post('/hook', function (req, res) {
shopping = 0;
shop_select = undefined
trans_counter = 0;
method_action = 0;
user_info.length = 0;
}
//---------------------------------------------------------------------
......@@ -79,12 +85,51 @@ app.post('/hook', function (req, res) {
replying(eventObj.replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트를 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.');
shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음
}
if(shopping == 1){
switch(message.text){
case '/g마켓': break;
case '/옥션': break;
case '/쿠팡': break;
default: shopping == 0;
if(message.text == '/g마켓'){
replying(eventObj.replyToken, 'g마켓을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화\n/장바구니조회\n/장바구니추가\n/장바구니삭제');
shopping = 2; shop_select = 'g마켓';
}else if(message.text == '/옥션'){
}else if(message.text == '쿠팡'){
}
}
if(shopping == 2 || shopping == 3){
if(shop_select=='g마켓'){
if(shopping == 2 && message.text == '/장바구니동기화')
{
replying(eventObj.replyToken, '====장바구니동기화====\n\n장바구니를 동기화 하기 위해 쇼핑몰에 연결합니다.\n\n아이디와 패스워드를 입력해 주세요:');
method_action = 2;
shopping = 3;
}
else if(shopping == 3 && method_action != 0)
{
replying(eventObj.replyToken, '===아이디/패스워드 입력중===');
user_info[method_action] = message.text; // 아이디 패스워드를 user_info[2], user_info[1]에 저장
method_action--;
if(method_action==0){
//장바구니 크롤링하는 함수
USER_ID = eventObj.source.userId; // 상대방 ID 획득
await gmarket.gmarket_c(USER_ID, user_info[2], user_info[1]);
// replying(eventObj.replyToken, '리스트를 출력중입니다... 출력전까지 기다려주세요\n\n\n/장바구니동기화\n장바구니조회\n/장바구니추가\n/장바구니삭제');
shopping = 2;
}
}
if(message.text == '/장바구니조회')
{
}
if(message.text == '/장바구니추가')
{
}
if(message.text == '/장바구니삭제')
{
}
}
}
//--------------------------------------------------------------------------------------------------------------------
......
const puppeteer = require('puppeteer');
const readline = require('readline');
const request = require('request');
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "www.chatbotshin.tk"
const sslport = 23023;
const bodyParser = require('body-parser');
//--------------- Line messanger modules -------------------
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
var USER_ID = ' ';
const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
//----------------------------------------------------------
//gmarket_c();
// Gmarket Cart Crawling
async function gmarket_c(){
async function gmarket_c(user_id, g_id, g_pw){
USER_ID = user_id;
// launching headless browser
const browser = await puppeteer.launch();
// making a new page
const page = await browser.newPage();
//console input id & password
var args = process.argv;
var g_id = args[2];
var g_pw = args[3];
console.log(args[2] + " " + args[3]);
console.log(g_id + " " + g_pw); //정상입력 확인용, 개인정보 보호를 위해 완성 후 삭제 예정
// Gmarket login page
await page.goto('https://signinssl.gmarket.co.kr/login/login?url=https://www.gmarket.co.kr/');
......@@ -35,17 +44,20 @@ async function gmarket_c(){
// goto cart page
await page.goto('https://cart.gmarket.co.kr/ko/cart');
//await page.screenshot({ path: 'gmarket.png', fullPage:true });
// container which will hold crawled data [{},{}...]
let data = [];
// crawling start! (using getOne and getAll function)
data = await getAll(page);
//data.push(await getAll(page)); // data[0][n]
//logging the result
for(let index = 0; index < data.length; index++){
await replying(data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
console.log(data[index]);
}
......@@ -65,7 +77,6 @@ async function gmarket_c(){
for (let index = 0; index < number; index++) {
data.push(await getOne(page, index + 1));
// pushing to the array
}
return Promise.resolve(data);
......@@ -93,6 +104,86 @@ async function getOne(page, index) {
return Promise.resolve(data);
}
async function replying(img_link, msg1, msg2, msg3){ //push function
request.post(
{
url: TARGET_URL_2,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"to": `${USER_ID}`,
"messages":[
{
"type":"image",
"originalContentUrl": img_link,
"previewImageUrl": img_link
},
{
"type": "text",
"text": "상품명: " + msg1 // replying message
},
{
"type": "text",
"text": "가격: " + msg2 + " 원" // replying message
},
{
"type": "text",
"text": "링크: " + msg3 // replying message
}
]
}
},(error, response, body) => {
//console.log(body)
});
}
function replying2(replyToken, sp_message){ // sp_message is message(string) that depends on the situation that user selects
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":sp_message // replying message
}
]
}
},(error, response, body) => {
//console.log(body)
});
}
/*function replying_img(replyToken, img_link){
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"image",
"originalContentUrl": img_link,
"previewImageUrl": img_link
}
]
}
},(error, response, body) => {
console.log(body)
});
}*/
module.exports.gmarket_c = gmarket_c;
module.exports.getOne = getOne;
module.exports.getAll = getAll;
\ No newline at end of file
module.exports.getAll = getAll;
module.exports.replying = replying;
//module.exports.replying_img = replying_img;
\ No newline at end of file
......