Megabox.js
6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const chatbot = require("./app.js");
const request = require('request');
const cheerio = require('cheerio');
const puppeteer = require('puppeteer');
require('chromedriver');
const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기
var webdriver = require('selenium-webdriver');
var By = webdriver.By;
const chrome = require('selenium-webdriver/chrome');//크롬 사용시
const async = require('async')
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
const { timeout } = require('async');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const booking_url = "https://megabox.co.kr/booking?";
exports.booking_url = booking_url;
const rate_url = "https://www.megabox.co.kr/movie";
let r =0;
let movie_data = [];
exports.movie_data = movie_data;
let location_data = [];
exports.location_data = location_data;
let index = 0;
exports.init = ()=>{async.waterfall([//for 동기적 처리
async () => {
const driver = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();//
driver.get(booking_url);
driver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴
let seoul = await driver.wait(until.elementsLocated(By.css('#mCSB_4_container>ul>li>#btn')));
let Gyeonggi = await driver.wait(until.elementsLocated(By.css('#mCSB_5_container>ul>li>#btn')));
const Incheon = await driver.wait(until.elementsLocated(By.css('#mCSB_6_container>ul>li>#btn')));
const DCS = await driver.wait(until.elementsLocated(By.css('#mCSB_7_container>ul>li>#btn')));//Daejeon Chungcheong Sejong
const BDG = await driver.wait(until.elementsLocated(By.css('#mCSB_8_container>ul>li>#btn')));//Busan Daegu Gyeongsang
const GJ= await driver.wait(until.elementsLocated(By.css('#mCSB_9_container>ul>li>#btn')));//gwangju_jeonla
const Gangwon = await driver.wait(until.elementsLocated(By.css('#mCSB_10_container>ul>li>#btn')));
const location_list = [seoul, Gyeonggi, Incheon, DCS, BDG, GJ, Gangwon]//
for(let i = 0; i < location_list.length; i++){
for (item of location_list[i]) {
location_data[index++] = {
'LocationName':await item.getAttribute("brch-nm"),
'LocationNUm' : await item.getAttribute("brch-no")
}
// let location_name = await item.getAttribute("brch-nm");
// let location_num = await item.getAttribute("brch-no");
// let obj = {};
// obj[location_name]= location_num
// location_data[index++] = obj;
}
}
let movie_list = await driver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn')));
r = 0;
for (item of movie_list) {
//Using getAttribute to get the data
movie_data[r++] = {
'rank' : r,
'title' : await item.getAttribute("movie-nm"),
'movie_num':await item.getAttribute("movie-no"),
}
}
driver.close();
},
async () => {
r = 0;
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto(rate_url);
const content = await page.content();
const $ = cheerio.load(content);
const $rate_lists = $("ol.list>li");
$rate_lists.each((index, list) => {
const name = $(list).find('div.tit-area > p.tit').attr('title');
const rate = $(list).find('div.rate-date > span.rate').text();
if(movie_data[r].title === name){
movie_data[r++]['rate'] = rate;
}
});
for(i of movie_data){
if(Object.keys(i).length==3){
movie_data[r++]['rate'] = '예매율 0%';
}
}
browser.close();
},
])}
const appdriver = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
exports.using_PlayingMovieURL = async(PlayingMovieURL) => {
appdriver.get(PlayingMovieURL);
//appdriver.switchTo().frame(0)
//frameBokdMBooking 프레임 가져옴
}
exports.geting_PlayingMovie= async() => {
let movie_list = await appdriver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn')));
let n = 0;
for (item of movie_list) {
movie_data[n++]['running'] = await item.getAttribute('form-at')
}
}
// let userData = {
// 'Date': '',
// 'location':''
// };
// // const _sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
// app.get('/Megabox', (req, res) => {
// res.send(movie_data);
// })
// app.post('/Megabox', (req, res) => {//사용자에게 Date와 location(영화관 장소) 받아옴
// let PlayingMovieURL;
// userData['Date'] = req.body.Date;
// for(i of location_data){
// if(i['LocationName'] == req.body.location){
// userData['location']=i['LocationNUm'];
// break;
// }
// }
// PlayingMovieURL = booking_url + '?brchNo1='+userData['location']+'&playDe='+userData['Date'];//사용자 정보 바탕으로 해당 일자 영화관 영화 상영 여부 확인
// appdriver.get(PlayingMovieURL);
// appdriver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴
// res.send(movie_data);
// })
// app.post('/Megabox', (req, res) => {//사용자에게 Date와 location(영화관 장소) 받아옴
// userData['Date'] = req.body.Date;
// for(i of location_data){
// if(i['LocationName'] == req.body.location){
// userData['location']=i['LocationNUm'];
// break;
// }
// }
// let PlayingMovieURL = booking_url + '?brchNo1='+userData['location']+'&playDe='+userData['Date'];//사용자 정보 바탕으로 해당 일자 영화관 영화 상영 여부 확인
// appdriver.get(PlayingMovieURL);
// appdriver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴
// res.send(movie_data);
// })
app.get('/Megabox/GetPlayingMovie', async(req, res, next) => {//영화 상영 여부 객체에 넣음
// let movie_list = await appdriver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn')));
// let n = 0;
// for (item of movie_list) {
// movie_data[n++]['running'] = await item.getAttribute('form-at')
// }
using_PlayingMovieURL('https://megabox.co.kr/booking?brchNo1=4451&playDe=20220606');
geting_PlayingMovie();
res.send(PlayingMovieList);
})
app.listen(6000);