CGVTicketing.js 4.44 KB
require('chromedriver');
const request = require('request');
const cheerio = require('cheerio');
const puppeteer = require('puppeteer');

const async = require('async');
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
const { timeout } = require('async');

const {Builder,until} = require('selenium-webdriver');  //모듈 불러오기
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const By = webdriver.By;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 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 = [];


class CGVMovieInfo {
    constructor(title, rank, score, GoldenEgg, movieCode){
        this.title = title;
        this.rank = rank;
        this.score = score;
        this.GoldenEgg = GoldenEgg;
        this.movieCode = movieCode;
    }

    getTitle() { return this.title; }
    setTitle(title) { this.title = title; }
    getRank() { return this.rank; }
    setRank(rank) { this.rank = rank; }
    getScore() { return this.score; }
    setScore(score) { this.score = score; }
    getGoldenEgg() { return this.GoldenEgg; }
    setGoldenEgg(GoldenEgg) { this.GoldenEgg = GoldenEgg; }
    getMovieCode() { return this.movieCode; }
    setMovieCode(movieCode) { this.movieCode = movieCode; }

    printMovieInfo(){
        return {
            'rank': this.rank + " : " + this.title,
            'score': "예매율 : " + this.score + "%",
            'goldenEgg': "골든에그지수 : " + this.GoldenEgg,
            'movieCode': "영화코드 : " + this.movieCode
        };
    }
    
}

async.waterfall([
    async () => {
        //크롬 설정을 담은 객체 생성
        const driver_theaters = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build();
        driver_theaters.get(url_theaters);
        //영화관 및 영화관에 대응되는 영화관별 고유 코드 가져오기.
        let area = await driver_theaters.wait(until.elementsLocated(By.className("area")));
        for (const elem of area) {
            let theaters_info = new Map();
            let theaters_by_area = await elem.wait(until.elementsLocated(By.tagName('a')));
            theaters_by_area.forEach(theater => {
                const theater_name = theater.getAttribute("title").replace("CGV", "");
                const theater_code = theater.getAttribute("href").replaceAll("(.+(?<=theaterCode=))|(.+(?<=theatercode=))", "").substring(0,4);
                theaters_info.set(theater_name, theater_code);
            });
            cgv_theaters.push(theaters_info);
        }
        driver_theaters.close();
    },
    async () => {
        const driver_movies = new webdriver.Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless().build());
        driver_movies.get(url_movies);
        //예매율 Top19까지의 영화의 정보를 가져옴.
        const chart = await driver_movies.wait(until.elementLocated(By.className("sect-movie-chart")));
        const rank = await chart.wait(until.elementsLocated(By.css("strong.rank")));
        const title = await chart.wait(until.elementsLocated(By.css("strong.title")));
        const score = await chart.wait(until.elementsLocated(By.css("strong.percent")));
        const GoldenEgg = await chart.wait(until.elementsLocated(By.css("span.percent")));
        const link = await chart.wait(until.elementsLocated(By.css("a.plink-reservation")));
        
        //영화 제목, 순위, 예매율, 영화 코드, 골든에그 지수를 가져와 CGVMovieInfo 객체 생성자에 파라미터로 넘겨주고, 인스턴스를 받아옴.
        for (let i = 0; i < rank.length; i++) {
            const newTitle = title[i];
            const newRank = parseInt(rank[i].replace("No.", ""));
            const newScore = score[i].replace("예매율", "").replace("%", "");
            const newCode = link[i].getAttribute("href").replace(("[^0-9]", "")).substring(0,8);
            const newMovie = new CGVMovieInfo(newTitle, newRank, newScore, GoldenEgg[i], newCode);
            cgv_movies.push(newMovie);
        }
    }
]);