14
14
import java .net .URL ;
15
15
import java .time .LocalDateTime ;
16
16
import java .util .List ;
17
+ import java .util .Objects ;
17
18
import java .util .Optional ;
18
19
import java .util .stream .Collectors ;
19
20
@@ -38,6 +39,11 @@ public List<Film> getFilmListFromDouban() {
38
39
try {
39
40
Document document = Jsoup .parse (new URL (RECENT_URL ), 30000 );
40
41
Element element = document .getElementById (NOWPLAYING );
42
+
43
+ if (Objects .isNull (element )) {
44
+ return filmList ;
45
+ }
46
+
41
47
Elements elements = element .getElementsByClass ("list-item" );
42
48
elements .forEach (item -> filmList .add (Film .builder ()
43
49
.movieId (Long .valueOf (item .attr ("id" )))
@@ -67,6 +73,9 @@ public List<Film> getTopFilmListFromDouban() {
67
73
try {
68
74
Document document = Jsoup .parse (new URL (TOP_DOUBAN_URL .concat (String .valueOf (i * gap ))), 30000 );
69
75
Element gridView = document .getElementById ("content" );
76
+ if (Objects .isNull (gridView )) {
77
+ return filmList ;
78
+ }
70
79
Elements liElements = gridView .getElementsByTag ("li" );
71
80
filmList .addAll (liElements .stream ()
72
81
.map (item -> {
@@ -106,19 +115,27 @@ private Film getFilmDetailFromUrl(String url) {
106
115
}
107
116
try {
108
117
Document document = Jsoup .parse (new URL (url ), 30000 );
118
+ String summary = null ;
119
+ String genres = null ;
109
120
Element summaryElement = document .getElementById ("link-report" );
110
- Elements summaryElements = summaryElement .getElementsByTag ("span" );
121
+ if (Objects .nonNull (summaryElement )) {
122
+ Elements summaryElements = summaryElement .getElementsByTag ("span" );
123
+ summary = StringUtils .strip (summaryElements .get (0 ).text ());
124
+ }
111
125
112
126
Element infoElement = document .getElementById ("info" );
113
- Elements spanElements = infoElement .getElementsByTag ("span" );
114
- List <String > genreList = spanElements .stream ()
115
- .filter (item -> "v:genre" .equals (item .attr (PROPERTY )))
116
- .map (Element ::text )
117
- .collect (Collectors .toList ());
127
+ if (Objects .nonNull (infoElement )) {
128
+ Elements spanElements = infoElement .getElementsByTag ("span" );
129
+ List <String > genreList = spanElements .stream ()
130
+ .filter (item -> "v:genre" .equals (item .attr (PROPERTY )))
131
+ .map (Element ::text )
132
+ .collect (Collectors .toList ());
133
+ genres = StringUtils .join (genreList , ConstantUtils .SEPARATOR );
134
+ }
118
135
119
136
return Film .builder ()
120
- .summary (( StringUtils . strip ( summaryElements . get ( 0 ). text ())) )
121
- .genres (StringUtils . join ( genreList , ConstantUtils . SEPARATOR ) )
137
+ .summary (summary )
138
+ .genres (genres )
122
139
.build ();
123
140
} catch (Exception e ) {
124
141
log .error ("failed to get detail from url: {}" , url , e );
@@ -132,52 +149,66 @@ private Film getTopFilmDetailFromUrl(String url) {
132
149
}
133
150
try {
134
151
Document document = Jsoup .parse (new URL (url ), 30000 );
152
+ String summary = null ;
153
+ String genres ;
135
154
Element summaryElement = document .getElementById ("link-report" );
136
- Elements summaryElements = summaryElement .getElementsByTag ("span" );
137
-
138
- Element infoElement = document .getElementById ("info" );
139
- Elements spanElements = infoElement .getElementsByTag ("span" );
140
- List <String > genreList = spanElements .stream ()
141
- .filter (item -> "v:genre" .equals (item .attr (PROPERTY )))
142
- .map (Element ::text )
143
- .collect (Collectors .toList ());
144
-
145
- Elements aElements = infoElement .getElementsByTag ("a" );
146
- List <String > directorList = aElements .stream ()
147
- .filter (item -> "v:directedBy" .equals (item .attr ("rel" )))
148
- .map (Element ::text )
149
- .collect (Collectors .toList ());
150
-
151
- List <String > actorList = aElements .stream ()
152
- .filter (item -> "v:starring" .equals (item .attr ("rel" )))
153
- .map (Element ::text )
154
- .limit (5 )
155
- .collect (Collectors .toList ());
156
-
157
- Optional <Integer > movieYear = spanElements .stream ()
158
- .filter (item -> "v:initialReleaseDate" .equals (item .attr (PROPERTY )))
159
- .map (Element ::text )
160
- .map (item -> Integer .parseInt (item .substring (0 , 4 )))
161
- .limit (1 )
162
- .findFirst ();
155
+ if (Objects .nonNull (summaryElement )) {
156
+ Elements summaryElements = summaryElement .getElementsByTag ("span" );
157
+ summary = StringUtils .strip (summaryElements .get (0 ).text ());
158
+ }
163
159
164
160
Element ratingElement = document .getElementById ("interest_sectl" );
165
- Elements strongElements = ratingElement .getElementsByTag ("strong" );
166
- Optional <Double > rating = strongElements .stream ()
167
- .filter (item -> "v:average" .equals (item .attr (PROPERTY )))
168
- .map (Element ::text )
169
- .map (Double ::parseDouble )
170
- .limit (1 )
171
- .findFirst ();
161
+ Optional <Double > rating = Optional .of (0d );
162
+ if (Objects .nonNull (ratingElement )) {
163
+ Elements strongElements = ratingElement .getElementsByTag ("strong" );
164
+ rating = strongElements .stream ()
165
+ .filter (item -> "v:average" .equals (item .attr (PROPERTY )))
166
+ .map (Element ::text )
167
+ .map (Double ::parseDouble )
168
+ .limit (1 )
169
+ .findFirst ();
170
+ }
172
171
173
- return Film .builder ()
174
- .summary ((StringUtils .strip (summaryElements .get (0 ).text ())))
175
- .directors (StringUtils .join (directorList , "," ))
176
- .genres (StringUtils .join (genreList , ConstantUtils .SEPARATOR ))
177
- .casts (StringUtils .join (actorList , "," ))
178
- .movieYear (movieYear .orElse (0 ))
172
+ Film film = Film .builder ()
173
+ .summary (summary )
179
174
.rating (rating .orElse (0d ))
180
175
.build ();
176
+
177
+ Element infoElement = document .getElementById ("info" );
178
+ if (Objects .nonNull (infoElement )) {
179
+ Elements spanElements = infoElement .getElementsByTag ("span" );
180
+ List <String > genreList = spanElements .stream ()
181
+ .filter (item -> "v:genre" .equals (item .attr (PROPERTY )))
182
+ .map (Element ::text )
183
+ .collect (Collectors .toList ());
184
+ genres = StringUtils .join (genreList , ConstantUtils .SEPARATOR );
185
+
186
+ Elements aElements = infoElement .getElementsByTag ("a" );
187
+ List <String > directorList = aElements .stream ()
188
+ .filter (item -> "v:directedBy" .equals (item .attr ("rel" )))
189
+ .map (Element ::text )
190
+ .collect (Collectors .toList ());
191
+
192
+ List <String > actorList = aElements .stream ()
193
+ .filter (item -> "v:starring" .equals (item .attr ("rel" )))
194
+ .map (Element ::text )
195
+ .limit (5 )
196
+ .collect (Collectors .toList ());
197
+
198
+ Optional <Integer > movieYear = spanElements .stream ()
199
+ .filter (item -> "v:initialReleaseDate" .equals (item .attr (PROPERTY )))
200
+ .map (Element ::text )
201
+ .map (item -> Integer .parseInt (item .substring (0 , 4 )))
202
+ .limit (1 )
203
+ .findFirst ();
204
+
205
+ film .setGenres (genres );
206
+ film .setDirectors (StringUtils .join (directorList , ConstantUtils .SEPARATOR ));
207
+ film .setCasts (StringUtils .join (actorList , ConstantUtils .SEPARATOR ));
208
+ film .setMovieYear (movieYear .orElse (0 ));
209
+ }
210
+
211
+ return film ;
181
212
} catch (Exception e ) {
182
213
log .error ("failed to get detail from url: {}" , url , e );
183
214
return null ;
0 commit comments