Showing
4 changed files
with
351 additions
and
0 deletions
ChatbotTest_Megabox/Megabox.js
0 → 100644
1 | +const chatbot = require("./app.js"); | ||
2 | +const request = require('request'); | ||
3 | +const cheerio = require('cheerio'); | ||
4 | +const puppeteer = require('puppeteer'); | ||
5 | +require('chromedriver'); | ||
6 | +const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기 | ||
7 | +var webdriver = require('selenium-webdriver'); | ||
8 | +var By = webdriver.By; | ||
9 | +const chrome = require('selenium-webdriver/chrome');//크롬 사용시 | ||
10 | +const async = require('async') | ||
11 | +let express = require('express'); | ||
12 | +let app = express(); | ||
13 | +let bodyParser = require('body-parser'); | ||
14 | +const { timeout } = require('async'); | ||
15 | +app.use(bodyParser.urlencoded({ extended: false })); | ||
16 | +app.use(bodyParser.json()); | ||
17 | +const booking_url = "https://megabox.co.kr/booking?"; | ||
18 | +exports.booking_url = booking_url; | ||
19 | +const rate_url = "https://www.megabox.co.kr/movie"; | ||
20 | +let r =0; | ||
21 | +let movie_data = []; | ||
22 | +exports.movie_data = movie_data; | ||
23 | +let location_data = []; | ||
24 | +exports.location_data = location_data; | ||
25 | +let index = 0; | ||
26 | +exports.init = ()=>{async.waterfall([//for 동기적 처리 | ||
27 | + async () => { | ||
28 | + const driver = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();// | ||
29 | + driver.get(booking_url); | ||
30 | + driver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴 | ||
31 | + let seoul = await driver.wait(until.elementsLocated(By.css('#mCSB_4_container>ul>li>#btn'))); | ||
32 | + let Gyeonggi = await driver.wait(until.elementsLocated(By.css('#mCSB_5_container>ul>li>#btn'))); | ||
33 | + const Incheon = await driver.wait(until.elementsLocated(By.css('#mCSB_6_container>ul>li>#btn'))); | ||
34 | + const DCS = await driver.wait(until.elementsLocated(By.css('#mCSB_7_container>ul>li>#btn')));//Daejeon Chungcheong Sejong | ||
35 | + const BDG = await driver.wait(until.elementsLocated(By.css('#mCSB_8_container>ul>li>#btn')));//Busan Daegu Gyeongsang | ||
36 | + const GJ= await driver.wait(until.elementsLocated(By.css('#mCSB_9_container>ul>li>#btn')));//gwangju_jeonla | ||
37 | + const Gangwon = await driver.wait(until.elementsLocated(By.css('#mCSB_10_container>ul>li>#btn'))); | ||
38 | + const location_list = [seoul, Gyeonggi, Incheon, DCS, BDG, GJ, Gangwon]// | ||
39 | + for(let i = 0; i < location_list.length; i++){ | ||
40 | + for (item of location_list[i]) { | ||
41 | + location_data[index++] = { | ||
42 | + 'LocationName':await item.getAttribute("brch-nm"), | ||
43 | + 'LocationNUm' : await item.getAttribute("brch-no") | ||
44 | + } | ||
45 | + // let location_name = await item.getAttribute("brch-nm"); | ||
46 | + // let location_num = await item.getAttribute("brch-no"); | ||
47 | + // let obj = {}; | ||
48 | + // obj[location_name]= location_num | ||
49 | + // location_data[index++] = obj; | ||
50 | + } | ||
51 | + } | ||
52 | + let movie_list = await driver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn'))); | ||
53 | + r = 0; | ||
54 | + for (item of movie_list) { | ||
55 | + //Using getAttribute to get the data | ||
56 | + movie_data[r++] = { | ||
57 | + 'rank' : r, | ||
58 | + 'title' : await item.getAttribute("movie-nm"), | ||
59 | + 'movie_num':await item.getAttribute("movie-no"), | ||
60 | + } | ||
61 | + } | ||
62 | + | ||
63 | + driver.close(); | ||
64 | + | ||
65 | + }, | ||
66 | + async () => { | ||
67 | + r = 0; | ||
68 | + const browser = await puppeteer.launch({ | ||
69 | + headless: true | ||
70 | + }); | ||
71 | + const page = await browser.newPage(); | ||
72 | + await page.goto(rate_url); | ||
73 | + const content = await page.content(); | ||
74 | + | ||
75 | + const $ = cheerio.load(content); | ||
76 | + const $rate_lists = $("ol.list>li"); | ||
77 | + $rate_lists.each((index, list) => { | ||
78 | + const name = $(list).find('div.tit-area > p.tit').attr('title'); | ||
79 | + const rate = $(list).find('div.rate-date > span.rate').text(); | ||
80 | + | ||
81 | + if(movie_data[r].title === name){ | ||
82 | + movie_data[r++]['rate'] = rate; | ||
83 | + } | ||
84 | + }); | ||
85 | + for(i of movie_data){ | ||
86 | + if(Object.keys(i).length==3){ | ||
87 | + movie_data[r++]['rate'] = '예매율 0%'; | ||
88 | + } | ||
89 | + } | ||
90 | + | ||
91 | + browser.close(); | ||
92 | + console.log("Comepleted!"); | ||
93 | + }, | ||
94 | + | ||
95 | +])} | ||
96 | +const appdriver = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build(); | ||
97 | +exports.using_PlayingMovieURL = async(PlayingMovieURL) => { | ||
98 | + appdriver.get(PlayingMovieURL); | ||
99 | + //appdriver.switchTo().frame(0) | ||
100 | + //frameBokdMBooking 프레임 가져옴 | ||
101 | +} | ||
102 | +exports.geting_PlayingMovie= async() => { | ||
103 | + let movie_list = await appdriver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn'))); | ||
104 | + let n = 0; | ||
105 | + for (item of movie_list) { | ||
106 | + movie_data[n++]['running'] = await item.getAttribute('form-at') | ||
107 | + } | ||
108 | + | ||
109 | +} | ||
110 | +// let userData = { | ||
111 | +// 'Date': '', | ||
112 | +// 'location':'' | ||
113 | +// }; | ||
114 | +// // const _sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay)); | ||
115 | +// app.get('/Megabox', (req, res) => { | ||
116 | +// res.send(movie_data); | ||
117 | +// }) | ||
118 | +// app.post('/Megabox', (req, res) => {//사용자에게 Date와 location(영화관 장소) 받아옴 | ||
119 | +// let PlayingMovieURL; | ||
120 | +// userData['Date'] = req.body.Date; | ||
121 | +// for(i of location_data){ | ||
122 | +// if(i['LocationName'] == req.body.location){ | ||
123 | +// userData['location']=i['LocationNUm']; | ||
124 | +// break; | ||
125 | +// } | ||
126 | +// } | ||
127 | +// PlayingMovieURL = booking_url + '?brchNo1='+userData['location']+'&playDe='+userData['Date'];//사용자 정보 바탕으로 해당 일자 영화관 영화 상영 여부 확인 | ||
128 | + | ||
129 | +// appdriver.get(PlayingMovieURL); | ||
130 | +// appdriver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴 | ||
131 | +// res.send(movie_data); | ||
132 | +// }) | ||
133 | +// app.post('/Megabox', (req, res) => {//사용자에게 Date와 location(영화관 장소) 받아옴 | ||
134 | +// userData['Date'] = req.body.Date; | ||
135 | +// for(i of location_data){ | ||
136 | +// if(i['LocationName'] == req.body.location){ | ||
137 | +// userData['location']=i['LocationNUm']; | ||
138 | +// break; | ||
139 | +// } | ||
140 | +// } | ||
141 | +// let PlayingMovieURL = booking_url + '?brchNo1='+userData['location']+'&playDe='+userData['Date'];//사용자 정보 바탕으로 해당 일자 영화관 영화 상영 여부 확인 | ||
142 | + | ||
143 | +// appdriver.get(PlayingMovieURL); | ||
144 | +// appdriver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴 | ||
145 | +// res.send(movie_data); | ||
146 | +// }) | ||
147 | +app.get('/Megabox/GetPlayingMovie', async(req, res, next) => {//영화 상영 여부 객체에 넣음 | ||
148 | + | ||
149 | + // let movie_list = await appdriver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn'))); | ||
150 | + // let n = 0; | ||
151 | + // for (item of movie_list) { | ||
152 | + // movie_data[n++]['running'] = await item.getAttribute('form-at') | ||
153 | + // } | ||
154 | + using_PlayingMovieURL('https://megabox.co.kr/booking?brchNo1=4451&playDe=20220606'); | ||
155 | + geting_PlayingMovie(); | ||
156 | + res.send(PlayingMovieList); | ||
157 | +}) | ||
158 | +app.listen(5000); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
ChatbotTest_Megabox/app.js
0 → 100644
1 | +let megabox = require('./Megabox.js'); | ||
2 | +const async = require('async') | ||
3 | +//let SearchingTheaterAPI = require('./SearchingTheaterAPI'); | ||
4 | +megabox.init(); | ||
5 | +const request = require('request'); | ||
6 | +var express = require('express'); | ||
7 | +const REPLY_TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
8 | +const PUSH_TARGET_URL = 'https://api.line.me/v2/bot/message/push' | ||
9 | +const USER_ID = 'U9d630077f22259431099ace7e9a0eb4c' | ||
10 | +const REPLY_TOKEN = 'Yq9KhnuTveSUn7SihUGL51E1YlFvpMkl/w0LwXhGyZq8lzZFWQ5naQgQ504rqwT0sVBScuXZOFHTxFvFb3F/3KXCcOVaj6NlSgwHCanXrsugF9E5KqhROLyRv4EGxvt9MleTefgt1qx5piktuYGkFwdB04t89/1O/w1cDnyilFU=' | ||
11 | +const fs = require('fs'); | ||
12 | +const path = require('path'); | ||
13 | +const HTTPS = require('https'); | ||
14 | +const domain = "2021105612.osschatbot2022.ml" | ||
15 | +const sslport = 23023; | ||
16 | +const bodyParser = require('body-parser'); | ||
17 | +var app = express(); | ||
18 | +app.use(bodyParser.json()); | ||
19 | +const moment = require("moment"); | ||
20 | +let MEGA_date; | ||
21 | +let MEGA_TheaterLocation; | ||
22 | +let MEGA_title; | ||
23 | +let MEGA_PlayingMovieURL; | ||
24 | +exports.MEGA_PlayingMovieURL = MEGA_PlayingMovieURL; | ||
25 | +let MEGA_flag = 0; | ||
26 | + | ||
27 | + | ||
28 | + | ||
29 | +function SendMessage(eventObj, text1, text2 = "") { | ||
30 | + request.post( | ||
31 | + { | ||
32 | + url: REPLY_TARGET_URL, | ||
33 | + headers: { | ||
34 | + 'Authorization': `Bearer ${REPLY_TOKEN}` | ||
35 | + }, | ||
36 | + json: { | ||
37 | + "replyToken": eventObj.replyToken, | ||
38 | + "messages": [ | ||
39 | + { | ||
40 | + "type": "text", | ||
41 | + "text": text1 | ||
42 | + }, | ||
43 | + { | ||
44 | + "type": "text", | ||
45 | + "text": text2 | ||
46 | + } | ||
47 | + ] | ||
48 | + } | ||
49 | + | ||
50 | + }, (error, response, body) => { | ||
51 | + //console.log(body) | ||
52 | + }); | ||
53 | +} | ||
54 | +function SendSingleMessage(text1) { | ||
55 | + request.post( | ||
56 | + { | ||
57 | + url: PUSH_TARGET_URL, | ||
58 | + headers: { | ||
59 | + 'Authorization': `Bearer ${REPLY_TOKEN | ||
60 | +}` | ||
61 | + }, | ||
62 | + json: { | ||
63 | + "to": `${USER_ID}`, | ||
64 | + "messages":[ | ||
65 | + { | ||
66 | + "type":"text", | ||
67 | + "text": text1 | ||
68 | + } | ||
69 | + ] | ||
70 | + } | ||
71 | + },(error, response, body) => { | ||
72 | + console.log(body) | ||
73 | + }); | ||
74 | +} | ||
75 | +app.post('/hook', function (req, res) { | ||
76 | + var eventObj = req.body.events[0]; | ||
77 | + var source = eventObj.source; | ||
78 | + var message = eventObj.message; | ||
79 | + // request log | ||
80 | + console.log('======================', new Date(), '======================'); | ||
81 | + console.log('[request]', req.body); | ||
82 | + console.log('[request source] ', eventObj.source); | ||
83 | + console.log('[request message]', eventObj.message); | ||
84 | + if (eventObj.message.text == 3 && MEGA_flag === 0) { | ||
85 | + if (megabox.location_data) { | ||
86 | + const text1 = "영화관 위치를 입력해주세요"; | ||
87 | + const text2 = "ex1)강남, ex2)강남대로(씨티)"; | ||
88 | + SendMessage(eventObj, text1, text2); | ||
89 | + MEGA_flag++; | ||
90 | + } | ||
91 | + } | ||
92 | + if (MEGA_flag === 1) { | ||
93 | + let count = 0; | ||
94 | + let inputAbleLocationList = []; | ||
95 | + for (i of megabox.location_data) { | ||
96 | + if (i['LocationName'].includes(message.text)) { | ||
97 | + inputAbleLocationList[count++] = i; | ||
98 | + //MEGA_TheaterLocation = i['LocationNUm']; | ||
99 | + //console.log(MEGA_TheaterLocation); | ||
100 | + //MEGA_flag++; | ||
101 | + } | ||
102 | + } | ||
103 | + if(count == 1){ //결과 1개 => 바로 다음 단계 넘어가기 | ||
104 | + MEGA_TheaterLocation = inputAbleLocationList[0].LocationName; | ||
105 | + console.log(MEGA_TheaterLocation); | ||
106 | + MEGA_flag++; | ||
107 | + }else if (count > 1){ //결과 2개 이상 => 리스트 출력해주고 번호로 입력받아 넘어가기 | ||
108 | + console.log(inputAbleLocationList[0],inputAbleLocationList[1]); | ||
109 | + | ||
110 | + SendSingleMessage("원하시는 상영관을 정확히 입력해주세요"); | ||
111 | + | ||
112 | + setTimeout(function () { | ||
113 | + for(let x =0; x<count; x++){ | ||
114 | + setTimeout(function () { | ||
115 | + SendSingleMessage(inputAbleLocationList[x].LocationName); | ||
116 | + console.log(inputAbleLocationList[x].LocationName); | ||
117 | + }, 100*(x+1)); | ||
118 | + } | ||
119 | + }, 100); | ||
120 | + //MEGA_flag= 101; | ||
121 | + } | ||
122 | + //console.log(count, inputAbleLocationList); | ||
123 | + | ||
124 | + } else { | ||
125 | + SendMessage(eventObj, "다시입력해주세요"); | ||
126 | + } | ||
127 | + if(MEGA_flag == 101){ | ||
128 | + | ||
129 | + } | ||
130 | + | ||
131 | + if (MEGA_flag === 2) { | ||
132 | + const text1 = "영화를 보실 날짜를 입력해주세요."; | ||
133 | + const text2 = "ex)20020409"; | ||
134 | + SendMessage(eventObj, text1, text2); | ||
135 | + ++MEGA_flag; | ||
136 | + } | ||
137 | + | ||
138 | + if (moment(eventObj.message.text, "YYYYMMDD", true).isValid() && MEGA_flag === 3) { | ||
139 | + MEGA_date = parseInt(message.text); | ||
140 | + console.log(MEGA_date, MEGA_TheaterLocation); | ||
141 | + if (MEGA_date && MEGA_TheaterLocation) { | ||
142 | + console.log("1"); | ||
143 | + MEGA_PlayingMovieURL = "https://megabox.co.kr/on/oh/ohb/SimpleBooking/simpleBookingPage.do" + '?brchNo1=' + MEGA_TheaterLocation + '&playDe=' + MEGA_date; | ||
144 | + console.log("1", MEGA_PlayingMovieURL); | ||
145 | + async.waterfall[ | ||
146 | + megabox.using_PlayingMovieURL(MEGA_PlayingMovieURL), | ||
147 | + megabox.geting_PlayingMovie() | ||
148 | + ] | ||
149 | + MEGA_flag++; | ||
150 | + } | ||
151 | + } | ||
152 | + if (MEGA_flag === 4) { | ||
153 | + console.log(megabox.movie_data); | ||
154 | + } | ||
155 | + res.sendStatus(200); | ||
156 | + | ||
157 | +}); | ||
158 | + | ||
159 | +try { | ||
160 | + const option = { | ||
161 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | ||
162 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | ||
163 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | ||
164 | + }; | ||
165 | + | ||
166 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
167 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
168 | + }); | ||
169 | +} catch (error) { | ||
170 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
171 | + console.log(error); | ||
172 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
ChatbotTest_Megabox/package-lock.json
0 → 100644
This diff could not be displayed because it is too large.
ChatbotTest_Megabox/package.json
0 → 100644
1 | +{ | ||
2 | + "name": "megabox", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "main": "app.js", | ||
6 | + "scripts": { | ||
7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | + }, | ||
9 | + "keywords": [], | ||
10 | + "author": "", | ||
11 | + "license": "ISC", | ||
12 | + "dependencies": { | ||
13 | + "async": "^3.2.3", | ||
14 | + "body-parser": "^1.20.0", | ||
15 | + "cheerio": "^1.0.0-rc.11", | ||
16 | + "chromedriver": "^101.0.0", | ||
17 | + "express": "^4.18.1", | ||
18 | + "puppeteer": "^14.1.1", | ||
19 | + "selenium-webdriver": "^4.1.2" | ||
20 | + } | ||
21 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment