2cbe696052dc16ec61e3ba333ac50c8180486aaf
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
.data
.MetaData
;
17 import be
.nikiroo
.utils
.StringUtils
;
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.
26 class Fanfiction
extends BasicSupport
{
28 protected boolean isHtml() {
33 public String
getSourceName() {
34 return "Fanfiction.net";
38 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
39 MetaData meta
= new MetaData();
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());
46 meta
.setUrl(source
.toString());
47 meta
.setPublisher(getSourceName());
48 meta
.setUuid(source
.toString());
51 meta
.setSubject(getSubject(reset(in
)));
52 meta
.setType(getType().toString());
53 meta
.setImageDocument(false);
54 meta
.setCover(getCover(source
, reset(in
)));
59 private String
getSubject(InputStream in
) {
60 String line
= getLine(in
, "id=pre_story_links", 0);
62 int pos
= line
.lastIndexOf('"');
64 line
= line
.substring(pos
+ 1);
65 pos
= line
.indexOf('<');
67 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
75 private List
<String
> getTags(InputStream in
) throws IOException
{
76 List
<String
> tags
= new ArrayList
<String
>();
78 String key
= "title=\"Send Private Message\"";
79 String line
= getLine(in
, key
, 2);
82 int pos
= line
.indexOf(key
);
84 line
= line
.substring(pos
+ key
.length());
86 pos
= line
.indexOf(key
);
88 line
= line
.substring(0, pos
);
89 line
= StringUtils
.unhtml(line
).trim();
90 if (line
.endsWith("-")) {
91 line
= line
.substring(0, line
.length() - 1);
94 for (String tag
: line
.split("-")) {
95 tags
.add(StringUtils
.unhtml(tag
).trim());
104 private String
getTitle(InputStream in
) {
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")) {
113 line
= StringUtils
.unhtml(line
).trim();
114 if (line
.startsWith("Follow/Fav")) {
115 line
= line
.substring("Follow/Fav".length()).trim();
118 return StringUtils
.unhtml(line
).trim();
126 private String
getAuthor(InputStream in
) {
128 @SuppressWarnings("resource")
129 Scanner scan
= new Scanner(in
, "UTF-8");
130 scan
.useDelimiter("\\n");
131 while (scan
.hasNext()) {
132 String line
= scan
.next();
133 if (line
.contains("xcontrast_txt")) {
135 return StringUtils
.unhtml(line
).trim();
143 private String
getDate(InputStream in
) {
144 String key
= "Published: <span data-xutime='";
145 String line
= getLine(in
, key
, 0);
147 int pos
= line
.indexOf(key
);
149 line
= line
.substring(pos
+ key
.length());
150 pos
= line
.indexOf('\'');
152 line
= line
.substring(0, pos
).trim();
154 SimpleDateFormat sdf
= new SimpleDateFormat(
157 .format(new Date(1000 * Long
.parseLong(line
)));
158 } catch (NumberFormatException e
) {
159 Instance
.syserr(new IOException(
160 "Cannot convert publication date: " + line
, e
));
170 protected String
getDesc(URL source
, InputStream in
) {
171 return getLine(in
, "title=\"Send Private Message\"", 1);
174 private BufferedImage
getCover(URL url
, InputStream in
) {
175 String key
= "class='cimage";
176 String line
= getLine(in
, key
, 0);
178 int pos
= line
.indexOf(key
);
180 line
= line
.substring(pos
+ key
.length());
182 pos
= line
.indexOf(key
);
184 line
= line
.substring(pos
+ key
.length());
185 pos
= line
.indexOf('\'');
187 line
= line
.substring(0, pos
);
188 if (line
.startsWith("//")) {
189 line
= url
.getProtocol() + "://"
191 } else if (line
.startsWith("//")) {
192 line
= url
.getProtocol() + "://" + url
.getHost()
193 + "/" + line
.substring(1);
195 line
= url
.getProtocol() + "://" + url
.getHost()
196 + "/" + url
.getPath() + "/" + line
;
199 return getImage(null, line
);
209 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
) {
210 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
212 String base
= source
.toString();
213 int pos
= base
.lastIndexOf('/');
214 String suffix
= base
.substring(pos
); // including '/' at start
215 base
= base
.substring(0, pos
);
216 if (base
.endsWith("/1")) {
217 base
= base
.substring(0, base
.length() - 1); // including '/' at end
220 String line
= getLine(in
, "id=chap_select", 0);
221 String key
= "<option value=";
223 for (pos
= line
.indexOf(key
); pos
>= 0; pos
= line
.indexOf(key
, pos
), i
++) {
224 pos
= line
.indexOf('>', pos
);
226 int endOfName
= line
.indexOf('<', pos
);
227 if (endOfName
>= 0) {
228 String name
= line
.substring(pos
+ 1, endOfName
);
229 String chapNum
= i
+ ".";
230 if (name
.startsWith(chapNum
)) {
231 name
= name
.substring(chapNum
.length(), name
.length());
235 final String chapName
= name
.trim();
236 final URL chapURL
= new URL(base
+ i
+ suffix
);
237 urls
.add(new Entry
<String
, URL
>() {
238 public URL
setValue(URL value
) {
242 public URL
getValue() {
246 public String
getKey() {
250 } catch (MalformedURLException e
) {
251 Instance
.syserr(new IOException("Cannot parse chapter "
252 + i
+ " url: " + (base
+ i
+ suffix
), e
));
262 protected String
getChapterContent(URL source
, InputStream in
, int number
) {
263 StringBuilder builder
= new StringBuilder();
264 String startAt
= "class='storytext ";
265 String endAt1
= "function review_init";
266 String endAt2
= "id=chap_select";
269 @SuppressWarnings("resource")
270 Scanner scan
= new Scanner(in
, "UTF-8");
271 scan
.useDelimiter("\\n");
272 while (scan
.hasNext()) {
273 String line
= scan
.next();
274 if (!ok
&& line
.contains(startAt
)) {
276 } else if (ok
&& (line
.contains(endAt1
) || line
.contains(endAt2
))) {
282 // First line may contain the title and chap name again
283 if (builder
.length() == 0) {
284 int pos
= line
.indexOf("<hr");
286 line
= line
.substring(pos
);
290 builder
.append(line
);
294 return builder
.toString();
298 protected boolean supports(URL url
) {
299 return "fanfiction.net".equals(url
.getHost())
300 || "www.fanfiction.net".equals(url
.getHost());