임승현

Load the CGV Theaters and Accessble Movies in NodeJS

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 By = webdriver.By;
16 +
17 +app.use(bodyParser.urlencoded({ extended: false }));
18 +app.use(bodyParser.json());
19 +
20 +const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것. 예매율 순위 가져오기
21 +const url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오는 링크.
22 +const url_ticketing = "https://www.cgv.co.kr/ticket/"; //상영중인 영화 정보 가져오는 링크.
23 +
24 +let cgv_theaters = [];
25 +let cgv_movies = [];
26 +
27 +
28 +class CGVMovieInfo {
29 + constructor(title, rank, score, GoldenEgg, movieCode){
30 + this.title = title;
31 + this.rank = rank;
32 + this.score = score;
33 + this.GoldenEgg = GoldenEgg;
34 + this.movieCode = movieCode;
35 + }
36 +
37 + getTitle() { return this.title; }
38 + setTitle(title) { this.title = title; }
39 + getRank() { return this.rank; }
40 + setRank(rank) { this.rank = rank; }
41 + getScore() { return this.score; }
42 + setScore(score) { this.score = score; }
43 + getGoldenEgg() { return this.GoldenEgg; }
44 + setGoldenEgg(GoldenEgg) { this.GoldenEgg = GoldenEgg; }
45 + getMovieCode() { return this.movieCode; }
46 + setMovieCode(movieCode) { this.movieCode = movieCode; }
47 +
48 + printMovieInfo(){
49 + return {
50 + 'rank': this.rank + " : " + this.title,
51 + 'score': "예매율 : " + this.score + "%",
52 + 'goldenEgg': "골든에그지수 : " + this.GoldenEgg,
53 + 'movieCode': "영화코드 : " + this.movieCode
54 + };
55 + }
56 +
57 +}
58 +
59 +async.waterfall([
60 + async () => {
61 + //크롬 설정을 담은 객체 생성
62 + const driver_theaters = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
63 + driver_theaters.get(url_theaters);
64 + //영화관 및 영화관에 대응되는 영화관별 고유 코드 가져오기.
65 + let area = await driver_theaters.wait(until.elementsLocated(By.className("area")));
66 + for (const elem of area) {
67 + let theaters_info = new Map();
68 + let theaters_by_area = await elem.wait(until.elementsLocated(By.tagName('a')));
69 + theaters_by_area.forEach(theater => {
70 + const theater_name = theater.getAttribute("title").replace("CGV", "");
71 + const theater_code = theater.getAttribute("href").replaceAll("(.+(?<=theaterCode=))|(.+(?<=theatercode=))", "").substring(0,4);
72 + theaters_info.set(theater_name, theater_code);
73 + });
74 + cgv_theaters.push(theaters_info);
75 + }
76 + driver_theaters.close();
77 + },
78 + async () => {
79 + const driver_movies = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless().build());
80 + driver_movies.get(url_movies);
81 + //예매율 Top19까지의 영화의 정보를 가져옴.
82 + const chart = await driver_movies.wait(until.elementLocated(By.className("sect-movie-chart")));
83 + const rank = await chart.wait(until.elementsLocated(By.css("strong.rank")));
84 + const title = await chart.wait(until.elementsLocated(By.css("strong.title")));
85 + const score = await chart.wait(until.elementsLocated(By.css("strong.percent")));
86 + const GoldenEgg = await chart.wait(until.elementsLocated(By.css("span.percent")));
87 + const link = await chart.wait(until.elementsLocated(By.css("a.plink-reservation")));
88 +
89 + //영화 제목, 순위, 예매율, 영화 코드, 골든에그 지수를 가져와 CGVMovieInfo 객체 생성자에 파라미터로 넘겨주고, 인스턴스를 받아옴.
90 + for (let i = 0; i < rank.length; i++) {
91 + const newTitle = title[i];
92 + const newRank = parseInt(rank[i].replace("No.", ""));
93 + const newScore = score[i].replace("예매율", "").replace("%", "");
94 + const newCode = link[i].getAttribute("href").replace(("[^0-9]", "")).substring(0,8);
95 + const newMovie = new CGVMovieInfo(newTitle, newRank, newScore, GoldenEgg[i], newCode);
96 + cgv_movies.push(newMovie);
97 + }
98 + }
99 +]);