CGVExample.java
5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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());
}
}
}