Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.IOException; | |
b5e9855b | 4 | import java.io.UnsupportedEncodingException; |
c4b18c94 | 5 | import java.net.MalformedURLException; |
08fe2e33 | 6 | import java.net.URL; |
b5e9855b | 7 | import java.net.URLDecoder; |
ce297a79 | 8 | import java.util.AbstractMap; |
08fe2e33 | 9 | import java.util.ArrayList; |
8ac3d099 | 10 | import java.util.Date; |
b5e9855b | 11 | import java.util.LinkedList; |
08fe2e33 NR |
12 | import java.util.List; |
13 | import java.util.Map.Entry; | |
8ac3d099 | 14 | |
5cf61f35 NR |
15 | import org.json.JSONArray; |
16 | import org.json.JSONException; | |
17 | import org.json.JSONObject; | |
8ac3d099 NR |
18 | import org.jsoup.helper.DataUtil; |
19 | import org.jsoup.nodes.Document; | |
20 | import org.jsoup.nodes.Element; | |
08fe2e33 NR |
21 | |
22 | import be.nikiroo.fanfix.Instance; | |
5cf61f35 | 23 | import be.nikiroo.fanfix.bundles.Config; |
68686a37 | 24 | import be.nikiroo.fanfix.data.MetaData; |
16a81ef7 | 25 | import be.nikiroo.utils.Image; |
3b2b638f | 26 | import be.nikiroo.utils.Progress; |
08fe2e33 | 27 | import be.nikiroo.utils.StringUtils; |
5cf61f35 | 28 | import be.nikiroo.utils.Version; |
08fe2e33 NR |
29 | |
30 | /** | |
8ac3d099 NR |
31 | * Support class for <a href="http://e621.net/">e621.net</a> and |
32 | * <a href="http://e926.net/">e926.net</a>, a Furry website supporting comics, | |
08fe2e33 NR |
33 | * including some of MLP. |
34 | * <p> | |
35 | * <a href="http://e926.net/">e926.net</a> only shows the "clean" images and | |
36 | * comics, but it can be difficult to browse. | |
37 | * | |
38 | * @author niki | |
39 | */ | |
8ac3d099 | 40 | class E621 extends BasicSupport { |
08fe2e33 NR |
41 | @Override |
42 | protected boolean supports(URL url) { | |
43 | String host = url.getHost(); | |
44 | if (host.startsWith("www.")) { | |
45 | host = host.substring("www.".length()); | |
46 | } | |
47 | ||
5cf61f35 NR |
48 | return ("e621.net".equals(host) || "e926.net".equals(host)) |
49 | && (isPool(url) || isSearchOrSet(url)); | |
08fe2e33 NR |
50 | } |
51 | ||
52 | @Override | |
53 | protected boolean isHtml() { | |
54 | return true; | |
55 | } | |
56 | ||
8ac3d099 NR |
57 | @Override |
58 | protected MetaData getMeta() throws IOException { | |
59 | MetaData meta = new MetaData(); | |
b5e9855b | 60 | |
8ac3d099 NR |
61 | meta.setTitle(getTitle()); |
62 | meta.setAuthor(getAuthor()); | |
bff19b54 | 63 | meta.setDate(bsHelper.formatDate(getDate())); |
8ac3d099 | 64 | meta.setTags(getTags()); |
8ac3d099 | 65 | meta.setUrl(getSource().toString()); |
8ac3d099 NR |
66 | meta.setUuid(getSource().toString()); |
67 | meta.setLuid(""); | |
68 | meta.setLang("en"); | |
69 | meta.setSubject("Furry"); | |
8ac3d099 NR |
70 | meta.setImageDocument(true); |
71 | meta.setCover(getCover()); | |
72 | meta.setFakeCover(true); | |
595dfa7a | 73 | |
8ac3d099 | 74 | return meta; |
595dfa7a NR |
75 | } |
76 | ||
8ac3d099 NR |
77 | @Override |
78 | protected String getDesc() throws IOException { | |
79 | if (isSearchOrSet(getSource())) { | |
b5e9855b | 80 | StringBuilder builder = new StringBuilder(); |
a8a7222f | 81 | builder.append("<div>"); |
5cf61f35 | 82 | builder.append("A collection of images from ") |
a8a7222f NR |
83 | .append(getSource().getHost()) // |
84 | .append("<br/>\n") // | |
85 | .append(" Time of creation: " | |
5cf61f35 | 86 | + StringUtils.fromTime(new Date().getTime())) |
a8a7222f NR |
87 | .append("<br/>\n") // |
88 | .append(" tTags: ");// | |
8ac3d099 | 89 | for (String tag : getTags()) { |
a8a7222f NR |
90 | builder.append( |
91 | "\n<br/> ") | |
92 | .append(tag); | |
b5e9855b | 93 | } |
a8a7222f | 94 | builder.append("\n</div>"); |
b5e9855b NR |
95 | |
96 | return builder.toString(); | |
97 | } | |
98 | ||
8ac3d099 NR |
99 | if (isPool(getSource())) { |
100 | Element el = getSourceNode().getElementById("description"); | |
101 | if (el != null) { | |
a8a7222f | 102 | return el.html(); |
08fe2e33 NR |
103 | } |
104 | } | |
105 | ||
106 | return null; | |
107 | } | |
108 | ||
08fe2e33 | 109 | @Override |
5cf61f35 | 110 | protected List<Entry<String, URL>> getChapters(Progress pg) |
8ac3d099 | 111 | throws IOException { |
5cf61f35 NR |
112 | int i = 1; |
113 | String jsonUrl = getJsonUrl(); | |
114 | if (jsonUrl != null) { | |
115 | for (i = 1; true; i++) { | |
116 | if (i > 1) { | |
117 | try { | |
118 | // The API does not accept more than 2 request per sec, | |
119 | // and asks us to limit at one per sec when possible | |
120 | Thread.sleep(1000); | |
121 | } catch (InterruptedException e) { | |
122 | } | |
123 | } | |
b5e9855b | 124 | |
b5e9855b | 125 | try { |
5cf61f35 NR |
126 | JSONObject json = getJson(jsonUrl + "&page=" + i, false); |
127 | if (!json.has("posts")) | |
b5e9855b | 128 | break; |
5cf61f35 NR |
129 | JSONArray posts = json.getJSONArray("posts"); |
130 | if (posts.isEmpty()) | |
131 | break; | |
132 | } catch (Exception e) { | |
133 | e.printStackTrace(); | |
b5e9855b | 134 | } |
b5e9855b | 135 | } |
5cf61f35 NR |
136 | |
137 | // The last page was empty: | |
138 | i--; | |
139 | } | |
140 | ||
141 | // The pages and images are in reverse order on /posts/ | |
142 | List<Entry<String, URL>> chapters = new LinkedList<Entry<String, URL>>(); | |
143 | for (int page = i; page > 0; page--) { | |
144 | chapters.add(new AbstractMap.SimpleEntry<String, URL>( | |
145 | "Page " + Integer.toString(i - page + 1), | |
146 | new URL(jsonUrl + "&page=" + page))); | |
b5e9855b NR |
147 | } |
148 | ||
5cf61f35 | 149 | return chapters; |
b5e9855b NR |
150 | } |
151 | ||
8ac3d099 | 152 | @Override |
5cf61f35 NR |
153 | protected String getChapterContent(URL chapUrl, int number, Progress pg) |
154 | throws IOException { | |
8ac3d099 | 155 | StringBuilder builder = new StringBuilder(); |
5cf61f35 NR |
156 | |
157 | JSONObject json = getJson(chapUrl, false); | |
158 | JSONArray postsArr = json.getJSONArray("posts"); | |
159 | ||
160 | // The pages and images are in reverse order on /posts/ | |
161 | List<JSONObject> posts = new ArrayList<JSONObject>(postsArr.length()); | |
162 | for (int i = postsArr.length() - 1; i >= 0; i--) { | |
163 | Object o = postsArr.get(i); | |
164 | if (o instanceof JSONObject) | |
165 | posts.add((JSONObject) o); | |
75002fcc | 166 | } |
5cf61f35 NR |
167 | |
168 | for (JSONObject post : posts) { | |
169 | if (!post.has("file")) | |
170 | continue; | |
171 | JSONObject file = post.getJSONObject("file"); | |
172 | if (!file.has("url")) | |
173 | continue; | |
174 | ||
175 | try { | |
176 | String url = file.getString("url"); | |
177 | builder.append("["); | |
178 | builder.append(url); | |
179 | builder.append("]<br/>"); | |
180 | } catch (JSONException e) { | |
181 | // Can be NULL if filtered | |
182 | // When the value is NULL, we get an exception | |
183 | // but the "has" method still returns true | |
a3d0728c NR |
184 | Instance.getInstance().getTraceHandler() |
185 | .error("Cannot get image for chapter " + number + " of " | |
186 | + getSource()); | |
5cf61f35 | 187 | } |
8ac3d099 NR |
188 | } |
189 | ||
190 | return builder.toString(); | |
191 | } | |
192 | ||
193 | @Override | |
194 | protected URL getCanonicalUrl(URL source) { | |
8fbfa934 NR |
195 | // Convert search-pools into proper pools |
196 | if (source.getPath().equals("/posts") && source.getQuery() != null | |
197 | && source.getQuery().startsWith("tags=pool%3A")) { | |
198 | String poolNumber = source.getQuery() | |
199 | .substring("tags=pool%3A".length()); | |
200 | try { | |
201 | Integer.parseInt(poolNumber); | |
202 | String base = source.getProtocol() + "://" + source.getHost(); | |
203 | if (source.getPort() != -1) { | |
204 | base = base + ":" + source.getPort(); | |
205 | } | |
42cdf6f0 | 206 | source = new URL(base + "/pools/" + poolNumber); |
8fbfa934 | 207 | } catch (NumberFormatException e) { |
36c35b92 | 208 | // Not a simple pool, skip |
8fbfa934 NR |
209 | } catch (MalformedURLException e) { |
210 | // Cannot happen | |
211 | } | |
212 | } | |
5cf61f35 | 213 | |
8ac3d099 NR |
214 | if (isSetOriginalUrl(source)) { |
215 | try { | |
5cf61f35 NR |
216 | Document doc = DataUtil.load(Instance.getInstance().getCache() |
217 | .open(source, this, false), "UTF-8", source.toString()); | |
218 | for (Element shortname : doc | |
219 | .getElementsByClass("set-shortname")) { | |
8ac3d099 NR |
220 | for (Element el : shortname.getElementsByTag("a")) { |
221 | if (!el.attr("href").isEmpty()) | |
222 | return new URL(el.absUrl("href")); | |
08fe2e33 NR |
223 | } |
224 | } | |
8ac3d099 | 225 | } catch (IOException e) { |
d66deb8d | 226 | Instance.getInstance().getTraceHandler().error(e); |
08fe2e33 NR |
227 | } |
228 | } | |
229 | ||
c4b18c94 NR |
230 | if (isPool(source)) { |
231 | try { | |
5cf61f35 NR |
232 | return new URL( |
233 | source.toString().replace("/pool/show/", "/pools/")); | |
c4b18c94 NR |
234 | } catch (MalformedURLException e) { |
235 | } | |
236 | } | |
237 | ||
8ac3d099 NR |
238 | return super.getCanonicalUrl(source); |
239 | } | |
240 | ||
8ac3d099 NR |
241 | private String getTitle() { |
242 | String title = ""; | |
243 | ||
244 | Element el = getSourceNode().getElementsByTag("title").first(); | |
245 | if (el != null) { | |
246 | title = el.text().trim(); | |
08fe2e33 NR |
247 | } |
248 | ||
36c35b92 | 249 | for (String s : new String[] { "e621", "-", "e621", "Pool", "-" }) { |
8ac3d099 NR |
250 | if (title.startsWith(s)) { |
251 | title = title.substring(s.length()).trim(); | |
08fe2e33 | 252 | } |
8ac3d099 NR |
253 | if (title.endsWith(s)) { |
254 | title = title.substring(0, title.length() - s.length()).trim(); | |
255 | } | |
08fe2e33 NR |
256 | } |
257 | ||
8ac3d099 NR |
258 | if (isSearchOrSet(getSource())) { |
259 | title = title.isEmpty() ? "e621" : "[e621] " + title; | |
260 | } | |
5cf61f35 | 261 | |
8ac3d099 | 262 | return title; |
08fe2e33 | 263 | } |
b5e9855b | 264 | |
5cf61f35 NR |
265 | private String getAuthor() { |
266 | List<String> list = new ArrayList<String>(); | |
267 | String jsonUrl = getJsonUrl(); | |
268 | if (jsonUrl != null) { | |
269 | try { | |
270 | JSONObject json = getJson(jsonUrl, false); | |
271 | JSONArray posts = json.getJSONArray("posts"); | |
272 | for (Object obj : posts) { | |
273 | if (!(obj instanceof JSONObject)) | |
274 | continue; | |
275 | ||
276 | JSONObject post = (JSONObject) obj; | |
277 | if (!post.has("tags")) | |
278 | continue; | |
279 | ||
280 | JSONObject tags = post.getJSONObject("tags"); | |
281 | if (!tags.has("artist")) | |
282 | continue; | |
283 | ||
284 | JSONArray artists = tags.getJSONArray("artist"); | |
285 | for (Object artist : artists) { | |
286 | if (list.contains(artist.toString())) | |
287 | continue; | |
288 | ||
289 | list.add(artist.toString()); | |
8ac3d099 | 290 | } |
9948521d | 291 | } |
5cf61f35 NR |
292 | } catch (Exception e) { |
293 | e.printStackTrace(); | |
8ac3d099 NR |
294 | } |
295 | } | |
296 | ||
5cf61f35 NR |
297 | StringBuilder builder = new StringBuilder(); |
298 | for (String artist : list) { | |
299 | if (builder.length() > 0) { | |
300 | builder.append(", "); | |
301 | } | |
302 | builder.append(artist); | |
303 | } | |
8ac3d099 | 304 | |
5cf61f35 NR |
305 | return builder.toString(); |
306 | } | |
8ac3d099 | 307 | |
5cf61f35 NR |
308 | private String getDate() { |
309 | String jsonUrl = getJsonUrl(); | |
310 | if (jsonUrl != null) { | |
311 | try { | |
312 | JSONObject json = getJson(jsonUrl, false); | |
313 | JSONArray posts = json.getJSONArray("posts"); | |
314 | for (Object obj : posts) { | |
315 | if (!(obj instanceof JSONObject)) | |
316 | continue; | |
8d1a4fd2 | 317 | |
5cf61f35 NR |
318 | JSONObject post = (JSONObject) obj; |
319 | if (!post.has("created_at")) | |
320 | continue; | |
321 | ||
322 | return post.getString("created_at"); | |
8d1a4fd2 | 323 | } |
5cf61f35 NR |
324 | } catch (Exception e) { |
325 | e.printStackTrace(); | |
8d1a4fd2 | 326 | } |
8ac3d099 | 327 | } |
9948521d | 328 | |
5cf61f35 | 329 | return ""; |
8ac3d099 NR |
330 | } |
331 | ||
332 | // no tags for pools | |
333 | private List<String> getTags() { | |
334 | List<String> tags = new ArrayList<String>(); | |
335 | if (isSearchOrSet(getSource())) { | |
336 | String str = getTagsFromUrl(getSource()); | |
337 | for (String tag : str.split("\\+")) { | |
9b863b20 | 338 | try { |
8ac3d099 NR |
339 | tags.add(URLDecoder.decode(tag.trim(), "UTF-8").trim()); |
340 | } catch (UnsupportedEncodingException e) { | |
9b863b20 NR |
341 | } |
342 | } | |
343 | } | |
9948521d | 344 | |
8ac3d099 NR |
345 | return tags; |
346 | } | |
347 | ||
5cf61f35 NR |
348 | // returns "xxx+ddd+ggg" if "tags=xxx+ddd+ggg" was present in the query |
349 | private String getTagsFromUrl(URL url) { | |
350 | String tags = url == null ? "" : url.getQuery(); | |
351 | int pos = tags.indexOf("tags="); | |
352 | ||
353 | if (pos >= 0) { | |
354 | tags = tags.substring(pos).substring("tags=".length()); | |
355 | } else { | |
356 | return ""; | |
357 | } | |
358 | ||
359 | pos = tags.indexOf('&'); | |
360 | if (pos > 0) { | |
361 | tags = tags.substring(0, pos); | |
362 | } | |
363 | pos = tags.indexOf('/'); | |
364 | if (pos > 0) { | |
365 | tags = tags.substring(0, pos); | |
366 | } | |
367 | ||
368 | return tags; | |
369 | } | |
370 | ||
8ac3d099 NR |
371 | private Image getCover() throws IOException { |
372 | Image image = null; | |
373 | List<Entry<String, URL>> chapters = getChapters(null); | |
374 | if (!chapters.isEmpty()) { | |
12c180fc NR |
375 | URL chap1Url = chapters.get(0).getValue(); |
376 | String imgsChap1 = getChapterContent(chap1Url, 1, null); | |
377 | if (!imgsChap1.isEmpty()) { | |
378 | imgsChap1 = imgsChap1.split("]")[0].substring(1).trim(); | |
379 | image = bsImages.getImage(this, new URL(imgsChap1)); | |
380 | } | |
8ac3d099 NR |
381 | } |
382 | ||
383 | return image; | |
384 | } | |
385 | ||
5cf61f35 NR |
386 | // always /posts.json/ url |
387 | private String getJsonUrl() { | |
388 | String url = null; | |
389 | if (isSearchOrSet(getSource())) { | |
390 | url = getSource().toString().replace("/posts", "/posts.json"); | |
391 | } | |
392 | ||
393 | if (isPool(getSource())) { | |
394 | String poolNumber = getSource().getPath() | |
395 | .substring("/pools/".length()); | |
396 | url = "https://e621.net/posts.json" + "?tags=pool%3A" + poolNumber; | |
397 | } | |
398 | ||
399 | if (url != null) { | |
400 | // Note: one way to override the blacklist | |
401 | String login = Instance.getInstance().getConfig() | |
402 | .getString(Config.LOGIN_E621_LOGIN); | |
403 | String apk = Instance.getInstance().getConfig() | |
404 | .getString(Config.LOGIN_E621_APIKEY); | |
405 | ||
406 | if (login != null && !login.isEmpty() && apk != null | |
407 | && !apk.isEmpty()) { | |
408 | url = String.format("%s&login=%s&api_key=%s&_client=%s", url, | |
409 | login, apk, "fanfix-" + Version.getCurrentVersion()); | |
410 | } | |
411 | } | |
412 | ||
413 | return url; | |
414 | } | |
415 | ||
8ac3d099 NR |
416 | // note: will be removed at getCanonicalUrl() |
417 | private boolean isSetOriginalUrl(URL originalUrl) { | |
418 | return originalUrl.getPath().startsWith("/post_sets/"); | |
9b863b20 NR |
419 | } |
420 | ||
b5e9855b | 421 | private boolean isPool(URL url) { |
5cf61f35 NR |
422 | return url.getPath().startsWith("/pools/") |
423 | || url.getPath().startsWith("/pool/show/"); | |
b5e9855b NR |
424 | } |
425 | ||
8ac3d099 NR |
426 | // set will be renamed into search by canonical url |
427 | private boolean isSearchOrSet(URL url) { | |
428 | return | |
429 | // search: | |
430 | (url.getPath().equals("/posts") && url.getQuery().contains("tags=")) | |
431 | // or set: | |
432 | || isSetOriginalUrl(url); | |
b5e9855b | 433 | } |
08fe2e33 | 434 | } |