Commit | Line | Data |
---|---|---|
158b372d NR |
1 | package be.nikiroo.fanfix.searchable; |
2 | ||
3 | import java.io.IOException; | |
e66c9078 | 4 | import java.io.InputStream; |
158b372d | 5 | import java.net.URL; |
e66c9078 NR |
6 | import java.net.URLEncoder; |
7 | import java.text.SimpleDateFormat; | |
158b372d | 8 | import java.util.ArrayList; |
e66c9078 | 9 | import java.util.Date; |
158b372d NR |
10 | import java.util.HashMap; |
11 | import java.util.List; | |
12 | import java.util.Map; | |
13 | ||
14 | import org.jsoup.nodes.Document; | |
15 | import org.jsoup.nodes.Element; | |
16 | import org.jsoup.select.Elements; | |
17 | ||
18 | import be.nikiroo.fanfix.Instance; | |
19 | import be.nikiroo.fanfix.bundles.StringId; | |
20 | import be.nikiroo.fanfix.data.MetaData; | |
21 | import be.nikiroo.fanfix.supported.SupportType; | |
e66c9078 | 22 | import be.nikiroo.utils.Image; |
596ed3d6 | 23 | import be.nikiroo.utils.StringUtils; |
158b372d NR |
24 | |
25 | /** | |
26 | * A {@link BasicSearchable} for Fanfiction.NET. | |
27 | * | |
28 | * @author niki | |
29 | */ | |
30 | class Fanfiction extends BasicSearchable { | |
76ec935e NR |
31 | static private String BASE_URL = "http://fanfiction.net/"; |
32 | ||
158b372d NR |
33 | /** |
34 | * Create a new {@link Fanfiction}. | |
35 | * | |
36 | * @param type | |
37 | * {@link SupportType#FANFICTION} | |
38 | */ | |
39 | public Fanfiction(SupportType type) { | |
40 | super(type); | |
41 | } | |
42 | ||
43 | @Override | |
44 | public List<SearchableTag> getTags() throws IOException { | |
45 | String storiesName = null; | |
46 | String crossoversName = null; | |
47 | Map<String, String> stories = new HashMap<String, String>(); | |
48 | Map<String, String> crossovers = new HashMap<String, String>(); | |
49 | ||
76ec935e | 50 | Document mainPage = load(BASE_URL, true); |
158b372d NR |
51 | Element menu = mainPage.getElementsByClass("dropdown").first(); |
52 | if (menu != null) { | |
53 | Element ul = menu.getElementsByClass("dropdown-menu").first(); | |
54 | if (ul != null) { | |
55 | Map<String, String> currentList = null; | |
56 | for (Element li : ul.getElementsByTag("li")) { | |
57 | if (li.hasClass("disabled")) { | |
58 | if (storiesName == null) { | |
59 | storiesName = li.text(); | |
60 | currentList = stories; | |
61 | } else { | |
62 | crossoversName = li.text(); | |
63 | currentList = crossovers; | |
64 | } | |
65 | } else if (currentList != null) { | |
66 | Element a = li.getElementsByTag("a").first(); | |
67 | if (a != null) { | |
68 | currentList.put(a.absUrl("href"), a.text()); | |
69 | } | |
70 | } | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | List<SearchableTag> tags = new ArrayList<SearchableTag>(); | |
76 | ||
77 | if (storiesName != null) { | |
76ec935e | 78 | SearchableTag tag = new SearchableTag(null, storiesName, false); |
158b372d | 79 | for (String id : stories.keySet()) { |
b3b9a1cd | 80 | tag.add(new SearchableTag(id, stories.get(id), false, false)); |
158b372d NR |
81 | } |
82 | tags.add(tag); | |
83 | } | |
84 | ||
85 | if (crossoversName != null) { | |
76ec935e | 86 | SearchableTag tag = new SearchableTag(null, crossoversName, false); |
158b372d | 87 | for (String id : crossovers.keySet()) { |
76ec935e | 88 | tag.add(new SearchableTag(id, crossovers.get(id), false, false)); |
158b372d NR |
89 | } |
90 | tags.add(tag); | |
91 | } | |
92 | ||
93 | return tags; | |
94 | } | |
95 | ||
96 | @Override | |
91b82a5c | 97 | public void fillTag(SearchableTag tag) throws IOException { |
158b372d NR |
98 | if (tag.getId() == null || tag.isComplete()) { |
99 | return; | |
100 | } | |
101 | ||
76ec935e | 102 | Document doc = load(tag.getId(), false); |
158b372d NR |
103 | Element list = doc.getElementById("list_output"); |
104 | if (list != null) { | |
105 | Element table = list.getElementsByTag("table").first(); | |
106 | if (table != null) { | |
107 | for (Element div : table.getElementsByTag("div")) { | |
108 | Element a = div.getElementsByTag("a").first(); | |
109 | Element span = div.getElementsByTag("span").first(); | |
110 | ||
111 | if (a != null) { | |
e66c9078 NR |
112 | String subid = a.absUrl("href"); |
113 | boolean crossoverSubtag = subid | |
114 | .contains("/crossovers/"); | |
115 | ||
116 | SearchableTag subtag = new SearchableTag(subid, | |
117 | a.text(), !crossoverSubtag, !crossoverSubtag); | |
118 | ||
158b372d NR |
119 | tag.add(subtag); |
120 | if (span != null) { | |
121 | String nr = span.text(); | |
122 | if (nr.startsWith("(")) { | |
123 | nr = nr.substring(1); | |
124 | } | |
125 | if (nr.endsWith(")")) { | |
126 | nr = nr.substring(0, nr.length() - 1); | |
127 | } | |
128 | nr = nr.trim(); | |
74a43961 NR |
129 | |
130 | // TODO: fix toNumber/fromNumber | |
b3b9a1cd | 131 | nr = nr.replaceAll("\\.[0-9]*", ""); |
74a43961 NR |
132 | |
133 | subtag.setCount(StringUtils.toNumber(nr)); | |
158b372d NR |
134 | } |
135 | } | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | tag.setComplete(true); | |
141 | } | |
142 | ||
143 | @Override | |
8ffc8b73 | 144 | public List<MetaData> search(String search, int page) throws IOException { |
e66c9078 | 145 | String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8"); |
124442f1 NR |
146 | String url = BASE_URL + "search/?ready=1&type=story&keywords=" |
147 | + encoded + "&ppage=" + page; | |
148 | ||
149 | return getStories(url, null, null); | |
150 | } | |
151 | ||
152 | @Override | |
153 | public int searchPages(String search) throws IOException { | |
154 | String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8"); | |
155 | String url = BASE_URL + "search/?ready=1&type=story&keywords=" | |
156 | + encoded; | |
157 | ||
158 | return getPages(load(url, false)); | |
158b372d NR |
159 | } |
160 | ||
161 | @Override | |
e66c9078 NR |
162 | public List<MetaData> search(SearchableTag tag, int page) |
163 | throws IOException { | |
158b372d NR |
164 | List<MetaData> metas = new ArrayList<MetaData>(); |
165 | ||
e66c9078 NR |
166 | String url = tag.getId(); |
167 | if (url != null) { | |
168 | if (page > 1) { | |
169 | int pos = url.indexOf("&p="); | |
170 | if (pos >= 0) { | |
171 | url = url.replaceAll("(.*\\&p=)[0-9]*(.*)", "$1\\" + page | |
172 | + "$2"); | |
173 | } else { | |
174 | url += "&p=" + page; | |
175 | } | |
176 | } | |
74a43961 | 177 | |
e66c9078 NR |
178 | Document doc = load(url, false); |
179 | ||
180 | // Update the pages number if needed | |
b3b9a1cd | 181 | if (tag.getPages() < 0 && tag.isLeaf()) { |
e66c9078 NR |
182 | tag.setPages(getPages(doc)); |
183 | } | |
184 | ||
185 | // Find out the full subjects (including parents) | |
186 | String subjects = ""; | |
187 | for (SearchableTag t = tag; t != null; t = t.getParent()) { | |
188 | if (!subjects.isEmpty()) { | |
189 | subjects += ", "; | |
190 | } | |
191 | subjects += t.getName(); | |
192 | } | |
193 | ||
194 | metas = getStories(url, doc, subjects); | |
195 | } | |
196 | ||
197 | return metas; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Return the number of pages in this stories result listing. | |
202 | * | |
203 | * @param doc | |
204 | * the document | |
205 | * | |
206 | * @return the number of pages or -1 if unknown | |
e66c9078 | 207 | */ |
8ffc8b73 | 208 | private int getPages(Document doc) { |
e66c9078 | 209 | int pages = -1; |
76ec935e | 210 | |
e66c9078 | 211 | if (doc != null) { |
76ec935e NR |
212 | Element center = doc.getElementsByTag("center").first(); |
213 | if (center != null) { | |
76ec935e NR |
214 | for (Element a : center.getElementsByTag("a")) { |
215 | if (a.absUrl("href").contains("&p=")) { | |
216 | int thisLinkPages = -1; | |
217 | try { | |
218 | String[] tab = a.absUrl("href").split("="); | |
219 | tab = tab[tab.length - 1].split("&"); | |
220 | thisLinkPages = Integer | |
221 | .parseInt(tab[tab.length - 1]); | |
222 | } catch (Exception e) { | |
223 | } | |
224 | ||
225 | pages = Math.max(pages, thisLinkPages); | |
226 | } | |
227 | } | |
76ec935e | 228 | } |
e66c9078 NR |
229 | } |
230 | ||
231 | return pages; | |
232 | } | |
233 | ||
234 | /** | |
235 | * Fetch the stories from the given page. | |
236 | * | |
237 | * @param sourceUrl | |
238 | * the url of the document | |
239 | * @param doc | |
240 | * the document to use (if NULL, will be loaded from | |
241 | * <tt>sourceUrl</tt>) | |
242 | * @param mainSubject | |
243 | * the main subject (the anime/book/movie item related to the | |
244 | * stories, like "MLP" or "Doctor Who"), or NULL if none | |
245 | * | |
246 | * @return the stories found in it | |
247 | * | |
248 | * @throws IOException | |
249 | * in case of I/O errors | |
250 | */ | |
251 | private List<MetaData> getStories(String sourceUrl, Document doc, | |
252 | String mainSubject) throws IOException { | |
253 | List<MetaData> metas = new ArrayList<MetaData>(); | |
254 | ||
255 | if (doc == null) { | |
256 | doc = load(sourceUrl, false); | |
257 | } | |
76ec935e | 258 | |
e66c9078 NR |
259 | for (Element story : doc.getElementsByClass("z-list")) { |
260 | MetaData meta = new MetaData(); | |
261 | meta.setImageDocument(false); | |
262 | meta.setSource(getType().getSourceName()); | |
263 | ||
596ed3d6 | 264 | // Title, URL, Cover |
e66c9078 NR |
265 | Element stitle = story.getElementsByClass("stitle").first(); |
266 | if (stitle != null) { | |
267 | meta.setTitle(stitle.text()); | |
268 | meta.setUrl(stitle.absUrl("href")); | |
269 | Element cover = stitle.getElementsByTag("img").first(); | |
270 | if (cover != null) { | |
271 | // note: see data-original if needed? | |
272 | String coverUrl = cover.absUrl("src"); | |
273 | ||
274 | try { | |
275 | InputStream in = Instance.getCache().open( | |
276 | new URL(coverUrl), getSupport(), true); | |
277 | try { | |
278 | meta.setCover(new Image(in)); | |
279 | } finally { | |
280 | in.close(); | |
281 | } | |
282 | } catch (Exception e) { | |
283 | Instance.getTraceHandler() | |
284 | .error(new Exception( | |
285 | "Cannot download cover for Fanfiction story in search mode", | |
286 | e)); | |
158b372d NR |
287 | } |
288 | } | |
e66c9078 | 289 | } |
158b372d | 290 | |
596ed3d6 | 291 | // Author |
e66c9078 NR |
292 | Elements as = story.getElementsByTag("a"); |
293 | if (as.size() > 1) { | |
294 | meta.setAuthor(as.get(1).text()); | |
295 | } | |
158b372d | 296 | |
596ed3d6 | 297 | // Tags (concatenated text), published date, updated date, Resume |
e66c9078 | 298 | String tags = ""; |
596ed3d6 | 299 | List<String> tagList = new ArrayList<String>(); |
e66c9078 NR |
300 | Elements divs = story.getElementsByTag("div"); |
301 | if (divs.size() > 1 && divs.get(1).childNodeSize() > 0) { | |
302 | String resume = divs.get(1).text(); | |
303 | if (divs.size() > 2) { | |
304 | tags = divs.get(2).text(); | |
305 | resume = resume.substring(0, | |
306 | resume.length() - tags.length()).trim(); | |
158b372d | 307 | |
e66c9078 NR |
308 | for (Element d : divs.get(2).getElementsByAttribute( |
309 | "data-xutime")) { | |
310 | String secs = d.attr("data-xutime"); | |
311 | try { | |
312 | String date = new SimpleDateFormat("yyyy-MM-dd") | |
313 | .format(new Date( | |
314 | Long.parseLong(secs) * 1000)); | |
315 | // (updated, ) published | |
316 | if (meta.getDate() != null) { | |
317 | tagList.add("Updated: " + meta.getDate()); | |
318 | } | |
319 | meta.setDate(date); | |
320 | } catch (Exception e) { | |
321 | } | |
158b372d NR |
322 | } |
323 | } | |
324 | ||
e66c9078 NR |
325 | meta.setResume(getSupport().makeChapter(new URL(sourceUrl), 0, |
326 | Instance.getTrans().getString(StringId.DESCRIPTION), | |
158b372d | 327 | resume)); |
158b372d | 328 | } |
158b372d | 329 | |
e66c9078 NR |
330 | // How are the tags ordered? |
331 | // We have "Rated: xx", then the language, then all other tags | |
332 | // If the subject(s) is/are present, they are before "Rated: xx" | |
333 | ||
596ed3d6 | 334 | // //////////// |
e66c9078 | 335 | // Examples: // |
596ed3d6 | 336 | // //////////// |
e66c9078 NR |
337 | |
338 | // Search (Luna) Tags: [Harry Potter, Rated: T, English, Chapters: | |
339 | // 1, Words: 270, Reviews: 2, Published: 2/19/2013, Luna L.] | |
340 | ||
341 | // Normal (MLP) Tags: [Rated: T, Spanish, Drama/Suspense, Chapters: | |
342 | // 2, Words: 8,686, Reviews: 1, Favs: 1, Follows: 1, Updated: 4/7, | |
596ed3d6 | 343 | // Published: 4/2] |
e66c9078 NR |
344 | |
345 | // Crossover (MLP/Who) Tags: [Rated: K+, English, Adventure/Romance, | |
346 | // Chapters: 8, Words: 7,788, Reviews: 2, Favs: 2, Follows: 1, | |
596ed3d6 | 347 | // Published: 9/1/2016] |
e66c9078 NR |
348 | |
349 | boolean rated = false; | |
350 | boolean isLang = false; | |
596ed3d6 | 351 | String subject = mainSubject == null ? "" : mainSubject; |
e66c9078 NR |
352 | String[] tab = tags.split(" *- *"); |
353 | for (int i = 0; i < tab.length; i++) { | |
354 | String tag = tab[i]; | |
355 | if (tag.startsWith("Rated: ")) { | |
356 | rated = true; | |
357 | } | |
158b372d | 358 | |
e66c9078 NR |
359 | if (!rated) { |
360 | if (!subject.isEmpty()) { | |
361 | subject += ", "; | |
362 | } | |
363 | subject += tag; | |
364 | } else if (isLang) { | |
365 | meta.setLang(tag); | |
366 | isLang = false; | |
367 | } else { | |
368 | if (tag.contains(":")) { | |
369 | // Handle special tags: | |
370 | if (tag.startsWith("Words: ")) { | |
371 | try { | |
372 | meta.setWords(Long.parseLong(tag | |
373 | .substring("Words: ".length()) | |
374 | .replace(",", "").trim())); | |
375 | } catch (Exception e) { | |
376 | } | |
377 | } else if (tag.startsWith("Rated: ")) { | |
378 | tagList.add(tag); | |
379 | } | |
380 | } else { | |
596ed3d6 | 381 | // Normal tags are "/"-separated |
e66c9078 NR |
382 | for (String t : tag.split("/")) { |
383 | tagList.add(t); | |
384 | } | |
385 | } | |
158b372d | 386 | |
e66c9078 NR |
387 | if (tag.startsWith("Rated: ")) { |
388 | isLang = true; | |
389 | } | |
390 | } | |
391 | } | |
158b372d | 392 | |
e66c9078 NR |
393 | meta.setSubject(subject); |
394 | meta.setTags(tagList); | |
158b372d | 395 | |
e66c9078 NR |
396 | metas.add(meta); |
397 | } | |
158b372d | 398 | |
e66c9078 | 399 | return metas; |
158b372d NR |
400 | } |
401 | } |