9be098d6b8bd67e5a419a10acd8fedc52ee5d695
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
.setPublisher(getSourceName());
47 meta
.setUuid(source
.toString());
50 meta
.setSubject(getSubject(reset(in
)));
51 meta
.setType(getType().toString());
52 meta
.setImageDocument(false);
53 meta
.setCover(getCover(source
, reset(in
)));
58 private String
getSubject(InputStream in
) {
59 String line
= getLine(in
, "id=pre_story_links", 0);
61 int pos
= line
.lastIndexOf('"');
63 line
= line
.substring(pos
+ 1);
64 pos
= line
.indexOf('<');
66 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
74 private List
<String
> getTags(InputStream in
) throws IOException
{
75 List
<String
> tags
= new ArrayList
<String
>();
77 String key
= "title=\"Send Private Message\"";
78 String line
= getLine(in
, key
, 2);
81 int pos
= line
.indexOf(key
);
83 line
= line
.substring(pos
+ key
.length());
85 pos
= line
.indexOf(key
);
87 line
= line
.substring(0, pos
);
88 line
= StringUtils
.unhtml(line
).trim();
89 if (line
.endsWith("-")) {
90 line
= line
.substring(0, line
.length() - 1);
93 for (String tag
: line
.split("-")) {
94 tags
.add(StringUtils
.unhtml(tag
).trim());
103 private String
getTitle(InputStream in
) {
105 @SuppressWarnings("resource")
106 Scanner scan
= new Scanner(in
, "UTF-8");
107 scan
.useDelimiter("\\n");
108 while (scan
.hasNext()) {
109 String line
= scan
.next();
110 if (line
.contains("xcontrast_txt")) {
112 line
= StringUtils
.unhtml(line
).trim();
113 if (line
.startsWith("Follow/Fav")) {
114 line
= line
.substring("Follow/Fav".length()).trim();
117 return StringUtils
.unhtml(line
).trim();
125 private String
getAuthor(InputStream in
) {
127 @SuppressWarnings("resource")
128 Scanner scan
= new Scanner(in
, "UTF-8");
129 scan
.useDelimiter("\\n");
130 while (scan
.hasNext()) {
131 String line
= scan
.next();
132 if (line
.contains("xcontrast_txt")) {
134 return StringUtils
.unhtml(line
).trim();
142 private String
getDate(InputStream in
) {
143 String key
= "Published: <span data-xutime='";
144 String line
= getLine(in
, key
, 0);
146 int pos
= line
.indexOf(key
);
148 line
= line
.substring(pos
+ key
.length());
149 pos
= line
.indexOf('\'');
151 line
= line
.substring(0, pos
).trim();
153 SimpleDateFormat sdf
= new SimpleDateFormat(
156 .format(new Date(1000 * Long
.parseLong(line
)));
157 } catch (NumberFormatException e
) {
158 Instance
.syserr(new IOException(
159 "Cannot convert publication date: " + line
, e
));
169 protected String
getDesc(URL source
, InputStream in
) {
170 return getLine(in
, "title=\"Send Private Message\"", 1);
173 private BufferedImage
getCover(URL url
, InputStream in
) {
174 String key
= "class='cimage";
175 String line
= getLine(in
, key
, 0);
177 int pos
= line
.indexOf(key
);
179 line
= line
.substring(pos
+ key
.length());
181 pos
= line
.indexOf(key
);
183 line
= line
.substring(pos
+ key
.length());
184 pos
= line
.indexOf('\'');
186 line
= line
.substring(0, pos
);
187 if (line
.startsWith("//")) {
188 line
= url
.getProtocol() + "://"
190 } else if (line
.startsWith("//")) {
191 line
= url
.getProtocol() + "://" + url
.getHost()
192 + "/" + line
.substring(1);
194 line
= url
.getProtocol() + "://" + url
.getHost()
195 + "/" + url
.getPath() + "/" + line
;
198 return getImage(null, line
);
208 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
) {
209 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
211 String base
= source
.toString();
212 int pos
= base
.lastIndexOf('/');
213 String suffix
= base
.substring(pos
); // including '/' at start
214 base
= base
.substring(0, pos
);
215 if (base
.endsWith("/1")) {
216 base
= base
.substring(0, base
.length() - 1); // including '/' at end
219 String line
= getLine(in
, "id=chap_select", 0);
220 String key
= "<option value=";
222 for (pos
= line
.indexOf(key
); pos
>= 0; pos
= line
.indexOf(key
, pos
), i
++) {
223 pos
= line
.indexOf('>', pos
);
225 int endOfName
= line
.indexOf('<', pos
);
226 if (endOfName
>= 0) {
227 String name
= line
.substring(pos
+ 1, endOfName
);
228 String chapNum
= i
+ ".";
229 if (name
.startsWith(chapNum
)) {
230 name
= name
.substring(chapNum
.length(), name
.length());
234 final String chapName
= name
.trim();
235 final URL chapURL
= new URL(base
+ i
+ suffix
);
236 urls
.add(new Entry
<String
, URL
>() {
237 public URL
setValue(URL value
) {
241 public URL
getValue() {
245 public String
getKey() {
249 } catch (MalformedURLException e
) {
250 Instance
.syserr(new IOException("Cannot parse chapter "
251 + i
+ " url: " + (base
+ i
+ suffix
), e
));
261 protected String
getChapterContent(URL source
, InputStream in
, int number
) {
262 StringBuilder builder
= new StringBuilder();
263 String startAt
= "class='storytext ";
264 String endAt1
= "function review_init";
265 String endAt2
= "id=chap_select";
268 @SuppressWarnings("resource")
269 Scanner scan
= new Scanner(in
, "UTF-8");
270 scan
.useDelimiter("\\n");
271 while (scan
.hasNext()) {
272 String line
= scan
.next();
273 if (!ok
&& line
.contains(startAt
)) {
275 } else if (ok
&& (line
.contains(endAt1
) || line
.contains(endAt2
))) {
281 // First line may contain the title and chap name again
282 if (builder
.length() == 0) {
283 int pos
= line
.indexOf("<hr");
285 line
= line
.substring(pos
);
289 builder
.append(line
);
293 return builder
.toString();
297 protected boolean supports(URL url
) {
298 return "fanfiction.net".equals(url
.getHost())
299 || "www.fanfiction.net".equals(url
.getHost());