Update nikiroo-utils, update Library
[fanfix.git] / src / be / nikiroo / fanfix / supported / Fanfiction.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.text.SimpleDateFormat;
9 import java.util.ArrayList;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Map.Entry;
13 import java.util.Scanner;
14
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix.bundles.Config;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.utils.Progress;
19 import be.nikiroo.utils.StringUtils;
20
21 /**
22 * Support class for <a href="http://www.fanfiction.net/">Faniction.net</a>
23 * stories, a website dedicated to fanfictions of many, many different
24 * universes, from TV shows to novels to games.
25 *
26 * @author niki
27 */
28 class Fanfiction extends BasicSupport {
29 @Override
30 protected boolean isHtml() {
31 return true;
32 }
33
34 @Override
35 public String getSourceName() {
36 return "Fanfiction.net";
37 }
38
39 @Override
40 protected MetaData getMeta(URL source, InputStream in) throws IOException {
41 MetaData meta = new MetaData();
42
43 meta.setTitle(getTitle(reset(in)));
44 meta.setAuthor(getAuthor(reset(in)));
45 meta.setDate(getDate(reset(in)));
46 meta.setTags(getTags(reset(in)));
47 meta.setSource(getSourceName());
48 meta.setUrl(source.toString());
49 meta.setPublisher(getSourceName());
50 meta.setUuid(source.toString());
51 meta.setLuid("");
52 meta.setLang("EN");
53 meta.setSubject(getSubject(reset(in)));
54 meta.setType(getType().toString());
55 meta.setImageDocument(false);
56 meta.setCover(getCover(source, reset(in)));
57
58 return meta;
59 }
60
61 private String getSubject(InputStream in) {
62 String line = getLine(in, "id=pre_story_links", 0);
63 if (line != null) {
64 int pos = line.lastIndexOf('"');
65 if (pos >= 1) {
66 line = line.substring(pos + 1);
67 pos = line.indexOf('<');
68 if (pos >= 0) {
69 return StringUtils.unhtml(line.substring(0, pos)).trim();
70 }
71 }
72 }
73
74 return null;
75 }
76
77 private List<String> getTags(InputStream in) throws IOException {
78 List<String> tags = new ArrayList<String>();
79
80 String key = "title=\"Send Private Message\"";
81 String line = getLine(in, key, 2);
82 if (line != null) {
83 key = "Rated:";
84 int pos = line.indexOf(key);
85 if (pos >= 0) {
86 line = line.substring(pos + key.length());
87 key = "Chapters:";
88 pos = line.indexOf(key);
89 if (pos >= 0) {
90 line = line.substring(0, pos);
91 line = StringUtils.unhtml(line).trim();
92 if (line.endsWith("-")) {
93 line = line.substring(0, line.length() - 1);
94 }
95
96 for (String tag : line.split("-")) {
97 tags.add(StringUtils.unhtml(tag).trim());
98 }
99 }
100 }
101 }
102
103 return tags;
104 }
105
106 private String getTitle(InputStream in) {
107 int i = 0;
108 @SuppressWarnings("resource")
109 Scanner scan = new Scanner(in, "UTF-8");
110 scan.useDelimiter("\\n");
111 while (scan.hasNext()) {
112 String line = scan.next();
113 if (line.contains("xcontrast_txt")) {
114 if ((++i) == 2) {
115 line = StringUtils.unhtml(line).trim();
116 if (line.startsWith("Follow/Fav")) {
117 line = line.substring("Follow/Fav".length()).trim();
118 }
119
120 return StringUtils.unhtml(line).trim();
121 }
122 }
123 }
124
125 return null;
126 }
127
128 private String getAuthor(InputStream in) {
129 String author = null;
130
131 int i = 0;
132 @SuppressWarnings("resource")
133 Scanner scan = new Scanner(in, "UTF-8");
134 scan.useDelimiter("\\n");
135 while (scan.hasNext()) {
136 String line = scan.next();
137 if (line.contains("xcontrast_txt")) {
138 if ((++i) == 3) {
139 author = StringUtils.unhtml(line).trim();
140 break;
141 }
142 }
143 }
144
145 return fixAuthor(author);
146 }
147
148 private String getDate(InputStream in) {
149 String key = "Published: <span data-xutime='";
150 String line = getLine(in, key, 0);
151 if (line != null) {
152 int pos = line.indexOf(key);
153 if (pos >= 0) {
154 line = line.substring(pos + key.length());
155 pos = line.indexOf('\'');
156 if (pos >= 0) {
157 line = line.substring(0, pos).trim();
158 try {
159 SimpleDateFormat sdf = new SimpleDateFormat(
160 "YYYY-MM-dd");
161 return sdf
162 .format(new Date(1000 * Long.parseLong(line)));
163 } catch (NumberFormatException e) {
164 Instance.syserr(new IOException(
165 "Cannot convert publication date: " + line, e));
166 }
167 }
168 }
169 }
170
171 return null;
172 }
173
174 @Override
175 protected String getDesc(URL source, InputStream in) {
176 return getLine(in, "title=\"Send Private Message\"", 1);
177 }
178
179 private BufferedImage getCover(URL url, InputStream in) {
180 String key = "class='cimage";
181 String line = getLine(in, key, 0);
182 if (line != null) {
183 int pos = line.indexOf(key);
184 if (pos >= 0) {
185 line = line.substring(pos + key.length());
186 key = "src='";
187 pos = line.indexOf(key);
188 if (pos >= 0) {
189 line = line.substring(pos + key.length());
190 pos = line.indexOf('\'');
191 if (pos >= 0) {
192 line = line.substring(0, pos);
193 if (line.startsWith("//")) {
194 line = url.getProtocol() + "://"
195 + line.substring(2);
196 } else if (line.startsWith("//")) {
197 line = url.getProtocol() + "://" + url.getHost()
198 + "/" + line.substring(1);
199 } else {
200 line = url.getProtocol() + "://" + url.getHost()
201 + "/" + url.getPath() + "/" + line;
202 }
203
204 return getImage(this, null, line);
205 }
206 }
207 }
208 }
209
210 return null;
211 }
212
213 @Override
214 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
215 Progress pg) {
216 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
217
218 String base = source.toString();
219 int pos = base.lastIndexOf('/');
220 String suffix = base.substring(pos); // including '/' at start
221 base = base.substring(0, pos);
222 if (base.endsWith("/1")) {
223 base = base.substring(0, base.length() - 1); // including '/' at end
224 }
225
226 String line = getLine(in, "id=chap_select", 0);
227 String key = "<option value=";
228 int i = 1;
229
230 if (line != null) {
231 for (pos = line.indexOf(key); pos >= 0; pos = line
232 .indexOf(key, pos), i++) {
233 pos = line.indexOf('>', pos);
234 if (pos >= 0) {
235 int endOfName = line.indexOf('<', pos);
236 if (endOfName >= 0) {
237 String name = line.substring(pos + 1, endOfName);
238 String chapNum = i + ".";
239 if (name.startsWith(chapNum)) {
240 name = name.substring(chapNum.length(),
241 name.length());
242 }
243
244 try {
245 final String chapName = name.trim();
246 final URL chapURL = new URL(base + i + suffix);
247 urls.add(new Entry<String, URL>() {
248 public URL setValue(URL value) {
249 return null;
250 }
251
252 public URL getValue() {
253 return chapURL;
254 }
255
256 public String getKey() {
257 return chapName;
258 }
259 });
260 } catch (MalformedURLException e) {
261 Instance.syserr(new IOException(
262 "Cannot parse chapter " + i + " url: "
263 + (base + i + suffix), e));
264 }
265 }
266 }
267 }
268 } else {
269 // only one chapter:
270 final String chapName = getTitle(reset(in));
271 final URL chapURL = source;
272 urls.add(new Entry<String, URL>() {
273 public URL setValue(URL value) {
274 return null;
275 }
276
277 public URL getValue() {
278 return chapURL;
279 }
280
281 public String getKey() {
282 return chapName;
283 }
284 });
285 }
286
287 return urls;
288 }
289
290 @Override
291 protected String getChapterContent(URL source, InputStream in, int number,
292 Progress pg) {
293 StringBuilder builder = new StringBuilder();
294 String startAt = "class='storytext ";
295 String endAt1 = "function review_init";
296 String endAt2 = "id=chap_select";
297 boolean ok = false;
298
299 @SuppressWarnings("resource")
300 Scanner scan = new Scanner(in, "UTF-8");
301 scan.useDelimiter("\\n");
302 while (scan.hasNext()) {
303 String line = scan.next();
304 if (!ok && line.contains(startAt)) {
305 ok = true;
306 } else if (ok && (line.contains(endAt1) || line.contains(endAt2))) {
307 ok = false;
308 break;
309 }
310
311 if (ok) {
312 // First line may contain the title and chap name again
313 if (builder.length() == 0) {
314 int pos = line.indexOf("<hr");
315 if (pos >= 0) {
316 boolean chaptered = false;
317 for (String lang : Instance.getConfig()
318 .getString(Config.CHAPTER).split(",")) {
319 String chapterWord = Instance.getConfig()
320 .getStringX(Config.CHAPTER, lang);
321 int posChap = line.indexOf(chapterWord + " ");
322 if (posChap < pos) {
323 chaptered = true;
324 break;
325 }
326 }
327
328 if (chaptered) {
329 line = line.substring(pos);
330 }
331 }
332 }
333
334 builder.append(line);
335 builder.append(' ');
336 }
337 }
338
339 return builder.toString();
340 }
341
342 @Override
343 protected boolean supports(URL url) {
344 return "fanfiction.net".equals(url.getHost())
345 || "www.fanfiction.net".equals(url.getHost());
346 }
347 }