CGVExample.java
4.24 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
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 = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것.
Document doc = null;
ArrayList<CGVMovieInfo> Movies = new ArrayList<>();
try {
doc = Jsoup.connect(url).get();
//Top 19 까지
Elements elements1 = doc.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();
//Top 19 이후
//Elements elements2 = doc.select("div.sect-movie-chart");
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);
}
}catch(IOException e){
e.printStackTrace();
}
for (CGVMovieInfo elem : Movies) {
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());
}
}
}