Add more warnings source to 1.6) and fix warnings
[fanfix.git] / src / be / nikiroo / fanfix / supported / Fanfiction.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
68686a37 3import java.awt.image.BufferedImage;
08fe2e33
NR
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.MalformedURLException;
7import java.net.URL;
8import java.text.SimpleDateFormat;
9import java.util.ArrayList;
10import java.util.Date;
11import java.util.List;
12import java.util.Map.Entry;
13import java.util.Scanner;
14
15import be.nikiroo.fanfix.Instance;
22848428 16import be.nikiroo.fanfix.bundles.Config;
68686a37 17import be.nikiroo.fanfix.data.MetaData;
ed08c171 18import be.nikiroo.utils.Progress;
08fe2e33
NR
19import 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 */
28class 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
68686a37
NR
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());
2206ef66 48 meta.setUrl(source.toString());
68686a37
NR
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) {
08fe2e33
NR
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) {
68686a37 69 return StringUtils.unhtml(line.substring(0, pos)).trim();
08fe2e33
NR
70 }
71 }
72 }
73
74 return null;
75 }
76
211f7ddb 77 private List<String> getTags(InputStream in) {
68686a37 78 List<String> tags = new ArrayList<String>();
08fe2e33
NR
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("-")) {
68686a37 97 tags.add(StringUtils.unhtml(tag).trim());
08fe2e33
NR
98 }
99 }
100 }
101 }
102
103 return tags;
104 }
105
68686a37 106 private String getTitle(InputStream in) {
08fe2e33
NR
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
68686a37 120 return StringUtils.unhtml(line).trim();
08fe2e33
NR
121 }
122 }
123 }
124
125 return null;
126 }
127
68686a37 128 private String getAuthor(InputStream in) {
b4dc6ab5
NR
129 String author = null;
130
08fe2e33
NR
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) {
b4dc6ab5
NR
139 author = StringUtils.unhtml(line).trim();
140 break;
08fe2e33
NR
141 }
142 }
143 }
144
b4dc6ab5 145 return fixAuthor(author);
08fe2e33
NR
146 }
147
68686a37 148 private String getDate(InputStream in) {
08fe2e33
NR
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
68686a37 179 private BufferedImage getCover(URL url, InputStream in) {
08fe2e33
NR
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
333f0e7b 204 return getImage(this, null, line);
08fe2e33
NR
205 }
206 }
207 }
208 }
209
210 return null;
211 }
212
213 @Override
ed08c171
NR
214 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
215 Progress pg) {
08fe2e33
NR
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;
08fe2e33 229
333f0e7b
NR
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>() {
211f7ddb 248 @Override
333f0e7b
NR
249 public URL setValue(URL value) {
250 return null;
251 }
252
211f7ddb 253 @Override
333f0e7b
NR
254 public URL getValue() {
255 return chapURL;
256 }
257
211f7ddb 258 @Override
333f0e7b
NR
259 public String getKey() {
260 return chapName;
261 }
262 });
263 } catch (MalformedURLException e) {
264 Instance.syserr(new IOException(
265 "Cannot parse chapter " + i + " url: "
266 + (base + i + suffix), e));
267 }
08fe2e33
NR
268 }
269 }
270 }
333f0e7b
NR
271 } else {
272 // only one chapter:
273 final String chapName = getTitle(reset(in));
274 final URL chapURL = source;
275 urls.add(new Entry<String, URL>() {
211f7ddb 276 @Override
333f0e7b
NR
277 public URL setValue(URL value) {
278 return null;
279 }
280
211f7ddb 281 @Override
333f0e7b
NR
282 public URL getValue() {
283 return chapURL;
284 }
285
211f7ddb 286 @Override
333f0e7b
NR
287 public String getKey() {
288 return chapName;
289 }
290 });
08fe2e33
NR
291 }
292
293 return urls;
294 }
295
296 @Override
ed08c171
NR
297 protected String getChapterContent(URL source, InputStream in, int number,
298 Progress pg) {
08fe2e33
NR
299 StringBuilder builder = new StringBuilder();
300 String startAt = "class='storytext ";
301 String endAt1 = "function review_init";
302 String endAt2 = "id=chap_select";
303 boolean ok = false;
304
305 @SuppressWarnings("resource")
306 Scanner scan = new Scanner(in, "UTF-8");
307 scan.useDelimiter("\\n");
308 while (scan.hasNext()) {
309 String line = scan.next();
310 if (!ok && line.contains(startAt)) {
311 ok = true;
312 } else if (ok && (line.contains(endAt1) || line.contains(endAt2))) {
313 ok = false;
314 break;
315 }
316
317 if (ok) {
318 // First line may contain the title and chap name again
319 if (builder.length() == 0) {
320 int pos = line.indexOf("<hr");
321 if (pos >= 0) {
22848428
NR
322 boolean chaptered = false;
323 for (String lang : Instance.getConfig()
324 .getString(Config.CHAPTER).split(",")) {
325 String chapterWord = Instance.getConfig()
326 .getStringX(Config.CHAPTER, lang);
327 int posChap = line.indexOf(chapterWord + " ");
328 if (posChap < pos) {
329 chaptered = true;
330 break;
331 }
332 }
333
334 if (chaptered) {
335 line = line.substring(pos);
336 }
08fe2e33
NR
337 }
338 }
339
340 builder.append(line);
406447a4 341 builder.append(' ');
08fe2e33
NR
342 }
343 }
344
345 return builder.toString();
346 }
347
348 @Override
349 protected boolean supports(URL url) {
350 return "fanfiction.net".equals(url.getHost())
351 || "www.fanfiction.net".equals(url.getHost());
352 }
353}