CGVExample.java 5.12 KB
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

class CGVMovieInfo { //CGV 영화 정보를 담는 class
    private String title; //영화 제목
    private int rank; //CGV 내 예매율 순위
    private float score; //예매율
    private String GoldenEgg; //골든에그 지수
    private String movieCode; //CGV 고유 영화코드 - 예매 사이트 연결 시 사용

    public CGVMovieInfo(String title, int rank, float score, String GoldenEgg, String movieCode) {
        this.title = title;
        this.rank = rank;
        this.score = score;
        this.GoldenEgg = GoldenEgg;
        this.movieCode = movieCode;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    public String getGoldenEgg() {
        return GoldenEgg;
    }

    public void setGoldenEgg(String goldenEgg) {
        GoldenEgg = goldenEgg;
    }

    public String getMovieCode() {
        return movieCode;
    }

    public void setMovieCode(String movieCode) {
        this.movieCode = movieCode;
    }

    public String getLink() {
        return String.format("https://www.cgv.co.kr/ticket/?MOVIE_CD=%s&MOVIE_CD_GROUP=%s", this.movieCode, this.movieCode);
    }

    public void printMovieInfo(){
        System.out.println("-------------------------------------------------------");
        System.out.println(this.rank + " : " + this.title);
        System.out.println("예매율 : " + this.score + "%");
        System.out.println("골든에그지수 : " + this.GoldenEgg);
        System.out.println("영화코드 : " + this.movieCode);
        System.out.println("-------------------------------------------------------");
    }

}

public class CGVExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것. 예매율 순위 가져오기
        String url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오기.

        Document doc_movies, doc_theaters;

        ArrayList<LinkedHashMap<String, Integer>> Theaters = new ArrayList<>(); //지역별 영화관 HashMap(Key: 영화관, value:영화관별 고유코드)으로 이루어진 Arraylist
        ArrayList<CGVMovieInfo> Movies = new ArrayList<>(); //CGVMovieInfo 클래스의 인스턴스들을 원소로 가지는 Arraylist

        try {
            doc_movies = Jsoup.connect(url_movies).get();
            doc_theaters = Jsoup.connect(url_theaters).get();

            //예매율 Top19까지의 영화의 정보를 가져옴.
            Elements elements1 = doc_movies.select("div.sect-movie-chart");
            Iterator<Element> rank = elements1.select("strong.rank").iterator();
            Iterator<Element> title = elements1.select("strong.title").iterator();
            Iterator<Element> score = elements1.select("strong.percent").iterator();
            Iterator<Element> GoldenEgg = elements1.select("span.percent").iterator();
            Iterator<Element> link = elements1.select("a.link-reservation").iterator();

            //서울/경기/인천/강원/대전,충청/대구/부산,울산/경상/광주,전라,제주 - 광역 정보 가져오기
            Elements elements2 = doc_theaters.getElementsByAttributeValue("class", "sect-city");
            Iterator<Element> region = elements2.select("li.on").iterator();

            while(title.hasNext()){
                String newTitle = title.next().text();
                int newRank = Integer.parseInt(rank.next().text().replace("No.",""));
                float newScore = Float.parseFloat(score.next().text().replace("예매율", "").replace("%", ""));
                String newCode = link.next().attr("href").replaceAll("[^0-9]", "").substring(0,8);

                CGVMovieInfo newMovie = new CGVMovieInfo(newTitle, newRank, newScore, GoldenEgg.next().text(), newCode);
                Movies.add(newMovie);
            }

            while(region.hasNext()){
                System.out.println(region.next().text());
            }

        }catch(IOException e){
            e.printStackTrace();
        }

        for (CGVMovieInfo elem : Movies) {
            //elem.printMovieInfo();
            System.out.println(elem.getRank() + " : " + elem.getTitle());
        }

        System.out.print("예매하고 싶은 영화의 순위를 입력하세요 : ");
        int inputRank = scanner.nextInt();
        try{
            Desktop.getDesktop().browse(new URI(Movies.get(inputRank - 1).getLink()));
        }
        catch(IndexOutOfBoundsException | URISyntaxException | IOException e){
            System.out.println(e.getClass());
        }
    }
}