신일섭

다른 쇼핑몰에도 동일메서드 적용완료

1 const puppeteer = require('puppeteer'); 1 const puppeteer = require('puppeteer');
2 - 2 +const request = require('request');
3 -//auction_c(); 3 +const fs = require('fs');
4 +const path = require('path');
5 +const HTTPS = require('https');
6 +const domain = "www.chatbotshin.tk"
7 +const sslport = 23023;
8 +const bodyParser = require('body-parser');
9 +
10 +//--------------- Line messanger modules -------------------
11 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
12 +const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
13 +var USER_ID = ' ';
14 +const token = require('../config/key');
15 +const TOKEN = token.token; //사용자 토큰
16 +//----------------------------------------------------------
17 +
18 +// ------------ db calling modules ---------
19 +var dbcontrol = require('../db/dbcontrol');
20 +// -----------------------------------------
4 21
5 // auction Cart Crawling 22 // auction Cart Crawling
6 -async function auction_c(){ 23 +async function auction_c(user_id, a_id, a_pw){
24 +
25 + USER_ID = user_id;
7 26
8 // launching headless browser 27 // launching headless browser
9 const browser = await puppeteer.launch(); 28 const browser = await puppeteer.launch();
10 // making a new page 29 // making a new page
11 const page = await browser.newPage(); 30 const page = await browser.newPage();
12 31
13 - //console input id & password 32 + console.log('유저정보입력완료'); //정상입력 확인용
14 - var args = process.argv;
15 -
16 - var a_id = args[2];
17 - var a_pw = args[3];
18 33
19 - console.log(args[2] + " " + args[3]);
20 34
21 // Gmarket login page 35 // Gmarket login page
22 await page.goto('https://memberssl.auction.co.kr/Authenticate/?url=http%3a%2f%2fwww.auction.co.kr%2f&return_value=0'); 36 await page.goto('https://memberssl.auction.co.kr/Authenticate/?url=http%3a%2f%2fwww.auction.co.kr%2f&return_value=0');
...@@ -26,7 +40,7 @@ async function auction_c(){ ...@@ -26,7 +40,7 @@ async function auction_c(){
26 document.querySelector('#password').value = pwd; 40 document.querySelector('#password').value = pwd;
27 }, a_id, a_pw); 41 }, a_id, a_pw);
28 42
29 - await console.log(a_id,a_pw); 43 + await console.log('로그인중');
30 44
31 // try login 45 // try login
32 await page.click('.btn_login'); 46 await page.click('.btn_login');
...@@ -34,18 +48,21 @@ async function auction_c(){ ...@@ -34,18 +48,21 @@ async function auction_c(){
34 48
35 // goto cart page 49 // goto cart page
36 await page.goto('https://cart.auction.co.kr/ko/cart'); 50 await page.goto('https://cart.auction.co.kr/ko/cart');
37 - //await page.screenshot({ path: 'auction.png', fullPage:true });
38 51
39 // container which will hold crawled data [{},{}...] 52 // container which will hold crawled data [{},{}...]
40 let data = []; 53 let data = [];
41 54
42 // crawling start! (using getOne and getAll function) 55 // crawling start! (using getOne and getAll function)
43 data = await getAll(page); 56 data = await getAll(page);
44 - //data.push(await getAll(page)); // data[0][n]
45 57
46 //logging the result 58 //logging the result
47 for(let index = 0; index < data.length; index++){ 59 for(let index = 0; index < data.length; index++){
48 - console.log(data[index]); 60 +
61 + // 뽑아낸 정보 reply
62 + await replying(data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
63 + // DB에 정보 삽입
64 + await dbcontrol.synchronization(USER_ID, data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
65 + console.log(data[index]);
49 } 66 }
50 67
51 await browser.close(); 68 await browser.close();
...@@ -92,6 +109,41 @@ async function getOne(page, index) { ...@@ -92,6 +109,41 @@ async function getOne(page, index) {
92 return Promise.resolve(data); 109 return Promise.resolve(data);
93 } 110 }
94 111
112 +async function replying(img_link, msg1, msg2, msg3){ //push function
113 + request.post(
114 + {
115 + url: TARGET_URL_2,
116 + headers: {
117 + 'Authorization': `Bearer ${TOKEN}`
118 + },
119 + json: {
120 + "to": `${USER_ID}`,
121 + "messages":[
122 + {
123 + "type":"image",
124 + "originalContentUrl": img_link,
125 + "previewImageUrl": img_link
126 + },
127 + {
128 + "type": "text",
129 + "text": "상품명: " + msg1 // replying message
130 + },
131 + {
132 + "type": "text",
133 + "text": "가격: " + msg2 + " 원" // replying message
134 + },
135 + {
136 + "type": "text",
137 + "text": "링크: " + msg3 // replying message
138 + }
139 +
140 + ]
141 + }
142 + },(error, response, body) => {
143 + //console.log(body)
144 + });
145 +}
146 +
95 module.exports.auction_c = auction_c; 147 module.exports.auction_c = auction_c;
96 module.exports.getOne = getOne; 148 module.exports.getOne = getOne;
97 module.exports.getAll = getAll; 149 module.exports.getAll = getAll;
...\ No newline at end of file ...\ No newline at end of file
......
1 const puppeteer = require('puppeteer'); 1 const puppeteer = require('puppeteer');
2 +const request = require('request');
3 +const fs = require('fs');
4 +const path = require('path');
5 +const HTTPS = require('https');
6 +const domain = "www.chatbotshin.tk"
7 +const sslport = 23023;
8 +const bodyParser = require('body-parser');
9 +
10 +//--------------- Line messanger modules -------------------
11 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
12 +const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
13 +var USER_ID = ' ';
14 +const token = require('../config/key');
15 +const TOKEN = token.token; //사용자 토큰
16 +//----------------------------------------------------------
17 +
18 +// ------------ db calling modules ---------
19 +var dbcontrol = require('../db/dbcontrol');
20 +// -----------------------------------------
2 21
3 -//coupang_c();
4 22
5 // coupang Cart Crawling 23 // coupang Cart Crawling
6 -async function coupang_c(){ 24 +async function coupang_c(user_id, c_id, c_pw){
7 - 25 + USER_ID = user_id;
26 +
8 // launching headless browser 27 // launching headless browser
9 const browser = await puppeteer.launch(); 28 const browser = await puppeteer.launch();
10 // making a new page 29 // making a new page
11 const page = await browser.newPage(); 30 const page = await browser.newPage();
12 31
13 - //console input id & password 32 + console.log('유저정보입력완료'); //정상입력 확인용
14 - var args = process.argv;
15 -
16 -
17 - var c_id = args[2];
18 - var c_pw = args[3];
19 -
20 - console.log(args[2] + " " + args[3]);
21 33
22 34
23 // Gmarket login page 35 // Gmarket login page
...@@ -28,7 +40,7 @@ async function coupang_c(){ ...@@ -28,7 +40,7 @@ async function coupang_c(){
28 document.querySelector('#login-password-input').value = pwd; 40 document.querySelector('#login-password-input').value = pwd;
29 }, c_id, c_pw); 41 }, c_id, c_pw);
30 42
31 - await console.log(c_id,c_pw); 43 + await console.log('로그인중');
32 44
33 // try login 45 // try login
34 await page.click('.login__button'); 46 await page.click('.login__button');
...@@ -36,18 +48,21 @@ async function coupang_c(){ ...@@ -36,18 +48,21 @@ async function coupang_c(){
36 48
37 // goto cart page 49 // goto cart page
38 await page.goto('https://cart.coupang.com/cartView.pang'); 50 await page.goto('https://cart.coupang.com/cartView.pang');
39 - //await page.screenshot({ path: 'coupang.png', fullPage:true });
40 51
41 // container which will hold crawled data [{},{}...] 52 // container which will hold crawled data [{},{}...]
42 let data = []; 53 let data = [];
43 54
44 // crawling start! (using getOne and getAll function) 55 // crawling start! (using getOne and getAll function)
45 data = await getAll(page); 56 data = await getAll(page);
46 - //data.push(await getAll(page)); // data[0][n]
47 57
48 //logging the result 58 //logging the result
49 for(let index = 0; index < data.length; index++){ 59 for(let index = 0; index < data.length; index++){
50 - console.log(data[index]); 60 +
61 + // 뽑아낸 정보 reply
62 + await replying(data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
63 + // DB에 정보 삽입
64 + await dbcontrol.synchronization(USER_ID, data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
65 + console.log(data[index]);
51 } 66 }
52 67
53 await browser.close(); 68 await browser.close();
...@@ -95,6 +110,41 @@ async function getOne(page, index) { ...@@ -95,6 +110,41 @@ async function getOne(page, index) {
95 return Promise.resolve(data); 110 return Promise.resolve(data);
96 } 111 }
97 112
113 +async function replying(img_link, msg1, msg2, msg3){ //push function
114 + request.post(
115 + {
116 + url: TARGET_URL_2,
117 + headers: {
118 + 'Authorization': `Bearer ${TOKEN}`
119 + },
120 + json: {
121 + "to": `${USER_ID}`,
122 + "messages":[
123 + {
124 + "type":"image",
125 + "originalContentUrl": img_link,
126 + "previewImageUrl": img_link
127 + },
128 + {
129 + "type": "text",
130 + "text": "상품명: " + msg1 // replying message
131 + },
132 + {
133 + "type": "text",
134 + "text": "가격: " + msg2 + " 원" // replying message
135 + },
136 + {
137 + "type": "text",
138 + "text": "링크: " + msg3 // replying message
139 + }
140 +
141 + ]
142 + }
143 + },(error, response, body) => {
144 + //console.log(body)
145 + });
146 +}
147 +
98 module.exports.coupang_c = coupang_c; 148 module.exports.coupang_c = coupang_c;
99 module.exports.getOne = getOne; 149 module.exports.getOne = getOne;
100 module.exports.getAll = getAll; 150 module.exports.getAll = getAll;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -30,7 +30,7 @@ async function gmarket_c(user_id, g_id, g_pw){ ...@@ -30,7 +30,7 @@ async function gmarket_c(user_id, g_id, g_pw){
30 // making a new page 30 // making a new page
31 const page = await browser.newPage(); 31 const page = await browser.newPage();
32 32
33 - console.log(g_id + " " + g_pw); //정상입력 확인용, 개인정보 보호를 위해 완성 후 삭제 예정 33 + console.log('유저정보입력완료'); //정상입력 확인용
34 34
35 // Gmarket login page 35 // Gmarket login page
36 await page.goto('https://signinssl.gmarket.co.kr/login/login?url=https://www.gmarket.co.kr/'); 36 await page.goto('https://signinssl.gmarket.co.kr/login/login?url=https://www.gmarket.co.kr/');
...@@ -40,7 +40,7 @@ async function gmarket_c(user_id, g_id, g_pw){ ...@@ -40,7 +40,7 @@ async function gmarket_c(user_id, g_id, g_pw){
40 document.querySelector('#pwd').value = pwd; 40 document.querySelector('#pwd').value = pwd;
41 }, g_id, g_pw); 41 }, g_id, g_pw);
42 42
43 - await console.log(g_id,g_pw); 43 + await console.log('로그인중');
44 44
45 // try login 45 // try login
46 await page.click('.button_login'); 46 await page.click('.button_login');
...@@ -67,8 +67,6 @@ async function gmarket_c(user_id, g_id, g_pw){ ...@@ -67,8 +67,6 @@ async function gmarket_c(user_id, g_id, g_pw){
67 await dbcontrol.synchronization(USER_ID, data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link); 67 await dbcontrol.synchronization(USER_ID, data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
68 console.log(data[index]); 68 console.log(data[index]);
69 } 69 }
70 -
71 - //여기에 db에 추가하는 코드 넣기
72 70
73 await browser.close(); 71 await browser.close();
74 72
......
...@@ -91,7 +91,7 @@ async function viewing(USER_ID){ ...@@ -91,7 +91,7 @@ async function viewing(USER_ID){
91 console.log(datalist); 91 console.log(datalist);
92 92
93 datalist.forEach(function(item, index, array){ 93 datalist.forEach(function(item, index, array){
94 - viewall(array[index].PRD_IMG, array[index].PRD_NAME, array[index].PRD_PRICE, array[index].PRD_LINK); 94 + viewall(USER_ID, array[index].PRD_IMG, array[index].PRD_NAME, array[index].PRD_PRICE, array[index].PRD_LINK);
95 }); 95 });
96 } 96 }
97 catch (err) { // 파일이 없다면 97 catch (err) { // 파일이 없다면
...@@ -156,7 +156,7 @@ async function pushing(USER_ID, sp_message){ //push function ...@@ -156,7 +156,7 @@ async function pushing(USER_ID, sp_message){ //push function
156 } 156 }
157 157
158 //================== 장바구니 목록 전체 출력 =================================== 158 //================== 장바구니 목록 전체 출력 ===================================
159 - async function viewall(img_link, msg1, msg2, msg3){ //push function 159 + async function viewall(USER_ID, img_link, msg1, msg2, msg3){ //push function
160 request.post( 160 request.post(
161 { 161 {
162 url: TARGET_URL_2, 162 url: TARGET_URL_2,
......
...@@ -35,7 +35,7 @@ async function shoppingroute(user_message, user_id, replyToken){ ...@@ -35,7 +35,7 @@ async function shoppingroute(user_message, user_id, replyToken){
35 USER_ID = user_id; 35 USER_ID = user_id;
36 36
37 if(user_message == '/쇼핑' ){ 37 if(user_message == '/쇼핑' ){
38 - replying(replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트 또는 기능을 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n\n기능은 /장바구니조회\n/장바구니삭제\n\n입니다.\n\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.'); 38 + replying(replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트 또는 기능을 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n\n기능은\n/장바구니조회\n/장바구니삭제\n\n입니다.\n\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.');
39 shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음 39 shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음
40 } 40 }
41 41
...@@ -50,9 +50,11 @@ async function shoppingroute(user_message, user_id, replyToken){ ...@@ -50,9 +50,11 @@ async function shoppingroute(user_message, user_id, replyToken){
50 replying(replyToken, 'g마켓을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화'); 50 replying(replyToken, 'g마켓을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화');
51 shopping = 2; shop_select = 'g마켓'; 51 shopping = 2; shop_select = 'g마켓';
52 }else if(user_message == '/옥션'){ 52 }else if(user_message == '/옥션'){
53 - 53 + replying(replyToken, '옥션을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화');
54 + shopping = 2; shop_select = '옥션';
54 }else if(user_message == '/쿠팡'){ 55 }else if(user_message == '/쿠팡'){
55 - 56 + replying(replyToken, '쿠팡을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화');
57 + shopping = 2; shop_select = '쿠팡';
56 }else if(user_message == '/장바구니조회'){ 58 }else if(user_message == '/장바구니조회'){
57 // DB에서 USER_ID.json 을 가진 파일을 읽어들임 59 // DB에서 USER_ID.json 을 가진 파일을 읽어들임
58 replying(replyToken, '장바구니조회를 선택하셨습니다.\nDB를 읽고 있습니다.'); 60 replying(replyToken, '장바구니조회를 선택하셨습니다.\nDB를 읽고 있습니다.');
...@@ -62,13 +64,11 @@ async function shoppingroute(user_message, user_id, replyToken){ ...@@ -62,13 +64,11 @@ async function shoppingroute(user_message, user_id, replyToken){
62 replying(replyToken, '장바구니삭제를 선택하셨습니다.\n삭제할 링크를 입력해주세요.'); 64 replying(replyToken, '장바구니삭제를 선택하셨습니다.\n삭제할 링크를 입력해주세요.');
63 deleting_able = true; // 삭제가능상태 적용 65 deleting_able = true; // 삭제가능상태 적용
64 dbcontrol.deleting(user_message); 66 dbcontrol.deleting(user_message);
65 - } 67 + shopping = 0; // 메인메뉴로 되돌아감
66 - }else{
67 - replying(replyToken, 'Error: 유효하지 않은 선택입니다. 메인으로 되돌아갑니다.');
68 - shopping = 0;
69 } 68 }
70 } 69 }
71 70
71 + // 쇼핑 -> g마켓
72 if(shopping == 2 || shopping == 3){ 72 if(shopping == 2 || shopping == 3){
73 if(shop_select=='g마켓'){ 73 if(shop_select=='g마켓'){
74 if(shopping == 2 && user_message == '/장바구니동기화') 74 if(shopping == 2 && user_message == '/장바구니동기화')
...@@ -90,11 +90,58 @@ async function shoppingroute(user_message, user_id, replyToken){ ...@@ -90,11 +90,58 @@ async function shoppingroute(user_message, user_id, replyToken){
90 shop_select = undefined; //메인메뉴로 돌아가기 위함 90 shop_select = undefined; //메인메뉴로 돌아가기 위함
91 } 91 }
92 } 92 }
93 - } 93 + }
94 } 94 }
95 95
96 + // 쇼핑 -> 옥션
97 + if(shopping == 2 || shopping == 3){
98 + if(shop_select=='옥션'){
99 + if(shopping == 2 && user_message == '/장바구니동기화')
100 + {
101 + replying(replyToken, '====장바구니동기화====\n\n장바구니를 동기화 하기 위해 쇼핑몰에 연결합니다.\n\n아이디와 패스워드를 입력해 주세요:');
102 + method_action = 2;
103 + shopping = 3;
104 + }
105 + else if(shopping == 3 && method_action != 0)
106 + {
107 + replying(replyToken, '===아이디/패스워드 입력중===');
108 + user_info[method_action] = user_message; // 아이디 패스워드를 user_info[2], user_info[1]에 저장
109 + method_action--;
110 + if(method_action==0){
111 + //장바구니 크롤링하는 함수
112 + USER_ID = user_id; // 상대방 ID 획득
113 + await auction.auction_c(USER_ID, user_info[2], user_info[1]);
114 + shopping = 0;
115 + shop_select = undefined; //메인메뉴로 돌아가기 위함
116 + }
117 + }
118 + }
119 + }
96 120
97 - 121 + // 쇼핑 -> 쿠팡
122 + if(shopping == 2 || shopping == 3){
123 + if(shop_select=='쿠팡'){
124 + if(shopping == 2 && user_message == '/장바구니동기화')
125 + {
126 + replying(replyToken, '====장바구니동기화====\n\n장바구니를 동기화 하기 위해 쇼핑몰에 연결합니다.\n\n아이디와 패스워드를 입력해 주세요:');
127 + method_action = 2;
128 + shopping = 3;
129 + }
130 + else if(shopping == 3 && method_action != 0)
131 + {
132 + replying(replyToken, '===아이디/패스워드 입력중===');
133 + user_info[method_action] = user_message; // 아이디 패스워드를 user_info[2], user_info[1]에 저장
134 + method_action--;
135 + if(method_action==0){
136 + //장바구니 크롤링하는 함수
137 + USER_ID = user_id; // 상대방 ID 획득
138 + await coupang.coupang_c(USER_ID, user_info[2], user_info[1]);
139 + shopping = 0;
140 + shop_select = undefined; //메인메뉴로 돌아가기 위함
141 + }
142 + }
143 + }
144 + }
98 } 145 }
99 146
100 // ============================ /취소 입력시 ================================= 147 // ============================ /취소 입력시 =================================
......