임승현

Merge branch 'feature/Chatbot_CGV' into 'master'

Feature/chatbot cgv



See merge request !36
1 +require('chromedriver');
2 +const request = require('request');
3 +const cheerio = require('cheerio');
4 +const puppeteer = require('puppeteer');
5 +
6 +const async = require('async');
7 +let express = require('express');
8 +let app = express();
9 +let bodyParser = require('body-parser');
10 +const { timeout } = require('async');
11 +
12 +const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기
13 +const webdriver = require('selenium-webdriver');
14 +const chrome = require('selenium-webdriver/chrome');
15 +const { delayed } = require('selenium-webdriver/lib/promise');
16 +const By = webdriver.By;
17 +
18 +app.use(bodyParser.urlencoded({ extended: false }));
19 +app.use(bodyParser.json());
20 +
21 +const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=0"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것. 예매율 순위 가져오기
22 +const url_theaters = "https://www.cgv.co.kr/theaters/"; //영화관 정보 가져오는 링크.
23 +const url_ticketing = "https://www.cgv.co.kr/ticket/"; //상영중인 영화 정보 가져오는 링크.
24 +
25 +let cgv_theaters = []; //영화관과 영화관 고유 코드를 담는 배열
26 +let cgv_movies = []; //예매율 상위 19위까지의 영화 정보(CGVMovieInfo Class의 인스턴스)들을 담는 배열.
27 +let cgv_accessible_movies = []; //선택한 일자, 영화관에서 예매할 수 있는 영화 이름과 영화 고유 코드를 담는 배열.
28 +
29 +//예매율 Top19위까지의 영화 정보를 관리하는 Class
30 +class CGVMovieInfo {
31 + constructor(title, rank, score, GoldenEgg, movieCode){
32 + this.title = title;
33 + this.rank = rank;
34 + this.score = score;
35 + this.GoldenEgg = GoldenEgg;
36 + this.movieCode = movieCode;
37 + }
38 +
39 + getTitle() { return this.title; }
40 + setTitle(title) { this.title = title; }
41 + getRank() { return this.rank; }
42 + setRank(rank) { this.rank = rank; }
43 + getScore() { return this.score; }
44 + setScore(score) { this.score = score; }
45 + getGoldenEgg() { return this.GoldenEgg; }
46 + setGoldenEgg(GoldenEgg) { this.GoldenEgg = GoldenEgg; }
47 + getMovieCode() { return this.movieCode; }
48 + setMovieCode(movieCode) { this.movieCode = movieCode; }
49 +
50 + printMovieInfo(){
51 + return {
52 + 'rank': this.rank + " : " + this.title,
53 + 'score': "예매율 : " + this.score + "%",
54 + 'goldenEgg': "골든에그지수 : " + this.GoldenEgg,
55 + 'movieCode': "영화코드 : " + this.movieCode
56 + };
57 + }
58 +
59 +}
60 +
61 +exports.init = () => {async.waterfall([
62 + async () => {
63 + //크롬 설정을 담은 객체 생성
64 + const driver_theaters = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
65 + driver_theaters.get(url_theaters);
66 +
67 + //9개 권역별로 영화관 list들을 list의 element로 넣기.
68 + let selector = '#contents > div.sect-common > div > div.sect-city > ul > li:nth-child({}) > div > ul > li > a';
69 + let area = [];
70 + for(let i = 1; i <= 9; i++){
71 + let region = await driver_theaters.wait(until.elementsLocated(By.css(selector.replace("{}", i))));
72 + area.push(region);
73 + }
74 +
75 + //영화관 및 영화관에 대응되는 영화관별 고유 코드 가져오기.
76 + for (const theaters_by_area of area) {
77 + let theaters_info_by_area = [];
78 + for (const theater of theaters_by_area){
79 + let theater_info = {
80 + "theater_name" : await theater.getAttribute('title'),
81 + "theater_code" : await theater.getAttribute('href')
82 + };
83 + theater_info.theater_name = theater_info.theater_name.replace("CGV", "")
84 + theater_info.theater_code = theater_info.theater_code.replace(/(.+(?<=theaterCode=))|(.+(?<=theatercode=))/, "").substring(0,4);
85 + theaters_info_by_area.push(theater_info);
86 + }
87 + cgv_theaters.push(theaters_info_by_area);
88 + }
89 + driver_theaters.close();
90 + },
91 + async () => {
92 + const driver_movies = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
93 + driver_movies.get(url_movies);
94 + //예매율 Top19까지의 영화의 정보를 가져옴.
95 +
96 + const rank = await driver_movies.wait(until.elementsLocated(By.css("strong.rank")));
97 + const title = await driver_movies.wait(until.elementsLocated(By.css("strong.title")));
98 + const score = await driver_movies.wait(until.elementsLocated(By.css("strong.percent")));
99 + const GoldenEgg = await driver_movies.wait(until.elementsLocated(By.css("span.percent")));
100 + const link = await driver_movies.wait(until.elementsLocated(By.css("a.link-reservation")));
101 +
102 + //영화 제목, 순위, 예매율, 영화 코드, 골든에그 지수를 가져와 CGVMovieInfo 객체 생성자에 파라미터로 넘겨주고, 인스턴스를 받아옴.
103 + for (let i = 0; i < rank.length; i++) {
104 + const newTitle = await title[i].getText();
105 + const newRank = await rank[i].getText();
106 + const newScore = await score[i].getText();
107 + const newCode = await link[i].getAttribute("href");
108 + const newMovie = new CGVMovieInfo(newTitle, parseInt(newRank.replace("No.", "")), newScore.replace("예매율", "").replace("%", ""), await GoldenEgg[i].getText(), newCode.replace(/[^0-9]/g, "").substring(0,8));
109 + cgv_movies.push(newMovie);
110 + }
111 + driver_movies.close();
112 + }
113 +])}
114 +
115 +app.get('/cgv_theaters', (req, res) => {
116 + res.send(cgv_theaters[0]);
117 +});
118 +
119 +/*
120 +app.post('/ticketing', async (req, res, next) => {
121 + //영화관 이름과 날짜를 가져옴.
122 + const theaterName = req.body.theaterName;
123 + const date = req.body.date;
124 + const LocateQuery = "?PLAY_YMD={}".replace("{}", date);
125 +
126 + //입력된 영화관에 맞는 지역 코드와 영화관 고유코드 찾기
127 + let regionCode = 0, theaterCode = "";
128 + for(let i = 0; i < 9; i++){
129 + for(const elem of cgv_theaters[i]){
130 + if(elem.theater_name == theaterName){
131 + regionCode = i;
132 + theaterCode = elem.theater_code;
133 + break;
134 + }
135 + }
136 + }
137 +
138 + //예매 가능한 영화 리스트를 얻기 위해 빠른 예매 사이트로 이동.
139 + const driver_ticketing = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options()).build();
140 + driver_ticketing.get(url_ticketing + LocateQuery);
141 + driver_ticketing.switchTo().frame("ticket_iframe"); //Frame 전환
142 + //setTimeout(() => {}, 1000);
143 +
144 + //지역 코드에 맞게 list element click
145 + const selected_areas_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li > a > span.name")));
146 + await selected_areas_list[regionCode].click();
147 + //setTimeout(() => {}, 5000);
148 +
149 + //선택한 지역에 대응되는 영화관 정보 가져오기
150 + const selected_theaters_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li.selected > div > ul > li")));
151 +
152 + //프로그램 내부에서 가지고 있는 영화관코드와 웹에서 받아온 영화관코드가 일치하는 경우, selected_theaters_list element 클릭
153 + for (const theater_element of selected_theaters_list){
154 + if(await theater_element.getAttribute("theater_cd") == theaterCode){
155 + await theater_element.click();
156 + //setTimeout(() => {}, 5000);
157 + break;
158 + }
159 + }
160 +
161 + //선택한 영화관에서, 선택한 일자에 상영하는 영화 목록 들고오기
162 + await driver_ticketing.sleep(1000);
163 + const selected_movies_list = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.text")));
164 + const codes_of_selected_movies = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li")));
165 +
166 + //선택불가를 제외한 영화 제목 및 영화 코드 가져오기.
167 + for(let i = 0; i < selected_movies_list.length; i++){
168 + //setTimeout(() => {}, 1000);
169 + const movie_enabled = await codes_of_selected_movies[i].getAttribute("class");
170 + if(movie_enabled.endsWith("dimmed"))
171 + break;
172 + const accessible_movie = {
173 + "movie_title": await selected_movies_list[i].getText(),
174 + "movie_code" : await codes_of_selected_movies[i].getAttribute("movie_cd_group")
175 + }
176 + cgv_accessible_movies.push(accessible_movie);
177 + }
178 + driver_ticketing.close();
179 +
180 + res.send(cgv_accessible_movies);
181 +});
182 +*/
183 +
184 +exports.getMovieChart = async(rank) => {
185 + let movie_chart = [];
186 + for(const movie_info of cgv_movies){
187 + if(movie_info.getRank() > rank)
188 + break;
189 + const top19_movie = {
190 + "rank" : movie_info.getRank(),
191 + "title" : movie_info.getTitle(),
192 + "code" : movie_info.getMovieCode()
193 + };
194 + movie_chart.push(top19_movie);
195 + }
196 + return movie_chart;
197 +}
198 +
199 +exports.getTheaterCode = async(theaterName) => {
200 + let theaterCode = "";
201 + for(let i = 0; i < 9; i++){
202 + for(const elem of cgv_theaters[i]){
203 + if(elem.theater_name == theaterName){
204 + theaterCode = elem.theater_code;
205 + break;
206 + }
207 + }
208 + }
209 + return theaterCode;
210 +}
211 +
212 +exports.getAccessibleMovies = async(theaterName, date) => {
213 + //영화관 이름과 날짜를 가져옴.
214 +
215 + //입력된 영화관에 맞는 지역 코드와 영화관 고유코드 찾기
216 + let regionCode = 0, theaterCode = "";
217 + for(let i = 0; i < 9; i++){
218 + for(const elem of cgv_theaters[i]){
219 + if(elem.theater_name == theaterName){
220 + regionCode = i;
221 + theaterCode = elem.theater_code;
222 + break;
223 + }
224 + }
225 + }
226 +
227 + const baseMovieCode = cgv_movies[0].getMovieCode();
228 + const LocateQuery = "?MOVIE_CD={0}&MOVIE_CD_GROUP={1}&THEATER_CD={2}&PLAY_YMD={3}".replace("{0}", baseMovieCode).replace("{1}", baseMovieCode).replace("{2}", theaterCode).replace("{3}", date);
229 +
230 + //예매 가능한 영화 리스트를 얻기 위해 빠른 예매 사이트로 이동.
231 + const driver_ticketing = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().addArguments("--headless")).build();
232 + driver_ticketing.get(url_ticketing + LocateQuery);
233 + await driver_ticketing.switchTo().frame("ticket_iframe"); //Frame 전환
234 +
235 + /*
236 + //setTimeout(() => {}, 1000);
237 +
238 + //지역 코드에 맞게 list element click
239 + const selected_areas_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li > a > span.name")));
240 + await selected_areas_list[regionCode].click();
241 + //setTimeout(() => {}, 5000);
242 +
243 + //선택한 지역에 대응되는 영화관 정보 가져오기
244 + const selected_theaters_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li.selected > div > ul > li")));
245 +
246 + //프로그램 내부에서 가지고 있는 영화관코드와 웹에서 받아온 영화관코드가 일치하는 경우, selected_theaters_list element 클릭
247 + for (const theater_element of selected_theaters_list){
248 + if(await theater_element.getAttribute("theater_cd") == theaterCode){
249 + await theater_element.click();
250 + //setTimeout(() => {}, 5000);
251 + break;
252 + }
253 + }
254 +
255 + //선택한 영화관에서, 선택한 일자에 상영하는 영화 목록 들고오기
256 + await driver_ticketing.sleep(1000);
257 + const selected_movies_list = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.text")));
258 + const codes_of_selected_movies = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li")));
259 + */
260 +
261 + //선택불가를 제외한 영화 제목 및 영화 코드 가져오기.
262 + //const selected_movies_list = await driver_ticketing.wait(until.elementsLocated(By.css("strong")));
263 +
264 + //await driver_ticketing.sleep(1000);
265 +
266 + //선택한 영화관에서, 선택한 일자에 상영하는 영화 목록 들고오기
267 + //await driver_ticketing.sleep(1000);
268 + const selected_movies_list = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.text")));
269 + const codes_of_selected_movies = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li")));
270 +
271 + //선택불가를 제외한 영화 제목 및 영화 코드 가져오기.
272 + for(let i = 0; i < selected_movies_list.length; i++){
273 + //setTimeout(() => {}, 1000);
274 + const movie_enabled = await codes_of_selected_movies[i].getAttribute("class");
275 + if(movie_enabled.endsWith("dimmed"))
276 + break;
277 + const accessible_movie = {
278 + "movie_title": await selected_movies_list[i].getText(),
279 + "movie_code" : await codes_of_selected_movies[i].getAttribute("movie_cd_group")
280 + }
281 + cgv_accessible_movies.push(accessible_movie);
282 + }
283 + return cgv_accessible_movies;
284 +}
285 +
286 +app.listen(23017);
...\ No newline at end of file ...\ No newline at end of file
1 +const CGV = require('./CGVTicketing.js');
2 +//const SearchingTheaterAPI = require('./SearchingTheaterAPI');
3 +const async = require('async');
4 +CGV.init();
5 +
6 +const PUSH_TARGET_URL = 'https://api.line.me/v2/bot/message/push'
7 +const REPLY_TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
8 +const asyncHandler = require('express-async-handler');
9 +const bodyParser = require('body-parser');
10 +const request = require('request');
11 +const moment = require("moment");
12 +const HTTPS = require('https');
13 +const path = require('path');
14 +const fs = require('fs');
15 +const sslport = 23023;
16 +
17 +var express = require('express');
18 +var app = express();
19 +app.use(bodyParser.json());
20 +
21 +/////////////////////////////////////////////////
22 +// commit 할때 지워야 할 것들
23 +const USER_ID = '';
24 +const TOKEN = '';
25 +const domain = "";
26 +const LocalAPI_TOKEN = "";
27 +/////////////////////////////////////////////////
28 +let isBrandSelected = false;
29 +let CGV_Flag = -1; //진행 단계
30 +let CGV_date = ""; //날짜
31 +let RequestedLocation = ""; //사용자가 입력한 장소
32 +let RespondedTheaters = []; //API를 통해 받아온 영화관들 및 카카오맵 연결 링크
33 +let SelectedTheater = ""; //사용자가 설정한 영화관 이름
34 +let SelectedTheaterCode = ""; //영화관 고유코드
35 +let CGV_accessible_movies = []; //상영 날짜와 상영관에 따라 예매할 수 있는 영화 목록
36 +let CGV_movie_chart = [];
37 +let url_web = "https://www.cgv.co.kr/ticket/";
38 +let url_mobile = "https://m.cgv.co.kr/WebApp/Reservation/quickResult.aspx";
39 +/////////////////////////////////////////////////
40 +
41 +//영화관 정보와 영화 랭킹을 가져오는 것까지 대략 30초가 걸림 => 30초 기다리고 메세지 전송
42 +setTimeout(function () {
43 + PushSingleMessage("원하시는 브랜드의 번호를 입력해주세요.\n1: CGV\n2: LotteCinema\n3: Megabox\n언제든 브랜드를 바꾸고 싶으시다면 '브랜드'를 입력해주세요.");
44 +}, 20000);
45 +
46 +app.post('/hook', asyncHandler(async (req, res, next) => {
47 +
48 + var eventObj = req.body.events[0];
49 + var source = eventObj.source;
50 + var message = eventObj.message;
51 +
52 + // request log
53 + console.log('======================', new Date() ,'======================');
54 + console.log('[request]', req.body);
55 + console.log('[request source] ', eventObj.source);
56 + console.log('[request message]', eventObj.message);
57 +
58 + //어느 순간에서든 "브랜드"를 입력해 원하는 브랜드 선택
59 + //initFlag : false ==> 브랜드 선택 전
60 + //initFlag : true ==> 브랜드 선택 됨
61 + if (eventObj.message.text == "브랜드") {
62 + isBrandSelected = false;
63 + CGV_Flag = -1;
64 + PushSingleMessage("원하시는 브랜드의 번호를 입력해주세요.\n1: CGV\n2: LotteCinema\n3: Megabox\n언제든 브랜드를 바꾸고 싶으시다면 '브랜드'를 입력해주세요.");
65 + }
66 +
67 + //브랜드 선택- CGV 인 경우 CGV_Flag를 0으로 두어 메가박스 임을 확인
68 + if (isBrandSelected == false && eventObj.message.text == 1) {
69 + isBrandSelected = true;
70 + CGV_Flag = 0;
71 + }
72 +
73 + //CGV로 브랜드가 선택된 경우
74 + if(isBrandSelected == true && CGV_Flag != -1){
75 + if(CGV_Flag === 0){
76 + const text1 = "영화관 위치를 입력해주세요";
77 + const text2 = "ex) 강남"
78 + SendMessage(eventObj, text1, text2);
79 + CGV_Flag++;
80 + }
81 + else if(CGV_Flag === 1){
82 + RespondedTheaters = [];
83 + RequestedLocation = message.text;
84 + KakaoLocalAPI("CGV", RequestedLocation);
85 + setTimeout(function() {
86 + if(RespondedTheaters.length == 0)
87 + PushSingleMessage("검색 결과가 없습니다. 다시 입력해주세요.");
88 + else{
89 + if(RespondedTheaters.length == 1){
90 + SelectedTheater = RespondedTheaters[0].theater_name;
91 + SelectedTheaterCode = CGV.getTheaterCode(SelectedTheater);
92 + setTimeout(function() {
93 + CGV_Flag = 2;
94 + }, 2000);
95 + }
96 + else{
97 + let CGV_OutputString = "원하시는 상영관의 번호를 정확히 입력해주세요\n";
98 + for (let i = 0; i < RespondedTheaters.length; i++) {
99 + CGV_OutputString += String(i + 1) + ": " + RespondedTheaters[i].theater_name + "\n";
100 + }
101 + CGV_OutputString += String(RespondedTheaters.length + 1) + ": 다시 검색하기";
102 + PushSingleMessage(CGV_OutputString);
103 + CGV_Flag = 101;
104 + }
105 + }
106 + }, 2000);
107 +
108 + }
109 + else if(CGV_Flag == 101 && RespondedTheaters.length != 0){
110 + let selection = parseInt(message.text);
111 + if(selection > 0 && selection < RespondedTheaters.length + 1){
112 + SelectedTheater = RespondedTheaters[selection - 1].theater_name;
113 + SelectedTheaterCode = await CGV.getTheaterCode(SelectedTheater);
114 + CGV_Flag = 2;
115 + }
116 + else{
117 + const text1 = "영화관 위치를 입력해주세요";
118 + const text2 = "ex) 강남"
119 + SendMessage(eventObj, text1, text2);
120 + CGV_Flag = 1;
121 + }
122 +
123 + }
124 + ////날짜 입력 받기
125 + if(CGV_Flag === 2){
126 + const text1 = "선택한 영화관은 CGV" + SelectedTheater + "입니다.\n 영화를 관람할 날짜를 선택해 주세요.";
127 + const text2 = "ex)20020409, YYYYMMDD";
128 + SendMessage(eventObj, text1, text2);
129 + CGV_Flag = 3;
130 + }
131 + //날짜 확인 및 날짜, 장소에 대해 상영중인 영화 리스트 가져오기
132 + if (moment(message.text, "YYYYMMDD", true).isValid() && CGV_Flag == 3) {
133 + CGV_date = message.text;
134 + //console.log(MEGA_date, MEGA_TheaterLocation);
135 + if (CGV_date && SelectedTheater) {
136 + CGV_accessible_movies = await CGV.getMovieChart(5);
137 + const text1 = "현재상영작을 가져오는 중입니다.";
138 + const text2 = "잠시만 기다려주세요.";
139 + PushMessage(text1, text2);
140 + console.log(CGV_accessible_movies);
141 + CGV_Flag++;
142 + }
143 + //원본 코드
144 + // MEGA_date = parseInt(eventObj.message.text);
145 + // if (MEGA_date && MEGA_TheaterLocationCode) {
146 + // MEGA_PlayingMovieURL = "https://megabox.co.kr/on/oh/ohb/SimpleBooking/simpleBookingPage.do" + '?brchNo1=' + MEGA_TheaterLocationCode + '&playDe=' + MEGA_date;
147 + // console.log(MEGA_PlayingMovieURL)
148 + // async.waterfall[
149 + // megabox.using_PlayingMovieURL(MEGA_PlayingMovieURL),
150 + // megabox.geting_PlayingMovie()
151 + // ]
152 + // MEGA_flag++
153 + // console.log(MEGA_flag);
154 + // }
155 + }
156 + if(CGV_Flag === 4){
157 + let AccessibleMovieText = "-- 예매 가능한 상영작 --\n\n";
158 + if(CGV_accessible_movies.length == 0){
159 + PushMessage("현재상영작이 없습니다.","영화관 선택 단계로 이동합니다.");
160 + setTimeout(function () {
161 + PushMessage("영화관 위치를 입력해주세요", "ex1) 강남");
162 + }, 1000);
163 + CGV_Flag = 1;
164 + }
165 + else if(CGV_accessible_movies.length == 1){
166 + AccessibleMovieText += ("1. " + CGV_accessible_movies[0].title);
167 + const SelectedMovieCode = CGV_accessible_movies[0].code;
168 + PushMessage(AccessibleMovieText, "바로 링크가 전송됩니다.");
169 + setTimeout(function() {
170 + const finalURL_web = url_web + "?MOVIE_CD=" + SelectedMovieCode + "&MOVIE_CD_GROUP=" + SelectedMovieCode + "&THEATER_CD=" + SelectedTheaterCode + "&PLAY_YMD=" + CGV_date;
171 + const finalURL_mobile = url_mobile + "?mgc=" + SelectedMovieCode + "&tc=" + SelectedTheaterCode + "&ymd=" + CGV_date;
172 + //console.log(finalURL_web);
173 + //PushMessage(finalURL_web, "링크를 누르면 예매 창으로 바로 이동합니다.");
174 + PushURLMessage(finalURL_web, finalURL_mobile);
175 + }, 1000);
176 + }
177 + else{
178 + setTimeout(function() {
179 + let rank = 1;
180 + for(const elem of CGV_accessible_movies){
181 + AccessibleMovieText += (rank.toString() + ". " + elem.title);
182 + AccessibleMovieText += "\n";
183 + rank++;
184 + }
185 + console.log(AccessibleMovieText);
186 + PushMessage(AccessibleMovieText, "예매할 영화 번호를 입력해주세요.\n ex)1 (영화 앞 숫자만 입력)");
187 + CGV_Flag = 5;
188 + }, 1000);
189 + }
190 + }
191 + if(CGV_Flag === 5){
192 + const index = parseInt(message.text) - 1;
193 + const SelectedMovieCode = CGV_accessible_movies[index].code;
194 + const finalURL_web = url_web + "?MOVIE_CD=" + SelectedMovieCode + "&MOVIE_CD_GROUP=" + SelectedMovieCode + "&THEATER_CD=" + SelectedTheaterCode + "&PLAY_YMD=" + CGV_date;
195 + const finalURL_mobile = url_mobile + "?mgc=" + SelectedMovieCode + "&tc=" + SelectedTheaterCode + "&ymd=" + CGV_date;
196 + //console.log(finalURL_web);
197 + //PushMessage(finalURL_web, "링크를 누르면 예매 창으로 바로 이동합니다.");
198 + PushURLMessage(finalURL_web, finalURL_mobile);
199 + setTimeout(function () {
200 + isBrandSelected = false;
201 + CGV_Flag = -1;
202 + PushSingleMessage("원하시는 브랜드의 번호를 입력해주세요.\n1: CGV\n2: LotteCinema\n3: Megabox\n언제든 브랜드를 바꾸고 싶으시다면 '브랜드'를 입력해주세요.");
203 + }, 1000);
204 + }
205 +
206 + }
207 + res.sendStatus(200);
208 +}));
209 +
210 +try {
211 + const option = {
212 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
213 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
214 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
215 + };
216 +
217 + HTTPS.createServer(option, app).listen(sslport, () => {
218 + console.log(`[HTTPS] Server is started on port ${sslport}`);
219 + });
220 + } catch (error) {
221 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
222 + console.log(error);
223 + }
224 +
225 +
226 +//메세지 전송하는 function 모음
227 +function SendMessage(eventObj, text1, text2 = "") { //reply message
228 + request.post(
229 + {
230 + url: REPLY_TARGET_URL,
231 + headers: {
232 + 'Authorization': `Bearer ${TOKEN}`
233 + },
234 + json: {
235 + "replyToken": eventObj.replyToken,
236 + "messages": [
237 + {
238 + "type": "text",
239 + "text": text1
240 + },
241 + {
242 + "type": "text",
243 + "text": text2
244 + }
245 + ]
246 + }
247 + }, (error, response, body) => {
248 + console.log(body);
249 + });
250 +}
251 +
252 +function PushMessage(text1, text2 = "") { //push two message
253 + request.post(
254 + {
255 + url: PUSH_TARGET_URL,
256 + headers: {
257 + 'Authorization': `Bearer ${TOKEN}`
258 + },
259 + json: {
260 + "to": `${USER_ID}`,
261 + "messages": [
262 + {
263 + "type": "text",
264 + "text": text1
265 + },
266 + {
267 + "type": "text",
268 + "text": text2
269 + }
270 + ]
271 + }
272 + }, (error, response, body) => {
273 + console.log(body)
274 + });
275 +}
276 +
277 +function PushSingleMessage(text1) {//push single message
278 + request.post(
279 + {
280 + url: PUSH_TARGET_URL,
281 + headers: {
282 + 'Authorization': `Bearer ${TOKEN}`
283 + },
284 + json: {
285 + "to": `${USER_ID}`,
286 + "messages": [
287 + {
288 + "type": "text",
289 + "text": text1
290 + }
291 + ]
292 + }
293 + }, (error, response, body) => {
294 + console.log(body)
295 + });
296 +}
297 +
298 +function PushURLMessage(pcurl, smartphoneurl) {//push single message
299 + request.post(
300 + {
301 + url: PUSH_TARGET_URL,
302 + headers: {
303 + 'Authorization': `Bearer ${TOKEN}`
304 + },
305 + json: {
306 + "to": `${USER_ID}`,
307 + "messages": [
308 + {
309 + "type": "text",
310 + "text": "pc버전 url입니다\n\n" + pcurl
311 + },
312 + {
313 + "type": "text",
314 + "text": "mobile버전 url입니다\n\n" + smartphoneurl
315 + }
316 + ]
317 + }
318 + }, (error, response, body) => {
319 + console.log(body)
320 + });
321 +}
322 +
323 +function KakaoLocalAPI(brand, location){
324 + let kakaoOptions = {
325 + url : "https://dapi.kakao.com/v2/local/search/keyword",
326 + method : "GET",
327 + headers : {
328 + 'Authorization': `KakaoAK ${LocalAPI_TOKEN}`
329 + },
330 + qs: {
331 + 'query': `${brand} ${location}`,
332 + //'category_group_code' : 'CT1',
333 + 'size' : 5
334 + },
335 + encoding : 'UTF-8'
336 + };
337 + request(kakaoOptions, function (err, res, body) {
338 + info_list = JSON.parse(body).documents;
339 + if(!err && res.statusCode == 200){
340 + info_list.forEach(info => {
341 + if(info.category_name.endsWith("CGV")){
342 + const theater_info = {
343 + "theater_name" : info.place_name.replace("CGV ", ""),
344 + "theater_url" : info.place_url
345 + };
346 + RespondedTheaters.push(theater_info);
347 + }
348 + });
349 + }
350 + });
351 +
352 +}
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "name": "test01",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "index.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "author": "",
10 + "license": "ISC",
11 + "dependencies": {
12 + "async": "^3.2.3",
13 + "body-parser": "^1.20.0",
14 + "cheerio": "^1.0.0-rc.11",
15 + "chromedriver": "^102.0.0",
16 + "express": "^4.18.1",
17 + "express-async-handler": "^1.2.0",
18 + "moment": "^2.29.3",
19 + "puppeteer": "^14.1.1",
20 + "request": "^2.88.2",
21 + "selenium-webdriver": "^4.1.2"
22 + }
23 +}
...@@ -3,19 +3,19 @@ import org.jsoup.nodes.Document; ...@@ -3,19 +3,19 @@ import org.jsoup.nodes.Document;
3 import org.jsoup.nodes.Element; 3 import org.jsoup.nodes.Element;
4 import org.jsoup.select.Elements; 4 import org.jsoup.select.Elements;
5 5
6 -import java.awt.*;
7 -import java.io.*;
8 -import java.net.URI;
9 -import java.net.URISyntaxException;
10 -import java.util.*;
11 -import java.util.List;
12 -
13 import org.openqa.selenium.By; 6 import org.openqa.selenium.By;
14 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.WebDriver;
15 import org.openqa.selenium.WebElement; 8 import org.openqa.selenium.WebElement;
16 import org.openqa.selenium.chrome.ChromeDriver; 9 import org.openqa.selenium.chrome.ChromeDriver;
17 import org.openqa.selenium.chrome.ChromeOptions; 10 import org.openqa.selenium.chrome.ChromeOptions;
18 11
12 +import java.awt.*;
13 +import java.io.IOException;
14 +import java.net.URI;
15 +import java.net.URISyntaxException;
16 +import java.util.List;
17 +import java.util.*;
18 +
19 class CGVMovieInfo { //CGV 영화 정보를 담는 class 19 class CGVMovieInfo { //CGV 영화 정보를 담는 class
20 private String title; //영화 제목 20 private String title; //영화 제목
21 private int rank; //CGV 내 예매율 순위 21 private int rank; //CGV 내 예매율 순위
......
1 +require('chromedriver');
2 +const request = require('request');
3 +const cheerio = require('cheerio');
4 +const puppeteer = require('puppeteer');
5 +
6 +const async = require('async');
7 +let express = require('express');
8 +let app = express();
9 +let bodyParser = require('body-parser');
10 +const { timeout } = require('async');
11 +
12 +const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기
13 +const webdriver = require('selenium-webdriver');
14 +const chrome = require('selenium-webdriver/chrome');
15 +const { delayed } = require('selenium-webdriver/lib/promise');
16 +const By = webdriver.By;
17 +
18 +app.use(bodyParser.urlencoded({ extended: false }));
19 +app.use(bodyParser.json());
20 +
21 +const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=0"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것. 예매율 순위 가져오기
22 +const url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오는 링크.
23 +const url_ticketing = "https://www.cgv.co.kr/ticket/"; //상영중인 영화 정보 가져오는 링크.
24 +
25 +let cgv_theaters = []; //영화관과 영화관 고유 코드를 담는 배열
26 +let cgv_movies = []; //예매율 상위 19위까지의 영화 정보(CGVMovieInfo Class의 인스턴스)들을 담는 배열.
27 +let cgv_accessible_movies = []; //선택한 일자, 영화관에서 예매할 수 있는 영화 이름과 영화 고유 코드를 담는 배열.
28 +
29 +class CGVMovieInfo {
30 + constructor(title, rank, score, GoldenEgg, movieCode){
31 + this.title = title;
32 + this.rank = rank;
33 + this.score = score;
34 + this.GoldenEgg = GoldenEgg;
35 + this.movieCode = movieCode;
36 + }
37 +
38 + getTitle() { return this.title; }
39 + setTitle(title) { this.title = title; }
40 + getRank() { return this.rank; }
41 + setRank(rank) { this.rank = rank; }
42 + getScore() { return this.score; }
43 + setScore(score) { this.score = score; }
44 + getGoldenEgg() { return this.GoldenEgg; }
45 + setGoldenEgg(GoldenEgg) { this.GoldenEgg = GoldenEgg; }
46 + getMovieCode() { return this.movieCode; }
47 + setMovieCode(movieCode) { this.movieCode = movieCode; }
48 +
49 + printMovieInfo(){
50 + return {
51 + 'rank': this.rank + " : " + this.title,
52 + 'score': "예매율 : " + this.score + "%",
53 + 'goldenEgg': "골든에그지수 : " + this.GoldenEgg,
54 + 'movieCode': "영화코드 : " + this.movieCode
55 + };
56 + }
57 +
58 +}
59 +
60 +async.waterfall([
61 + async () => {
62 + //크롬 설정을 담은 객체 생성
63 + const driver_theaters = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
64 + driver_theaters.get(url_theaters);
65 + //영화관 및 영화관에 대응되는 영화관별 고유 코드 가져오기.
66 + let selector = '#contents > div.sect-common > div > div.sect-city > ul > li:nth-child({}) > div > ul > li > a';
67 + let area = [];
68 + for(let i = 1; i <= 9; i++){
69 + let region = await driver_theaters.wait(until.elementsLocated(By.css(selector.replace("{}", i))));
70 + area.push(region);
71 + }
72 + for (const theaters_by_area of area) {
73 + let theaters_info_by_area = [];
74 + for (const theater of theaters_by_area){
75 + let theater_info = {
76 + "theater_name" : await theater.getAttribute('title'),
77 + "theater_code" : await theater.getAttribute('href')
78 + };
79 + theater_info.theater_name = theater_info.theater_name.replace("CGV", "")
80 + theater_info.theater_code = theater_info.theater_code.replace(/(.+(?<=theaterCode=))|(.+(?<=theatercode=))/, "").substring(0,4);
81 + theaters_info_by_area.push(theater_info);
82 + }
83 + cgv_theaters.push(theaters_info_by_area);
84 + }
85 + driver_theaters.close();
86 + },
87 + async () => {
88 + const driver_movies = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
89 + driver_movies.get(url_movies);
90 + //예매율 Top19까지의 영화의 정보를 가져옴.
91 +
92 + const rank = await driver_movies.wait(until.elementsLocated(By.css("strong.rank")));
93 + const title = await driver_movies.wait(until.elementsLocated(By.css("strong.title")));
94 + const score = await driver_movies.wait(until.elementsLocated(By.css("strong.percent")));
95 + const GoldenEgg = await driver_movies.wait(until.elementsLocated(By.css("span.percent")));
96 + const link = await driver_movies.wait(until.elementsLocated(By.css("a.link-reservation")));
97 +
98 + //영화 제목, 순위, 예매율, 영화 코드, 골든에그 지수를 가져와 CGVMovieInfo 객체 생성자에 파라미터로 넘겨주고, 인스턴스를 받아옴.
99 + for (let i = 0; i < rank.length; i++) {
100 + const newTitle = await title[i].getText();
101 + const newRank = await rank[i].getText();
102 + const newScore = await score[i].getText();
103 + const newCode = await link[i].getAttribute("href");
104 + const newMovie = new CGVMovieInfo(newTitle, parseInt(newRank.replace("No.", "")), newScore.replace("예매율", "").replace("%", ""), await GoldenEgg[i].getText(), newCode.replace(/[^0-9]/g, "").substring(0,8));
105 + cgv_movies.push(newMovie);
106 + }
107 + driver_movies.close();
108 + }
109 +])
110 +
111 +app.get('/cgv_theaters', (req, res) => {
112 + res.send(cgv_theaters[0]);
113 +});
114 +
115 +app.post('/ticketing', async (req, res, next) => {
116 + //영화관 이름과 날짜를 가져옴.
117 + const theaterName = req.body.theaterName;
118 + const date = req.body.date;
119 + const LocateQuery = "?PLAY_YMD={}".replace("{}", date);
120 +
121 + //입력된 영화관에 맞는 지역 코드와 영화관 고유코드 찾기
122 + let regionCode = 0, theaterCode = "";
123 + for(let i = 0; i < 9; i++){
124 + for(const elem of cgv_theaters[i]){
125 + if(elem.theater_name == theaterName){
126 + regionCode = i;
127 + theaterCode = elem.theater_code;
128 + break;
129 + }
130 + }
131 + }
132 +
133 + //예매 가능한 영화 리스트를 얻기 위해 빠른 예매 사이트로 이동.
134 + const driver_ticketing = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options()).build();
135 + driver_ticketing.get(url_ticketing + LocateQuery);
136 + driver_ticketing.switchTo().frame("ticket_iframe"); //Frame 전환
137 +
138 + //지역 코드에 맞게 list element click
139 + const selected_areas_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li > a > span.name")));
140 + await selected_areas_list[regionCode].click();
141 + driver_ticketing.sleep(1000);
142 +
143 + //선택한 지역에 대응되는 영화관 정보 가져오기
144 + const selected_theaters_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li.selected > div > ul > li")));
145 +
146 + //프로그램 내부에서 가지고 있는 영화관코드와 웹에서 받아온 영화관코드가 일치하는 경우, selected_theaters_list element 클릭
147 + for (const theater_element of selected_theaters_list){
148 + if(await theater_element.getAttribute("theater_cd") == theaterCode){
149 + await theater_element.click();
150 + driver_ticketing.sleep(1000);
151 + break;
152 + }
153 + }
154 +
155 + //선택한 영화관에서, 선택한 일자에 상영하는 영화 목록 들고오기
156 + const selected_movies_list = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.text")));
157 + const codes_of_selected_movies = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li")));
158 +
159 + //선택불가를 제외한 영화 제목 및 영화 코드 가져오기.
160 + for(let i = 0; i < selected_movies_list.length; i++){
161 + const movie_enabled = await codes_of_selected_movies[i].getAttribute("class")
162 + if(movie_enabled.endsWith("dimmed"))
163 + break;
164 + const accessible_movie = {
165 + "movie_title": await selected_movies_list[i].getText(),
166 + "movie_code" : await codes_of_selected_movies[i].getAttribute("movie_cd_group")
167 + }
168 + cgv_accessible_movies.push(accessible_movie);
169 + }
170 + driver_ticketing.close();
171 +
172 + res.send(cgv_accessible_movies);
173 +});
174 +
175 +app.listen(23023);
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "name": "test01",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "index.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "author": "",
10 + "license": "ISC",
11 + "dependencies": {
12 + "async": "^3.2.3",
13 + "body-parser": "^1.20.0",
14 + "cheerio": "^1.0.0-rc.11",
15 + "express": "^4.18.1",
16 + "puppeteer": "^14.1.1",
17 + "selenium-webdriver": "^4.1.2"
18 + }
19 +}