임승현

Enable to load accessible movies by date and theater

...@@ -12,6 +12,7 @@ const { timeout } = require('async'); ...@@ -12,6 +12,7 @@ const { timeout } = require('async');
12 const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기 12 const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기
13 const webdriver = require('selenium-webdriver'); 13 const webdriver = require('selenium-webdriver');
14 const chrome = require('selenium-webdriver/chrome'); 14 const chrome = require('selenium-webdriver/chrome');
15 +const { delayed } = require('selenium-webdriver/lib/promise');
15 const By = webdriver.By; 16 const By = webdriver.By;
16 17
17 app.use(bodyParser.urlencoded({ extended: false })); 18 app.use(bodyParser.urlencoded({ extended: false }));
...@@ -21,9 +22,9 @@ const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=0"; //ëì˜ ì¿¼ë¦¬ 0ì ...@@ -21,9 +22,9 @@ const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=0"; //ëì˜ ì¿¼ë¦¬ 0ì
21 const url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오는 링크. 22 const url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오는 링크.
22 const url_ticketing = "https://www.cgv.co.kr/ticket/"; //상영중인 영화 정보 가져오는 링크. 23 const url_ticketing = "https://www.cgv.co.kr/ticket/"; //상영중인 영화 정보 가져오는 링크.
23 24
24 -let cgv_theaters = []; 25 +let cgv_theaters = []; //영화관과 영화관 고유 코드를 담는 배열
25 -let cgv_movies = []; 26 +let cgv_movies = []; //예매율 상위 19위까지의 영화 정보(CGVMovieInfo Class의 인스턴스)들을 담는 배열.
26 - 27 +let cgv_accessible_movies = []; //선택한 일자, 영화관에서 예매할 수 있는 영화 이름과 영화 고유 코드를 담는 배열.
27 28
28 class CGVMovieInfo { 29 class CGVMovieInfo {
29 constructor(title, rank, score, GoldenEgg, movieCode){ 30 constructor(title, rank, score, GoldenEgg, movieCode){
...@@ -68,15 +69,14 @@ async.waterfall([ ...@@ -68,15 +69,14 @@ async.waterfall([
68 let region = await driver_theaters.wait(until.elementsLocated(By.css(selector.replace("{}", i)))); 69 let region = await driver_theaters.wait(until.elementsLocated(By.css(selector.replace("{}", i))));
69 area.push(region); 70 area.push(region);
70 } 71 }
71 - let n = 0;
72 for (const theaters_by_area of area) { 72 for (const theaters_by_area of area) {
73 let theaters_info_by_area = []; 73 let theaters_info_by_area = [];
74 for (const theater of theaters_by_area){ 74 for (const theater of theaters_by_area){
75 - let theater_name = await theater.getAttribute('title');
76 let theater_info = { 75 let theater_info = {
77 "theater_name" : await theater.getAttribute('title'), 76 "theater_name" : await theater.getAttribute('title'),
78 - "theater_code" : await theater.getAttribute('href')//.replace("(.+(?<=theaterCode=))|(.+(?<=theatercode=))", "").substring(0,4) 77 + "theater_code" : await theater.getAttribute('href')
79 }; 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); 80 theater_info.theater_code = theater_info.theater_code.replace(/(.+(?<=theaterCode=))|(.+(?<=theatercode=))/, "").substring(0,4);
81 theaters_info_by_area.push(theater_info); 81 theaters_info_by_area.push(theater_info);
82 } 82 }
...@@ -89,7 +89,6 @@ async.waterfall([ ...@@ -89,7 +89,6 @@ async.waterfall([
89 driver_movies.get(url_movies); 89 driver_movies.get(url_movies);
90 //예매율 Top19까지의 영화의 정보를 가져옴. 90 //예매율 Top19까지의 영화의 정보를 가져옴.
91 91
92 - //let chart = await driver_movies.wait(until.elementLocated(By.className("sect-movie-chart")));
93 const rank = await driver_movies.wait(until.elementsLocated(By.css("strong.rank"))); 92 const rank = await driver_movies.wait(until.elementsLocated(By.css("strong.rank")));
94 const title = await driver_movies.wait(until.elementsLocated(By.css("strong.title"))); 93 const title = await driver_movies.wait(until.elementsLocated(By.css("strong.title")));
95 const score = await driver_movies.wait(until.elementsLocated(By.css("strong.percent"))); 94 const score = await driver_movies.wait(until.elementsLocated(By.css("strong.percent")));
...@@ -102,7 +101,7 @@ async.waterfall([ ...@@ -102,7 +101,7 @@ async.waterfall([
102 const newRank = await rank[i].getText(); 101 const newRank = await rank[i].getText();
103 const newScore = await score[i].getText(); 102 const newScore = await score[i].getText();
104 const newCode = await link[i].getAttribute("href"); 103 const newCode = await link[i].getAttribute("href");
105 - const newMovie = new CGVMovieInfo(newTitle, parseInt(newRank.replace("No.", "")), newScore.replace("예매율", "").replace("%", ""), await GoldenEgg[i].getText(), newCode.replace(/[^0-9]/, "").substring(0,8)); 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));
106 cgv_movies.push(newMovie); 105 cgv_movies.push(newMovie);
107 } 106 }
108 driver_movies.close(); 107 driver_movies.close();
...@@ -113,8 +112,64 @@ app.get('/cgv_theaters', (req, res) => { ...@@ -113,8 +112,64 @@ app.get('/cgv_theaters', (req, res) => {
113 res.send(cgv_theaters[0]); 112 res.send(cgv_theaters[0]);
114 }); 113 });
115 114
116 -app.post('', (req, res) => { 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 + //const movie_enabled = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.sreader")));
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();
117 171
172 + res.send(cgv_accessible_movies);
118 }); 173 });
119 174
120 app.listen(23023); 175 app.listen(23023);
...\ No newline at end of file ...\ No newline at end of file
......