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