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