임승현

Enable to load accessible movies by date and theater

......@@ -12,6 +12,7 @@ const { timeout } = require('async');
const {Builder,until} = require('selenium-webdriver'); //모듈 불러오기
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const { delayed } = require('selenium-webdriver/lib/promise');
const By = webdriver.By;
app.use(bodyParser.urlencoded({ extended: false }));
......@@ -21,9 +22,9 @@ const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=0"; //ëì˜ ì¿¼ë¦¬ 0ì
const url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오는 링크.
const url_ticketing = "https://www.cgv.co.kr/ticket/"; //상영중인 영화 정보 가져오는 링크.
let cgv_theaters = [];
let cgv_movies = [];
let cgv_theaters = []; //영화관과 영화관 고유 코드를 담는 배열
let cgv_movies = []; //예매율 상위 19위까지의 영화 정보(CGVMovieInfo Class의 인스턴스)들을 담는 배열.
let cgv_accessible_movies = []; //선택한 일자, 영화관에서 예매할 수 있는 영화 이름과 영화 고유 코드를 담는 배열.
class CGVMovieInfo {
constructor(title, rank, score, GoldenEgg, movieCode){
......@@ -68,15 +69,14 @@ async.waterfall([
let region = await driver_theaters.wait(until.elementsLocated(By.css(selector.replace("{}", i))));
area.push(region);
}
let n = 0;
for (const theaters_by_area of area) {
let theaters_info_by_area = [];
for (const theater of theaters_by_area){
let theater_name = await theater.getAttribute('title');
let theater_info = {
"theater_name" : await theater.getAttribute('title'),
"theater_code" : await theater.getAttribute('href')//.replace("(.+(?<=theaterCode=))|(.+(?<=theatercode=))", "").substring(0,4)
"theater_code" : await theater.getAttribute('href')
};
theater_info.theater_name = theater_info.theater_name.replace("CGV", "")
theater_info.theater_code = theater_info.theater_code.replace(/(.+(?<=theaterCode=))|(.+(?<=theatercode=))/, "").substring(0,4);
theaters_info_by_area.push(theater_info);
}
......@@ -89,7 +89,6 @@ async.waterfall([
driver_movies.get(url_movies);
//예매율 Top19까지의 영화의 정보를 가져옴.
//let chart = await driver_movies.wait(until.elementLocated(By.className("sect-movie-chart")));
const rank = await driver_movies.wait(until.elementsLocated(By.css("strong.rank")));
const title = await driver_movies.wait(until.elementsLocated(By.css("strong.title")));
const score = await driver_movies.wait(until.elementsLocated(By.css("strong.percent")));
......@@ -102,7 +101,7 @@ async.waterfall([
const newRank = await rank[i].getText();
const newScore = await score[i].getText();
const newCode = await link[i].getAttribute("href");
const newMovie = new CGVMovieInfo(newTitle, parseInt(newRank.replace("No.", "")), newScore.replace("예매율", "").replace("%", ""), await GoldenEgg[i].getText(), newCode.replace(/[^0-9]/, "").substring(0,8));
const newMovie = new CGVMovieInfo(newTitle, parseInt(newRank.replace("No.", "")), newScore.replace("예매율", "").replace("%", ""), await GoldenEgg[i].getText(), newCode.replace(/[^0-9]/g, "").substring(0,8));
cgv_movies.push(newMovie);
}
driver_movies.close();
......@@ -113,8 +112,64 @@ app.get('/cgv_theaters', (req, res) => {
res.send(cgv_theaters[0]);
});
app.post('', (req, res) => {
app.post('/ticketing', async (req, res, next) => {
//영화관 이름과 날짜를 가져옴.
const theaterName = req.body.theaterName;
const date = req.body.date;
const LocateQuery = "?PLAY_YMD={}".replace("{}", date);
//입력된 영화관에 맞는 지역 코드와 영화관 고유코드 찾기
let regionCode = 0, theaterCode = "";
for(let i = 0; i < 9; i++){
for(const elem of cgv_theaters[i]){
if(elem.theater_name == theaterName){
regionCode = i;
theaterCode = elem.theater_code;
break;
}
}
}
//예매 가능한 영화 리스트를 얻기 위해 빠른 예매 사이트로 이동.
const driver_ticketing = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options()).build();
driver_ticketing.get(url_ticketing + LocateQuery);
driver_ticketing.switchTo().frame("ticket_iframe"); //Frame 전환
//지역 코드에 맞게 list element click
const selected_areas_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li > a > span.name")));
await selected_areas_list[regionCode].click();
driver_ticketing.sleep(1000);
//선택한 지역에 대응되는 영화관 정보 가져오기
const selected_theaters_list = await driver_ticketing.wait(until.elementsLocated(By.css("#theater_area_list > ul > li.selected > div > ul > li")));
//프로그램 내부에서 가지고 있는 영화관코드와 웹에서 받아온 영화관코드가 일치하는 경우, selected_theaters_list element 클릭
for (const theater_element of selected_theaters_list){
if(await theater_element.getAttribute("theater_cd") == theaterCode){
await theater_element.click();
driver_ticketing.sleep(1000);
break;
}
}
//선택한 영화관에서, 선택한 일자에 상영하는 영화 목록 들고오기
const selected_movies_list = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.text")));
const codes_of_selected_movies = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li")));
//const movie_enabled = await driver_ticketing.wait(until.elementsLocated(By.css("#movie_list > ul > li > a > span.sreader")));
for(let i = 0; i < selected_movies_list.length; i++){
const movie_enabled = await codes_of_selected_movies[i].getAttribute("class")
if(movie_enabled.endsWith("dimmed"))
break;
const accessible_movie = {
"movie_title": await selected_movies_list[i].getText(),
"movie_code" : await codes_of_selected_movies[i].getAttribute("movie_cd_group")
}
cgv_accessible_movies.push(accessible_movie);
}
driver_ticketing.close();
res.send(cgv_accessible_movies);
});
app.listen(23023);
\ No newline at end of file
......