Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
b5e9855b | 5 | import java.io.UnsupportedEncodingException; |
c4b18c94 | 6 | import java.net.MalformedURLException; |
08fe2e33 | 7 | import java.net.URL; |
b5e9855b | 8 | import java.net.URLDecoder; |
ce297a79 | 9 | import java.util.AbstractMap; |
08fe2e33 | 10 | import java.util.ArrayList; |
9b863b20 | 11 | import java.util.Collections; |
8ac3d099 | 12 | import java.util.Date; |
b5e9855b | 13 | import java.util.LinkedList; |
08fe2e33 NR |
14 | import java.util.List; |
15 | import java.util.Map.Entry; | |
8ac3d099 NR |
16 | |
17 | import org.jsoup.helper.DataUtil; | |
18 | import org.jsoup.nodes.Document; | |
19 | import org.jsoup.nodes.Element; | |
08fe2e33 NR |
20 | |
21 | import be.nikiroo.fanfix.Instance; | |
68686a37 | 22 | import be.nikiroo.fanfix.data.MetaData; |
8ac3d099 | 23 | import be.nikiroo.utils.IOUtils; |
16a81ef7 | 24 | import be.nikiroo.utils.Image; |
3b2b638f | 25 | import be.nikiroo.utils.Progress; |
08fe2e33 NR |
26 | import be.nikiroo.utils.StringUtils; |
27 | ||
28 | /** | |
8ac3d099 NR |
29 | * Support class for <a href="http://e621.net/">e621.net</a> and |
30 | * <a href="http://e926.net/">e926.net</a>, a Furry website supporting comics, | |
08fe2e33 NR |
31 | * including some of MLP. |
32 | * <p> | |
33 | * <a href="http://e926.net/">e926.net</a> only shows the "clean" images and | |
34 | * comics, but it can be difficult to browse. | |
35 | * | |
36 | * @author niki | |
37 | */ | |
8ac3d099 | 38 | class E621 extends BasicSupport { |
08fe2e33 NR |
39 | @Override |
40 | protected boolean supports(URL url) { | |
41 | String host = url.getHost(); | |
42 | if (host.startsWith("www.")) { | |
43 | host = host.substring("www.".length()); | |
44 | } | |
45 | ||
8ac3d099 | 46 | return ("e621.net".equals(host) || "e926.net".equals(host)) && (isPool(url) || isSearchOrSet(url)); |
08fe2e33 NR |
47 | } |
48 | ||
49 | @Override | |
50 | protected boolean isHtml() { | |
51 | return true; | |
52 | } | |
53 | ||
8ac3d099 NR |
54 | @Override |
55 | protected MetaData getMeta() throws IOException { | |
56 | MetaData meta = new MetaData(); | |
b5e9855b | 57 | |
8ac3d099 NR |
58 | meta.setTitle(getTitle()); |
59 | meta.setAuthor(getAuthor()); | |
60 | meta.setDate(""); | |
61 | meta.setTags(getTags()); | |
62 | meta.setSource(getType().getSourceName()); | |
63 | meta.setUrl(getSource().toString()); | |
64 | meta.setPublisher(getType().getSourceName()); | |
65 | meta.setUuid(getSource().toString()); | |
66 | meta.setLuid(""); | |
67 | meta.setLang("en"); | |
68 | meta.setSubject("Furry"); | |
69 | meta.setType(getType().toString()); | |
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(); |
8ac3d099 NR |
81 | builder.append("A collection of images from ").append(getSource().getHost()).append("\n") // |
82 | .append("\tTime of creation: " + StringUtils.fromTime(new Date().getTime())).append("\n") // | |
83 | .append("\tTags: ");// | |
84 | for (String tag : getTags()) { | |
85 | builder.append("\t\t").append(tag); | |
b5e9855b NR |
86 | } |
87 | ||
88 | return builder.toString(); | |
89 | } | |
90 | ||
8ac3d099 NR |
91 | if (isPool(getSource())) { |
92 | Element el = getSourceNode().getElementById("description"); | |
93 | if (el != null) { | |
94 | return el.text(); | |
08fe2e33 NR |
95 | } |
96 | } | |
97 | ||
98 | return null; | |
99 | } | |
100 | ||
08fe2e33 | 101 | @Override |
8ac3d099 NR |
102 | protected List<Entry<String, URL>> getChapters(Progress pg) throws IOException { |
103 | if (isPool(getSource())) { | |
104 | String baseUrl = "https://e621.net/" + getSource().getPath() + "?page="; | |
105 | return getChapters(getSource(), pg, baseUrl, ""); | |
106 | } else if (isSearchOrSet(getSource())) { | |
107 | String baseUrl = "https://e621.net/posts/?page="; | |
108 | String search = "&tags=" + getTagsFromUrl(getSource()); | |
109 | return getChapters(getSource(), pg, baseUrl, search); | |
b5e9855b NR |
110 | } |
111 | ||
112 | return new LinkedList<Entry<String, URL>>(); | |
113 | } | |
114 | ||
8ac3d099 NR |
115 | private List<Entry<String, URL>> getChapters(URL source, Progress pg, String baseUrl, String parameters) |
116 | throws IOException { | |
b5e9855b NR |
117 | List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>(); |
118 | ||
b5e9855b NR |
119 | if (source.getHost().contains("e926")) { |
120 | baseUrl = baseUrl.replace("e621", "e926"); | |
121 | } | |
122 | ||
123 | for (int i = 1; true; i++) { | |
8ac3d099 | 124 | URL url = new URL(baseUrl + i + parameters); |
b5e9855b NR |
125 | try { |
126 | InputStream pageI = Instance.getCache().open(url, this, false); | |
127 | try { | |
8ac3d099 | 128 | if (IOUtils.readSmallStream(pageI).contains("Nobody here but us chickens!")) { |
b5e9855b | 129 | break; |
8ac3d099 NR |
130 | } |
131 | urls.add(new AbstractMap.SimpleEntry<String, URL>("Page " + Integer.toString(i), url)); | |
b5e9855b NR |
132 | } finally { |
133 | pageI.close(); | |
134 | } | |
135 | } catch (Exception e) { | |
136 | break; | |
137 | } | |
138 | } | |
139 | ||
9b863b20 NR |
140 | // They are sorted in reverse order on the website |
141 | Collections.reverse(urls); | |
b5e9855b NR |
142 | return urls; |
143 | } | |
144 | ||
8ac3d099 NR |
145 | @Override |
146 | protected String getChapterContent(URL chapUrl, int number, Progress pg) throws IOException { | |
147 | StringBuilder builder = new StringBuilder(); | |
148 | Document chapterNode = loadDocument(chapUrl); | |
149 | for (Element el : chapterNode.getElementsByTag("article")) { | |
150 | builder.append("["); | |
151 | builder.append(el.attr("data-file-url")); | |
152 | builder.append("]<br/>"); | |
153 | } | |
154 | ||
155 | return builder.toString(); | |
156 | } | |
157 | ||
158 | @Override | |
159 | protected URL getCanonicalUrl(URL source) { | |
160 | if (isSetOriginalUrl(source)) { | |
161 | try { | |
162 | Document doc = DataUtil.load(Instance.getCache().open(source, this, false), "UTF-8", source.toString()); | |
163 | for (Element shortname : doc.getElementsByClass("set-shortname")) { | |
164 | for (Element el : shortname.getElementsByTag("a")) { | |
165 | if (!el.attr("href").isEmpty()) | |
166 | return new URL(el.absUrl("href")); | |
08fe2e33 NR |
167 | } |
168 | } | |
8ac3d099 NR |
169 | } catch (IOException e) { |
170 | Instance.getTraceHandler().error(e); | |
08fe2e33 NR |
171 | } |
172 | } | |
173 | ||
c4b18c94 NR |
174 | if (isPool(source)) { |
175 | try { | |
176 | return new URL(source.toString().replace("/pool/show/", "/pools/")); | |
177 | } catch (MalformedURLException e) { | |
178 | } | |
179 | } | |
180 | ||
8ac3d099 NR |
181 | return super.getCanonicalUrl(source); |
182 | } | |
183 | ||
184 | // returns "xxx+ddd+ggg" if "tags=xxx+ddd+ggg" was present in the query | |
185 | private String getTagsFromUrl(URL url) { | |
186 | String tags = url == null ? "" : url.getQuery(); | |
187 | int pos = tags.indexOf("tags="); | |
188 | ||
189 | if (pos >= 0) { | |
190 | tags = tags.substring(pos).substring("tags=".length()); | |
191 | } else { | |
192 | return ""; | |
08fe2e33 NR |
193 | } |
194 | ||
8ac3d099 NR |
195 | pos = tags.indexOf('&'); |
196 | if (pos > 0) { | |
197 | tags = tags.substring(0, pos); | |
198 | } | |
199 | pos = tags.indexOf('/'); | |
200 | if (pos > 0) { | |
201 | tags = tags.substring(0, pos); | |
202 | } | |
203 | ||
204 | return tags; | |
08fe2e33 NR |
205 | } |
206 | ||
8ac3d099 NR |
207 | private String getTitle() { |
208 | String title = ""; | |
209 | ||
210 | Element el = getSourceNode().getElementsByTag("title").first(); | |
211 | if (el != null) { | |
212 | title = el.text().trim(); | |
08fe2e33 NR |
213 | } |
214 | ||
8ac3d099 NR |
215 | for (String s : new String[] { "e621", "-", "e621" }) { |
216 | if (title.startsWith(s)) { | |
217 | title = title.substring(s.length()).trim(); | |
08fe2e33 | 218 | } |
8ac3d099 NR |
219 | if (title.endsWith(s)) { |
220 | title = title.substring(0, title.length() - s.length()).trim(); | |
221 | } | |
222 | ||
08fe2e33 NR |
223 | } |
224 | ||
8ac3d099 NR |
225 | if (isSearchOrSet(getSource())) { |
226 | title = title.isEmpty() ? "e621" : "[e621] " + title; | |
227 | } | |
228 | return title; | |
08fe2e33 | 229 | } |
b5e9855b | 230 | |
8ac3d099 NR |
231 | private String getAuthor() throws IOException { |
232 | StringBuilder builder = new StringBuilder(); | |
233 | ||
234 | if (isSearchOrSet(getSource())) { | |
235 | for (Element el : getSourceNode().getElementsByClass("search-tag")) { | |
236 | if (el.attr("itemprop").equals("author")) { | |
237 | if (builder.length() > 0) { | |
238 | builder.append(", "); | |
239 | } | |
240 | builder.append(el.text().trim()); | |
9948521d | 241 | } |
8ac3d099 NR |
242 | } |
243 | } | |
244 | ||
245 | if (isPool(getSource())) { | |
246 | String desc = getDesc(); | |
247 | String descL = desc.toLowerCase(); | |
248 | ||
249 | if (descL.startsWith("by:") || descL.startsWith("by ")) { | |
250 | desc = desc.substring(3).trim(); | |
251 | desc = desc.split("\n")[0]; | |
252 | ||
253 | String tab[] = desc.split(" "); | |
254 | for (int i = 0; i < Math.min(tab.length, 5); i++) { | |
255 | if (tab[i].startsWith("http")) | |
256 | break; | |
257 | builder.append(" ").append(tab[i]); | |
9948521d | 258 | } |
8ac3d099 NR |
259 | } |
260 | } | |
9948521d | 261 | |
8ac3d099 NR |
262 | return builder.toString(); |
263 | } | |
264 | ||
265 | // no tags for pools | |
266 | private List<String> getTags() { | |
267 | List<String> tags = new ArrayList<String>(); | |
268 | if (isSearchOrSet(getSource())) { | |
269 | String str = getTagsFromUrl(getSource()); | |
270 | for (String tag : str.split("\\+")) { | |
9b863b20 | 271 | try { |
8ac3d099 NR |
272 | tags.add(URLDecoder.decode(tag.trim(), "UTF-8").trim()); |
273 | } catch (UnsupportedEncodingException e) { | |
9b863b20 NR |
274 | } |
275 | } | |
276 | } | |
9948521d | 277 | |
8ac3d099 NR |
278 | return tags; |
279 | } | |
280 | ||
281 | private Image getCover() throws IOException { | |
282 | Image image = null; | |
283 | List<Entry<String, URL>> chapters = getChapters(null); | |
284 | if (!chapters.isEmpty()) { | |
285 | URL url = chapters.get(0).getValue(); | |
286 | image = bsImages.getImage(this, url); | |
287 | } | |
288 | ||
289 | return image; | |
290 | } | |
291 | ||
292 | // note: will be removed at getCanonicalUrl() | |
293 | private boolean isSetOriginalUrl(URL originalUrl) { | |
294 | return originalUrl.getPath().startsWith("/post_sets/"); | |
9b863b20 NR |
295 | } |
296 | ||
b5e9855b | 297 | private boolean isPool(URL url) { |
c4b18c94 | 298 | return url.getPath().startsWith("/pools/") || url.getPath().startsWith("/pool/show/"); |
b5e9855b NR |
299 | } |
300 | ||
8ac3d099 NR |
301 | // set will be renamed into search by canonical url |
302 | private boolean isSearchOrSet(URL url) { | |
303 | return | |
304 | // search: | |
305 | (url.getPath().equals("/posts") && url.getQuery().contains("tags=")) | |
306 | // or set: | |
307 | || isSetOriginalUrl(url); | |
b5e9855b | 308 | } |
08fe2e33 | 309 | } |