1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
7 import java
.text
.SimpleDateFormat
;
8 import java
.util
.AbstractMap
;
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
;
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
.Image
;
19 import be
.nikiroo
.utils
.Progress
;
20 import be
.nikiroo
.utils
.StringUtils
;
23 * Support class for <a href="http://www.fanfiction.net/">Faniction.net</a>
24 * stories, a website dedicated to fanfictions of many, many different
25 * universes, from TV shows to novels to games.
29 class Fanfiction
extends BasicSupport_Deprecated
{
31 protected boolean isHtml() {
36 public String
getSourceName() {
37 return "Fanfiction.net";
41 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
42 MetaData meta
= new MetaData();
44 meta
.setTitle(getTitle(reset(in
)));
45 meta
.setAuthor(getAuthor(reset(in
)));
46 meta
.setDate(getDate(reset(in
)));
47 meta
.setTags(getTags(reset(in
)));
48 meta
.setSource(getSourceName());
49 meta
.setUrl(source
.toString());
50 meta
.setPublisher(getSourceName());
51 meta
.setUuid(source
.toString());
53 meta
.setLang("en"); // TODO!
54 meta
.setSubject(getSubject(reset(in
)));
55 meta
.setType(getType().toString());
56 meta
.setImageDocument(false);
57 meta
.setCover(getCover(source
, reset(in
)));
62 private String
getSubject(InputStream in
) {
63 String line
= getLine(in
, "id=pre_story_links", 0);
65 int pos
= line
.lastIndexOf('"');
67 line
= line
.substring(pos
+ 1);
68 pos
= line
.indexOf('<');
70 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
78 private List
<String
> getTags(InputStream in
) {
79 List
<String
> tags
= new ArrayList
<String
>();
81 String key
= "title=\"Send Private Message\"";
82 String line
= getLine(in
, key
, 2);
85 int pos
= line
.indexOf(key
);
87 line
= line
.substring(pos
+ key
.length());
89 pos
= line
.indexOf(key
);
91 line
= line
.substring(0, pos
);
92 line
= StringUtils
.unhtml(line
).trim();
93 if (line
.endsWith("-")) {
94 line
= line
.substring(0, line
.length() - 1);
97 for (String tag
: line
.split("-")) {
98 tags
.add(StringUtils
.unhtml(tag
).trim());
107 private String
getTitle(InputStream in
) {
109 @SuppressWarnings("resource")
110 Scanner scan
= new Scanner(in
, "UTF-8");
111 scan
.useDelimiter("\\n");
112 while (scan
.hasNext()) {
113 String line
= scan
.next();
114 if (line
.contains("xcontrast_txt")) {
116 line
= StringUtils
.unhtml(line
).trim();
117 if (line
.startsWith("Follow/Fav")) {
118 line
= line
.substring("Follow/Fav".length()).trim();
121 return StringUtils
.unhtml(line
).trim();
129 private String
getAuthor(InputStream in
) {
130 String author
= null;
133 @SuppressWarnings("resource")
134 Scanner scan
= new Scanner(in
, "UTF-8");
135 scan
.useDelimiter("\\n");
136 while (scan
.hasNext()) {
137 String line
= scan
.next();
138 if (line
.contains("xcontrast_txt")) {
140 author
= StringUtils
.unhtml(line
).trim();
146 return BasicSupportHelper
.fixAuthor(author
);
149 private String
getDate(InputStream in
) {
150 String key
= "Published: <span data-xutime='";
151 String line
= getLine(in
, key
, 0);
153 int pos
= line
.indexOf(key
);
155 line
= line
.substring(pos
+ key
.length());
156 pos
= line
.indexOf('\'');
158 line
= line
.substring(0, pos
).trim();
160 SimpleDateFormat sdf
= new SimpleDateFormat(
163 .format(new Date(1000 * Long
.parseLong(line
)));
164 } catch (NumberFormatException e
) {
165 Instance
.getTraceHandler().error(
167 "Cannot convert publication date: "
178 protected String
getDesc(URL source
, InputStream in
) {
179 return getLine(in
, "title=\"Send Private Message\"", 1);
182 private Image
getCover(URL url
, InputStream in
) {
183 String key
= "class='cimage";
184 String line
= getLine(in
, key
, 0);
186 int pos
= line
.indexOf(key
);
188 line
= line
.substring(pos
+ key
.length());
190 pos
= line
.indexOf(key
);
192 line
= line
.substring(pos
+ key
.length());
193 pos
= line
.indexOf('\'');
195 line
= line
.substring(0, pos
);
196 if (line
.startsWith("//")) {
197 line
= url
.getProtocol() + "://"
199 } else if (line
.startsWith("//")) {
200 line
= url
.getProtocol() + "://" + url
.getHost()
201 + "/" + line
.substring(1);
203 line
= url
.getProtocol() + "://" + url
.getHost()
204 + "/" + url
.getPath() + "/" + line
;
207 return getImage(this, null, line
);
217 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
219 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
221 String base
= source
.toString();
222 int pos
= base
.lastIndexOf('/');
223 String suffix
= base
.substring(pos
); // including '/' at start
224 base
= base
.substring(0, pos
);
225 if (base
.endsWith("/1")) {
226 base
= base
.substring(0, base
.length() - 1); // including '/' at end
229 String line
= getLine(in
, "id=chap_select", 0);
230 String key
= "<option value=";
234 for (pos
= line
.indexOf(key
); pos
>= 0; pos
= line
235 .indexOf(key
, pos
), i
++) {
236 pos
= line
.indexOf('>', pos
);
238 int endOfName
= line
.indexOf('<', pos
);
239 if (endOfName
>= 0) {
240 String name
= line
.substring(pos
+ 1, endOfName
);
241 String chapNum
= i
+ ".";
242 if (name
.startsWith(chapNum
)) {
243 name
= name
.substring(chapNum
.length(),
248 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(
249 name
.trim(), new URL(base
+ i
+ suffix
)));
250 } catch (MalformedURLException e
) {
251 Instance
.getTraceHandler()
252 .error(new IOException(
253 "Cannot parse chapter " + i
255 + (base
+ i
+ suffix
), e
));
262 final String chapName
= getTitle(reset(in
));
263 final URL chapURL
= source
;
264 urls
.add(new Entry
<String
, URL
>() {
266 public URL
setValue(URL value
) {
271 public URL
getValue() {
276 public String
getKey() {
286 protected String
getChapterContent(URL source
, InputStream in
, int number
,
288 StringBuilder builder
= new StringBuilder();
289 String startAt
= "class='storytext ";
290 String endAt1
= "function review_init";
291 String endAt2
= "id=chap_select";
294 @SuppressWarnings("resource")
295 Scanner scan
= new Scanner(in
, "UTF-8");
296 scan
.useDelimiter("\\n");
297 while (scan
.hasNext()) {
298 String line
= scan
.next();
299 if (!ok
&& line
.contains(startAt
)) {
301 } else if (ok
&& (line
.contains(endAt1
) || line
.contains(endAt2
))) {
307 // First line may contain the title and chap name again
308 if (builder
.length() == 0) {
309 int pos
= line
.indexOf("<hr");
311 boolean chaptered
= false;
312 for (String lang
: Instance
.getConfig()
313 .getString(Config
.CHAPTER
).split(",")) {
314 String chapterWord
= Instance
.getConfig()
315 .getStringX(Config
.CHAPTER
, lang
);
316 int posChap
= line
.indexOf(chapterWord
+ " ");
324 line
= line
.substring(pos
);
329 builder
.append(line
);
334 return builder
.toString();
338 protected boolean supports(URL url
) {
339 return "fanfiction.net".equals(url
.getHost())
340 || "www.fanfiction.net".equals(url
.getHost());