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"); |
596ed3d6 | 146 | return getStories(BASE_URL + "search/?ready=1&type=story&keywords=" |
8ffc8b73 | 147 | + encoded + "&ppage=" + page, null, null); |
158b372d NR |
148 | } |
149 | ||
150 | @Override | |
e66c9078 NR |
151 | public List<MetaData> search(SearchableTag tag, int page) |
152 | throws IOException { | |
158b372d NR |
153 | List<MetaData> metas = new ArrayList<MetaData>(); |
154 | ||
e66c9078 NR |
155 | String url = tag.getId(); |
156 | if (url != null) { | |
157 | if (page > 1) { | |
158 | int pos = url.indexOf("&p="); | |
159 | if (pos >= 0) { | |
160 | url = url.replaceAll("(.*\\&p=)[0-9]*(.*)", "$1\\" + page | |
161 | + "$2"); | |
162 | } else { | |
163 | url += "&p=" + page; | |
164 | } | |
165 | } | |
74a43961 | 166 | |
e66c9078 NR |
167 | Document doc = load(url, false); |
168 | ||
169 | // Update the pages number if needed | |
b3b9a1cd | 170 | if (tag.getPages() < 0 && tag.isLeaf()) { |
e66c9078 NR |
171 | tag.setPages(getPages(doc)); |
172 | } | |
173 | ||
174 | // Find out the full subjects (including parents) | |
175 | String subjects = ""; | |
176 | for (SearchableTag t = tag; t != null; t = t.getParent()) { | |
177 | if (!subjects.isEmpty()) { | |
178 | subjects += ", "; | |
179 | } | |
180 | subjects += t.getName(); | |
181 | } | |
182 | ||
183 | metas = getStories(url, doc, subjects); | |
184 | } | |
185 | ||
186 | return metas; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Return the number of pages in this stories result listing. | |
191 | * | |
192 | * @param doc | |
193 | * the document | |
194 | * | |
195 | * @return the number of pages or -1 if unknown | |
e66c9078 | 196 | */ |
8ffc8b73 | 197 | private int getPages(Document doc) { |
e66c9078 | 198 | int pages = -1; |
76ec935e | 199 | |
e66c9078 | 200 | if (doc != null) { |
76ec935e NR |
201 | Element center = doc.getElementsByTag("center").first(); |
202 | if (center != null) { | |
76ec935e NR |
203 | for (Element a : center.getElementsByTag("a")) { |
204 | if (a.absUrl("href").contains("&p=")) { | |
205 | int thisLinkPages = -1; | |
206 | try { | |
207 | String[] tab = a.absUrl("href").split("="); | |
208 | tab = tab[tab.length - 1].split("&"); | |
209 | thisLinkPages = Integer | |
210 | .parseInt(tab[tab.length - 1]); | |
211 | } catch (Exception e) { | |
212 | } | |
213 | ||
214 | pages = Math.max(pages, thisLinkPages); | |
215 | } | |
216 | } | |
76ec935e | 217 | } |
e66c9078 NR |
218 | } |
219 | ||
220 | return pages; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Fetch the stories from the given page. | |
225 | * | |
226 | * @param sourceUrl | |
227 | * the url of the document | |
228 | * @param doc | |
229 | * the document to use (if NULL, will be loaded from | |
230 | * <tt>sourceUrl</tt>) | |
231 | * @param mainSubject | |
232 | * the main subject (the anime/book/movie item related to the | |
233 | * stories, like "MLP" or "Doctor Who"), or NULL if none | |
234 | * | |
235 | * @return the stories found in it | |
236 | * | |
237 | * @throws IOException | |
238 | * in case of I/O errors | |
239 | */ | |
240 | private List<MetaData> getStories(String sourceUrl, Document doc, | |
241 | String mainSubject) throws IOException { | |
242 | List<MetaData> metas = new ArrayList<MetaData>(); | |
243 | ||
244 | if (doc == null) { | |
245 | doc = load(sourceUrl, false); | |
246 | } | |
76ec935e | 247 | |
e66c9078 NR |
248 | for (Element story : doc.getElementsByClass("z-list")) { |
249 | MetaData meta = new MetaData(); | |
250 | meta.setImageDocument(false); | |
251 | meta.setSource(getType().getSourceName()); | |
252 | ||
596ed3d6 | 253 | // Title, URL, Cover |
e66c9078 NR |
254 | Element stitle = story.getElementsByClass("stitle").first(); |
255 | if (stitle != null) { | |
256 | meta.setTitle(stitle.text()); | |
257 | meta.setUrl(stitle.absUrl("href")); | |
258 | Element cover = stitle.getElementsByTag("img").first(); | |
259 | if (cover != null) { | |
260 | // note: see data-original if needed? | |
261 | String coverUrl = cover.absUrl("src"); | |
262 | ||
263 | try { | |
264 | InputStream in = Instance.getCache().open( | |
265 | new URL(coverUrl), getSupport(), true); | |
266 | try { | |
267 | meta.setCover(new Image(in)); | |
268 | } finally { | |
269 | in.close(); | |
270 | } | |
271 | } catch (Exception e) { | |
272 | Instance.getTraceHandler() | |
273 | .error(new Exception( | |
274 | "Cannot download cover for Fanfiction story in search mode", | |
275 | e)); | |
158b372d NR |
276 | } |
277 | } | |
e66c9078 | 278 | } |
158b372d | 279 | |
596ed3d6 | 280 | // Author |
e66c9078 NR |
281 | Elements as = story.getElementsByTag("a"); |
282 | if (as.size() > 1) { | |
283 | meta.setAuthor(as.get(1).text()); | |
284 | } | |
158b372d | 285 | |
596ed3d6 | 286 | // Tags (concatenated text), published date, updated date, Resume |
e66c9078 | 287 | String tags = ""; |
596ed3d6 | 288 | List<String> tagList = new ArrayList<String>(); |
e66c9078 NR |
289 | Elements divs = story.getElementsByTag("div"); |
290 | if (divs.size() > 1 && divs.get(1).childNodeSize() > 0) { | |
291 | String resume = divs.get(1).text(); | |
292 | if (divs.size() > 2) { | |
293 | tags = divs.get(2).text(); | |
294 | resume = resume.substring(0, | |
295 | resume.length() - tags.length()).trim(); | |
158b372d | 296 | |
e66c9078 NR |
297 | for (Element d : divs.get(2).getElementsByAttribute( |
298 | "data-xutime")) { | |
299 | String secs = d.attr("data-xutime"); | |
300 | try { | |
301 | String date = new SimpleDateFormat("yyyy-MM-dd") | |
302 | .format(new Date( | |
303 | Long.parseLong(secs) * 1000)); | |
304 | // (updated, ) published | |
305 | if (meta.getDate() != null) { | |
306 | tagList.add("Updated: " + meta.getDate()); | |
307 | } | |
308 | meta.setDate(date); | |
309 | } catch (Exception e) { | |
310 | } | |
158b372d NR |
311 | } |
312 | } | |
313 | ||
e66c9078 NR |
314 | meta.setResume(getSupport().makeChapter(new URL(sourceUrl), 0, |
315 | Instance.getTrans().getString(StringId.DESCRIPTION), | |
158b372d | 316 | resume)); |
158b372d | 317 | } |
158b372d | 318 | |
e66c9078 NR |
319 | // How are the tags ordered? |
320 | // We have "Rated: xx", then the language, then all other tags | |
321 | // If the subject(s) is/are present, they are before "Rated: xx" | |
322 | ||
596ed3d6 | 323 | // //////////// |
e66c9078 | 324 | // Examples: // |
596ed3d6 | 325 | // //////////// |
e66c9078 NR |
326 | |
327 | // Search (Luna) Tags: [Harry Potter, Rated: T, English, Chapters: | |
328 | // 1, Words: 270, Reviews: 2, Published: 2/19/2013, Luna L.] | |
329 | ||
330 | // Normal (MLP) Tags: [Rated: T, Spanish, Drama/Suspense, Chapters: | |
331 | // 2, Words: 8,686, Reviews: 1, Favs: 1, Follows: 1, Updated: 4/7, | |
596ed3d6 | 332 | // Published: 4/2] |
e66c9078 NR |
333 | |
334 | // Crossover (MLP/Who) Tags: [Rated: K+, English, Adventure/Romance, | |
335 | // Chapters: 8, Words: 7,788, Reviews: 2, Favs: 2, Follows: 1, | |
596ed3d6 | 336 | // Published: 9/1/2016] |
e66c9078 NR |
337 | |
338 | boolean rated = false; | |
339 | boolean isLang = false; | |
596ed3d6 | 340 | String subject = mainSubject == null ? "" : mainSubject; |
e66c9078 NR |
341 | String[] tab = tags.split(" *- *"); |
342 | for (int i = 0; i < tab.length; i++) { | |
343 | String tag = tab[i]; | |
344 | if (tag.startsWith("Rated: ")) { | |
345 | rated = true; | |
346 | } | |
158b372d | 347 | |
e66c9078 NR |
348 | if (!rated) { |
349 | if (!subject.isEmpty()) { | |
350 | subject += ", "; | |
351 | } | |
352 | subject += tag; | |
353 | } else if (isLang) { | |
354 | meta.setLang(tag); | |
355 | isLang = false; | |
356 | } else { | |
357 | if (tag.contains(":")) { | |
358 | // Handle special tags: | |
359 | if (tag.startsWith("Words: ")) { | |
360 | try { | |
361 | meta.setWords(Long.parseLong(tag | |
362 | .substring("Words: ".length()) | |
363 | .replace(",", "").trim())); | |
364 | } catch (Exception e) { | |
365 | } | |
366 | } else if (tag.startsWith("Rated: ")) { | |
367 | tagList.add(tag); | |
368 | } | |
369 | } else { | |
596ed3d6 | 370 | // Normal tags are "/"-separated |
e66c9078 NR |
371 | for (String t : tag.split("/")) { |
372 | tagList.add(t); | |
373 | } | |
374 | } | |
158b372d | 375 | |
e66c9078 NR |
376 | if (tag.startsWith("Rated: ")) { |
377 | isLang = true; | |
378 | } | |
379 | } | |
380 | } | |
158b372d | 381 | |
e66c9078 NR |
382 | meta.setSubject(subject); |
383 | meta.setTags(tagList); | |
158b372d | 384 | |
e66c9078 NR |
385 | metas.add(meta); |
386 | } | |
158b372d | 387 | |
e66c9078 | 388 | return metas; |
158b372d NR |
389 | } |
390 | } |