Showing
4 changed files
with
169 additions
and
18 deletions
... | @@ -15,12 +15,10 @@ const token = require('../config/key'); | ... | @@ -15,12 +15,10 @@ const token = require('../config/key'); |
15 | const TOKEN = token.token; //사용자 토큰 | 15 | const TOKEN = token.token; //사용자 토큰 |
16 | //---------------------------------------------------------- | 16 | //---------------------------------------------------------- |
17 | 17 | ||
18 | -//----------------low db----------------------------------- | 18 | +// ------------ db calling modules --------- |
19 | -const low = require('lowdb'); | 19 | +var dbcontrol = require('../db/dbcontrol'); |
20 | -const FileSync = require('lowdb/adapters/FileSync') | 20 | +// ----------------------------------------- |
21 | -const adapter = new FileSync('db.json'); | 21 | + |
22 | -const db = low(adapter); | ||
23 | -//--------------------------------------------------------- | ||
24 | 22 | ||
25 | // Gmarket Cart Crawling | 23 | // Gmarket Cart Crawling |
26 | async function gmarket_c(user_id, g_id, g_pw){ | 24 | async function gmarket_c(user_id, g_id, g_pw){ |
... | @@ -66,6 +64,8 @@ async function gmarket_c(user_id, g_id, g_pw){ | ... | @@ -66,6 +64,8 @@ async function gmarket_c(user_id, g_id, g_pw){ |
66 | console.log(data[index]); | 64 | console.log(data[index]); |
67 | } | 65 | } |
68 | 66 | ||
67 | + //여기에 db에 추가하는 코드 넣기 | ||
68 | + | ||
69 | await browser.close(); | 69 | await browser.close(); |
70 | 70 | ||
71 | 71 | ... | ... |
db/dbcontrol.js
0 → 100644
1 | +//----------------low db----------------------------------- | ||
2 | +const request = require('request'); | ||
3 | + | ||
4 | +const low = require('lowdb'); | ||
5 | +const FileSync = require('lowdb/adapters/FileSync') | ||
6 | +const fs = require('fs'); | ||
7 | + | ||
8 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
9 | +const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push' | ||
10 | +const token = require('../config/key'); | ||
11 | +const TOKEN = token.token; // 사용자 토큰 | ||
12 | +//var USER_ID = ' '; // LINE 메세지 유저 아이디 | ||
13 | + | ||
14 | +//--------------------------------------------------------- | ||
15 | + | ||
16 | +// db 초기화 | ||
17 | +function start_db(USER_ID){ | ||
18 | + | ||
19 | +const adapter = new FileSync(USER_ID + '.json'); | ||
20 | +const db = low(adapter); | ||
21 | +db.defaults({cart : []}).write(); | ||
22 | + | ||
23 | +} | ||
24 | + | ||
25 | +// 장바구니 동기화 | ||
26 | +function synchronization(USER_ID, prd_img, prd_name, prd_price, prd_link){ | ||
27 | + | ||
28 | + const adapter = new FileSync(USER_ID + '.json'); | ||
29 | + const db = low(adapter); | ||
30 | + db.defaults({cart : []}).write(); | ||
31 | + | ||
32 | + // 중복 있는지 검사하는 코드를 if구문에 넣기 (prd_link 기반으로) | ||
33 | + // 입력받은 정보를 db에 저장 | ||
34 | + db.get('cart') | ||
35 | + .push({ | ||
36 | + PRD_IMG : prd_img, | ||
37 | + PRD_NAME : prd_name, | ||
38 | + PRD_PRICE : prd_price, | ||
39 | + PRD_LINK : prd_link | ||
40 | + }).write(); | ||
41 | + | ||
42 | +} | ||
43 | + | ||
44 | +// db에서 목록 검색 후 삭제 | ||
45 | +function deleting(USER_ID, prd_link){ | ||
46 | + | ||
47 | + const adapter = new FileSync(USER_ID + '.json'); | ||
48 | + const db = low(adapter); | ||
49 | + db.defaults({cart : []}).write(); | ||
50 | + | ||
51 | + db.get('cart') | ||
52 | + .remove({PRD_LINK : prd_link}) | ||
53 | + .write(); // prd_link 참조 삭제 | ||
54 | + | ||
55 | +} | ||
56 | + | ||
57 | +// db에서 USER_ID 기반 장바구니 조회 | ||
58 | +function viewing(USER_ID){ | ||
59 | + | ||
60 | + const adapter = new FileSync(USER_ID + '.json'); | ||
61 | + const db = low(adapter); | ||
62 | + db.defaults({cart : []}).write(); | ||
63 | + | ||
64 | + var datalist = new Array(); | ||
65 | + | ||
66 | + datalist = db.get('cart').value(); // [ { } ] 배열속 객체형태로 리턴 | ||
67 | + //미완성 - 메세지로 내역 push하는 기능 업데이트 요망 | ||
68 | + | ||
69 | +} | ||
70 | + | ||
71 | + | ||
72 | + | ||
73 | + | ||
74 | +//================== Just replying specific messages needed =================================== | ||
75 | +async function replying(replyToken, sp_message){ // sp_message is message(string) that depends on the situation that user selects | ||
76 | + request.post( | ||
77 | + { | ||
78 | + url: TARGET_URL, | ||
79 | + headers: { | ||
80 | + 'Authorization': `Bearer ${TOKEN}` | ||
81 | + }, | ||
82 | + json: { | ||
83 | + "replyToken":replyToken, | ||
84 | + "messages":[ | ||
85 | + { | ||
86 | + "type":"text", | ||
87 | + "text":sp_message // replying message | ||
88 | + } | ||
89 | + ] | ||
90 | + } | ||
91 | + },(error, response, body) => { | ||
92 | + // console.log(body) | ||
93 | + }); | ||
94 | +} | ||
95 | + | ||
96 | +//================== Just pushing specific messages needed =================================== | ||
97 | +async function pushing(USER_ID, sp_message){ //push function | ||
98 | + request.post( | ||
99 | + { | ||
100 | + url: TARGET_URL_2, | ||
101 | + headers: { | ||
102 | + 'Authorization': `Bearer ${TOKEN}` | ||
103 | + }, | ||
104 | + json: { | ||
105 | + "to": `${USER_ID}`, | ||
106 | + "messages":[ | ||
107 | + { | ||
108 | + "type": "text", | ||
109 | + "text": sp_message // replying message | ||
110 | + } | ||
111 | + ] | ||
112 | + } | ||
113 | + },(error, response, body) => { | ||
114 | + //console.log(body) | ||
115 | + }); | ||
116 | + } | ||
117 | + | ||
118 | + //================== 장바구니 목록 전체 출력 =================================== | ||
119 | + async function viewall(img_link, msg1, msg2, msg3){ //push function | ||
120 | + request.post( | ||
121 | + { | ||
122 | + url: TARGET_URL_2, | ||
123 | + headers: { | ||
124 | + 'Authorization': `Bearer ${TOKEN}` | ||
125 | + }, | ||
126 | + json: { | ||
127 | + "to": `${USER_ID}`, | ||
128 | + "messages":[ | ||
129 | + { | ||
130 | + "type":"image", | ||
131 | + "originalContentUrl": img_link, | ||
132 | + "previewImageUrl": img_link | ||
133 | + }, | ||
134 | + { | ||
135 | + "type": "text", | ||
136 | + "text": "상품명: " + msg1 // replying message | ||
137 | + }, | ||
138 | + { | ||
139 | + "type": "text", | ||
140 | + "text": "가격: " + msg2 + " 원" // replying message | ||
141 | + }, | ||
142 | + { | ||
143 | + "type": "text", | ||
144 | + "text": "링크: " + msg3 // replying message | ||
145 | + } | ||
146 | + | ||
147 | + ] | ||
148 | + } | ||
149 | + },(error, response, body) => { | ||
150 | + //console.log(body) | ||
151 | + }); | ||
152 | + } | ||
153 | + | ||
154 | +module.exports.start_db = start_db; | ||
155 | +module.exports.synchronization = synchronization; | ||
156 | +module.exports.deleting = deleting; | ||
157 | +module.exports.viewing = viewing; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
dbcontrol.js
deleted
100644 → 0
1 | -//----------------low db----------------------------------- | ||
2 | -const low = require('lowdb'); | ||
3 | -const FileSync = require('lowdb/adapters/FileSync') | ||
4 | -const adapter = new FileSync('db.json'); | ||
5 | -const db = low(adapter); | ||
6 | -//--------------------------------------------------------- | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -24,6 +24,10 @@ var method_action = 0; // typing count; | ... | @@ -24,6 +24,10 @@ var method_action = 0; // typing count; |
24 | var user_info = new Array(); // 유저 정보 저장용 | 24 | var user_info = new Array(); // 유저 정보 저장용 |
25 | //------------------------------------------ | 25 | //------------------------------------------ |
26 | 26 | ||
27 | +// ------------ db calling modules --------- | ||
28 | +var dbcontrol = require('../db/dbcontrol'); | ||
29 | +// ----------------------------------------- | ||
30 | + | ||
27 | // ========================================= 쇼핑몰 연결 중계 ========================================= | 31 | // ========================================= 쇼핑몰 연결 중계 ========================================= |
28 | async function shoppingroute(user_message, user_id, replyToken){ | 32 | async function shoppingroute(user_message, user_id, replyToken){ |
29 | 33 | ||
... | @@ -66,18 +70,14 @@ async function shoppingroute(user_message, user_id, replyToken){ | ... | @@ -66,18 +70,14 @@ async function shoppingroute(user_message, user_id, replyToken){ |
66 | shopping = 2; | 70 | shopping = 2; |
67 | } | 71 | } |
68 | } | 72 | } |
69 | - if(user_message == '/장바구니조회') | 73 | + /* if(user_message == '/장바구니조회') |
70 | - { | ||
71 | - | ||
72 | - } | ||
73 | - if(user_message == '/장바구니추가') | ||
74 | { | 74 | { |
75 | 75 | ||
76 | } | 76 | } |
77 | if(user_message == '/장바구니삭제') | 77 | if(user_message == '/장바구니삭제') |
78 | { | 78 | { |
79 | 79 | ||
80 | - } | 80 | + }*/ //이 기능을 마켓선택기능에 넣는것이 좋을거같음 (db를 공유하기때문에) |
81 | } | 81 | } |
82 | } | 82 | } |
83 | 83 | ... | ... |
-
Please register or login to post a comment