Showing
4 changed files
with
131 additions
and
84 deletions
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | -var path = require('path'); | 2 | +const request = require('request'); |
3 | -var favicon = require('serve-favicon'); | 3 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
4 | -//var logger = require('morgan'); | 4 | +const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU=' |
5 | -var cookieParser = require('cookie-parser'); | 5 | +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' |
6 | -var bodyParser = require('body-parser'); | 6 | +const PAPAGO_ID = 'r_kuebFKCqBeL3SI_dFb' |
7 | -var mysql = require('mysql'); | 7 | +const PAPAGO_SECRET = 'Wdz2tudrzB' |
8 | -var request = require('request'); | 8 | +const fs = require('fs'); |
9 | -//var index = require('./routes/index'); | 9 | +const path = require('path'); |
10 | -//var users = require('./routes/users'); | 10 | +const HTTPS = require('https'); |
11 | -//var mytoons = require('./routes/mytoons'); | 11 | +const domain = "www.chatbotshin.tk" |
12 | -//var yourtoons=require('./routes/yourtoons'); | 12 | +const sslport = 23023; |
13 | -var passport = require('passport'); | 13 | +const bodyParser = require('body-parser'); |
14 | -//var setting = require('./routes/setting'); | 14 | + |
15 | -var session = require('express-session'); | 15 | +// target language verification |
16 | -//port | 16 | +var trans_dest = undefined; |
17 | -/*passport.serializeUser(function(user, done) { | 17 | + |
18 | - console.log('serialized'); | ||
19 | - done(null, user); | ||
20 | -}); | ||
21 | -passport.deserializeUser(function(user, done) { | ||
22 | - console.log('deserialized'); | ||
23 | - done(null, user); | ||
24 | -}); | ||
25 | -*/ | ||
26 | var app = express(); | 18 | var app = express(); |
27 | -// view engine setup | ||
28 | -app.set('views', path.join(__dirname, 'views')); | ||
29 | -app.set('view engine', 'ejs'); | ||
30 | -// uncomment after placing your favicon in /public | ||
31 | -//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | ||
32 | -app.use(logger('dev')); | ||
33 | app.use(bodyParser.json()); | 19 | app.use(bodyParser.json()); |
34 | -app.use(bodyParser.urlencoded({ extended: false })); | 20 | +app.post('/hook', function (req, res) { |
35 | -app.use(cookieParser()); | 21 | + |
36 | -app.use(express.static(path.join(__dirname, 'public'))); | 22 | + var eventObj = req.body.events[0]; |
37 | -connection = mysql.createConnection({ | 23 | + var source = eventObj.source; |
38 | - host : 'localhost', | 24 | + var message = eventObj.message; |
39 | - user : 'root', | 25 | + |
40 | - password : 'password', | 26 | + // request log |
41 | - port : 3306, | 27 | + console.log('======================', new Date() ,'======================'); |
42 | - database : 'Shoppingcart' | 28 | + console.log('[request]', req.body); |
43 | -}); | 29 | + console.log('[request source] ', eventObj.source); |
44 | -app.use(session({ | 30 | + console.log('[request message]', eventObj.message); |
45 | - secret: 'secrettexthere', | 31 | + |
46 | - saveUninitialized: true, | 32 | + // language identification & translation |
47 | - resave: true | 33 | + if(eventObj.message.text=='영어' || eventObj.message.text=='프랑스어' || eventObj.message.text=='일본어'){ |
48 | -})); | 34 | + trans_change(eventObj.message.text); |
49 | -app.use(passport.initialize()); | 35 | + console.log('[trans_dest]', trans_dest); |
50 | -app.use(passport.session()); | 36 | + } |
51 | -app.use('/', index); | 37 | + else if(trans_dest != undefined){ |
52 | -app.use('/users', users); | 38 | + trans(eventObj.replyToken, eventObj.message.text); |
53 | -app.use('/mytoons', mytoons); | 39 | + } |
54 | -app.use('/setting', setting); | 40 | + |
55 | -app.use('/yourtoons', yourtoons); | 41 | + res.sendStatus(200); |
56 | -//app.use(express.static('views')); | ||
57 | -// catch 404 and forward to error handler | ||
58 | -app.use(function(req, res, next) { | ||
59 | - var err = new Error('Not Found'); | ||
60 | - err.status = 404; | ||
61 | - next(err); | ||
62 | -}); | ||
63 | -// error handler | ||
64 | -app.use(function(err, req, res, next) { | ||
65 | - // set locals, only providing error in development | ||
66 | - res.locals.message = err.message; | ||
67 | - res.locals.error = req.app.get('env') === 'development' ? err : {}; | ||
68 | - // render the error page | ||
69 | - res.status(err.status || 500); | ||
70 | - res.render('error'); | ||
71 | }); | 42 | }); |
72 | -var server = app.listen(3000); | ||
73 | -module.exports = app; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
43 | + | ||
44 | +function trans(replyToken, message) { | ||
45 | + | ||
46 | + request.post( | ||
47 | + { | ||
48 | + url: PAPAGO_URL, | ||
49 | + headers: { | ||
50 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
51 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
52 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
53 | + }, | ||
54 | + body: 'source=ko&' + trans_dest + '&text=' + message, | ||
55 | + json:true | ||
56 | + },(error, response, body) => { | ||
57 | + if(!error && response.statusCode == 200) { | ||
58 | + console.log(body.message); | ||
59 | + var transMessage = body.message.result.translatedText; | ||
60 | + request.post( | ||
61 | + { | ||
62 | + url: TARGET_URL, | ||
63 | + headers: { | ||
64 | + 'Authorization': `Bearer ${TOKEN}` | ||
65 | + }, | ||
66 | + json: { | ||
67 | + "replyToken":replyToken, | ||
68 | + "messages":[ | ||
69 | + { | ||
70 | + "type":"text", | ||
71 | + "text":transMessage | ||
72 | + } | ||
73 | + ] | ||
74 | + } | ||
75 | + },(error, response, body) => { | ||
76 | + console.log(body) | ||
77 | + }); | ||
78 | + } | ||
79 | + }); | ||
80 | + | ||
81 | +} | ||
82 | + | ||
83 | +// language index identification | ||
84 | +function trans_change(index){ | ||
85 | + if(index == '영어'){ | ||
86 | + trans_dest = 'target=en'; | ||
87 | + } | ||
88 | + else if(index == '일본어'){ | ||
89 | + trans_dest = 'target=ja'; | ||
90 | + } | ||
91 | + else if(index == '프랑스어'){ | ||
92 | + trans_dest = 'target=fr'; | ||
93 | + } | ||
94 | +} | ||
95 | + | ||
96 | +try { | ||
97 | + const option = { | ||
98 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
99 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
100 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
101 | + }; | ||
102 | + | ||
103 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
104 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
105 | + }); | ||
106 | + } catch (error) { | ||
107 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
108 | + console.log(error); | ||
109 | + } | ||
110 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | const puppeteer = require('puppeteer'); | 1 | const puppeteer = require('puppeteer'); |
2 | -const readline = require('readline'); | ||
3 | 2 | ||
3 | +//auction_c(); | ||
4 | 4 | ||
5 | -// Gmarket Cart Crawling | 5 | +// auction Cart Crawling |
6 | -(async () => { | 6 | +async function auction_c(){ |
7 | 7 | ||
8 | // launching headless browser | 8 | // launching headless browser |
9 | const browser = await puppeteer.launch(); | 9 | const browser = await puppeteer.launch(); |
... | @@ -29,13 +29,12 @@ const readline = require('readline'); | ... | @@ -29,13 +29,12 @@ const readline = require('readline'); |
29 | await console.log(a_id,a_pw); | 29 | await console.log(a_id,a_pw); |
30 | 30 | ||
31 | // try login | 31 | // try login |
32 | - // await page.keyboard.press(String.fromCharCode(13)); | ||
33 | await page.click('.btn_login'); | 32 | await page.click('.btn_login'); |
34 | await page.waitForNavigation(); | 33 | await page.waitForNavigation(); |
35 | 34 | ||
36 | // goto cart page | 35 | // goto cart page |
37 | await page.goto('https://cart.auction.co.kr/ko/cart'); | 36 | await page.goto('https://cart.auction.co.kr/ko/cart'); |
38 | - await page.screenshot({ path: 'auction.png', fullPage:true }); | 37 | + //await page.screenshot({ path: 'auction.png', fullPage:true }); |
39 | 38 | ||
40 | // container which will hold crawled data [{},{}...] | 39 | // container which will hold crawled data [{},{}...] |
41 | let data = []; | 40 | let data = []; |
... | @@ -51,7 +50,7 @@ const readline = require('readline'); | ... | @@ -51,7 +50,7 @@ const readline = require('readline'); |
51 | 50 | ||
52 | await browser.close(); | 51 | await browser.close(); |
53 | 52 | ||
54 | -})(); | 53 | +} |
55 | 54 | ||
56 | 55 | ||
57 | 56 | ... | ... |
1 | const puppeteer = require('puppeteer'); | 1 | const puppeteer = require('puppeteer'); |
2 | 2 | ||
3 | -// Gmarket Cart Crawling | 3 | +//coupang_c(); |
4 | -(async () => { | 4 | + |
5 | +// coupang Cart Crawling | ||
6 | +async function coupang_c(){ | ||
5 | 7 | ||
6 | // launching headless browser | 8 | // launching headless browser |
7 | const browser = await puppeteer.launch(); | 9 | const browser = await puppeteer.launch(); |
... | @@ -34,7 +36,7 @@ const puppeteer = require('puppeteer'); | ... | @@ -34,7 +36,7 @@ const puppeteer = require('puppeteer'); |
34 | 36 | ||
35 | // goto cart page | 37 | // goto cart page |
36 | await page.goto('https://cart.coupang.com/cartView.pang'); | 38 | await page.goto('https://cart.coupang.com/cartView.pang'); |
37 | - await page.screenshot({ path: 'coupang.png', fullPage:true }); | 39 | + //await page.screenshot({ path: 'coupang.png', fullPage:true }); |
38 | 40 | ||
39 | // container which will hold crawled data [{},{}...] | 41 | // container which will hold crawled data [{},{}...] |
40 | let data = []; | 42 | let data = []; |
... | @@ -50,7 +52,7 @@ const puppeteer = require('puppeteer'); | ... | @@ -50,7 +52,7 @@ const puppeteer = require('puppeteer'); |
50 | 52 | ||
51 | await browser.close(); | 53 | await browser.close(); |
52 | 54 | ||
53 | -})(); | 55 | +} |
54 | 56 | ||
55 | 57 | ||
56 | 58 | ||
... | @@ -91,4 +93,8 @@ async function getOne(page, index) { | ... | @@ -91,4 +93,8 @@ async function getOne(page, index) { |
91 | // not yet | 93 | // not yet |
92 | 94 | ||
93 | return Promise.resolve(data); | 95 | return Promise.resolve(data); |
94 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
96 | +} | ||
97 | + | ||
98 | +module.exports.coupang_c = coupang_c; | ||
99 | +module.exports.getOne = getOne; | ||
100 | +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 readline = require('readline'); | 2 | const readline = require('readline'); |
3 | 3 | ||
4 | +//gmarket_c(); | ||
4 | 5 | ||
5 | // Gmarket Cart Crawling | 6 | // Gmarket Cart Crawling |
6 | -(async () => { | 7 | +async function gmarket_c(){ |
7 | 8 | ||
8 | // launching headless browser | 9 | // launching headless browser |
9 | const browser = await puppeteer.launch(); | 10 | const browser = await puppeteer.launch(); |
... | @@ -34,7 +35,7 @@ const readline = require('readline'); | ... | @@ -34,7 +35,7 @@ const readline = require('readline'); |
34 | 35 | ||
35 | // goto cart page | 36 | // goto cart page |
36 | await page.goto('https://cart.gmarket.co.kr/ko/cart'); | 37 | await page.goto('https://cart.gmarket.co.kr/ko/cart'); |
37 | - await page.screenshot({ path: 'gmarket.png', fullPage:true }); | 38 | + //await page.screenshot({ path: 'gmarket.png', fullPage:true }); |
38 | 39 | ||
39 | // container which will hold crawled data [{},{}...] | 40 | // container which will hold crawled data [{},{}...] |
40 | let data = []; | 41 | let data = []; |
... | @@ -50,7 +51,7 @@ const readline = require('readline'); | ... | @@ -50,7 +51,7 @@ const readline = require('readline'); |
50 | 51 | ||
51 | await browser.close(); | 52 | await browser.close(); |
52 | 53 | ||
53 | -})(); | 54 | +} |
54 | 55 | ||
55 | 56 | ||
56 | 57 | ||
... | @@ -90,4 +91,8 @@ async function getOne(page, index) { | ... | @@ -90,4 +91,8 @@ async function getOne(page, index) { |
90 | // not yet | 91 | // not yet |
91 | 92 | ||
92 | return Promise.resolve(data); | 93 | return Promise.resolve(data); |
93 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
94 | +} | ||
95 | + | ||
96 | +module.exports.gmarket_c = gmarket_c; | ||
97 | +module.exports.getOne = getOne; | ||
98 | +module.exports.getAll = getAll; | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment