1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.MalformedURLException
;
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
;
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
;
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.
28 class Fanfiction
extends BasicSupport
{
30 protected boolean isHtml() {
35 public String
getSourceName() {
36 return "Fanfiction.net";
40 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
41 MetaData meta
= new MetaData();
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());
53 meta
.setSubject(getSubject(reset(in
)));
54 meta
.setType(getType().toString());
55 meta
.setImageDocument(false);
56 meta
.setCover(getCover(source
, reset(in
)));
61 private String
getSubject(InputStream in
) {
62 String line
= getLine(in
, "id=pre_story_links", 0);
64 int pos
= line
.lastIndexOf('"');
66 line
= line
.substring(pos
+ 1);
67 pos
= line
.indexOf('<');
69 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
77 private List
<String
> getTags(InputStream in
) throws IOException
{
78 List
<String
> tags
= new ArrayList
<String
>();
80 String key
= "title=\"Send Private Message\"";
81 String line
= getLine(in
, key
, 2);
84 int pos
= line
.indexOf(key
);
86 line
= line
.substring(pos
+ key
.length());
88 pos
= line
.indexOf(key
);
90 line
= line
.substring(0, pos
);
91 line
= StringUtils
.unhtml(line
).trim();
92 if (line
.endsWith("-")) {
93 line
= line
.substring(0, line
.length() - 1);
96 for (String tag
: line
.split("-")) {
97 tags
.add(StringUtils
.unhtml(tag
).trim());
106 private String
getTitle(InputStream in
) {
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")) {
115 line
= StringUtils
.unhtml(line
).trim();
116 if (line
.startsWith("Follow/Fav")) {
117 line
= line
.substring("Follow/Fav".length()).trim();
120 return StringUtils
.unhtml(line
).trim();
128 private String
getAuthor(InputStream in
) {
129 String author
= null;
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")) {
139 author
= StringUtils
.unhtml(line
).trim();
145 return fixAuthor(author
);
148 private String
getDate(InputStream in
) {
149 String key
= "Published: <span data-xutime='";
150 String line
= getLine(in
, key
, 0);
152 int pos
= line
.indexOf(key
);
154 line
= line
.substring(pos
+ key
.length());
155 pos
= line
.indexOf('\'');
157 line
= line
.substring(0, pos
).trim();
159 SimpleDateFormat sdf
= new SimpleDateFormat(
162 .format(new Date(1000 * Long
.parseLong(line
)));
163 } catch (NumberFormatException e
) {
164 Instance
.syserr(new IOException(
165 "Cannot convert publication date: " + line
, e
));
175 protected String
getDesc(URL source
, InputStream in
) {
176 return getLine(in
, "title=\"Send Private Message\"", 1);
179 private BufferedImage
getCover(URL url
, InputStream in
) {
180 String key
= "class='cimage";
181 String line
= getLine(in
, key
, 0);
183 int pos
= line
.indexOf(key
);
185 line
= line
.substring(pos
+ key
.length());
187 pos
= line
.indexOf(key
);
189 line
= line
.substring(pos
+ key
.length());
190 pos
= line
.indexOf('\'');
192 line
= line
.substring(0, pos
);
193 if (line
.startsWith("//")) {
194 line
= url
.getProtocol() + "://"
196 } else if (line
.startsWith("//")) {
197 line
= url
.getProtocol() + "://" + url
.getHost()
198 + "/" + line
.substring(1);
200 line
= url
.getProtocol() + "://" + url
.getHost()
201 + "/" + url
.getPath() + "/" + line
;
204 return getImage(this, null, line
);
214 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
216 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
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
226 String line
= getLine(in
, "id=chap_select", 0);
227 String key
= "<option value=";
231 for (pos
= line
.indexOf(key
); pos
>= 0; pos
= line
232 .indexOf(key
, pos
), i
++) {
233 pos
= line
.indexOf('>', pos
);
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(),
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
) {
252 public URL
getValue() {
256 public String
getKey() {
260 } catch (MalformedURLException e
) {
261 Instance
.syserr(new IOException(
262 "Cannot parse chapter " + i
+ " url: "
263 + (base
+ i
+ suffix
), e
));
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
) {
277 public URL
getValue() {
281 public String
getKey() {
291 protected String
getChapterContent(URL source
, InputStream in
, int number
,
293 StringBuilder builder
= new StringBuilder();
294 String startAt
= "class='storytext ";
295 String endAt1
= "function review_init";
296 String endAt2
= "id=chap_select";
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
)) {
306 } else if (ok
&& (line
.contains(endAt1
) || line
.contains(endAt2
))) {
312 // First line may contain the title and chap name again
313 if (builder
.length() == 0) {
314 int pos
= line
.indexOf("<hr");
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
+ " ");
329 line
= line
.substring(pos
);
334 builder
.append(line
);
339 return builder
.toString();
343 protected boolean supports(URL url
) {
344 return "fanfiction.net".equals(url
.getHost())
345 || "www.fanfiction.net".equals(url
.getHost());