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
.util
.ArrayList
;
9 import java
.util
.HashMap
;
10 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.fimfiction.net/">FimFiction.net</a>
21 * stories, a website dedicated to My Little Pony.
25 class Fimfiction
extends BasicSupport
{
27 protected boolean isHtml() {
32 public String
getSourceName() {
33 return "FimFiction.net";
37 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
38 MetaData meta
= new MetaData();
40 meta
.setTitle(getTitle(reset(in
)));
41 meta
.setAuthor(getAuthor(reset(in
)));
42 meta
.setDate(getDate(reset(in
)));
43 meta
.setTags(getTags(reset(in
)));
44 meta
.setSource(getSourceName());
45 meta
.setPublisher(getSourceName());
46 meta
.setUuid(source
.toString());
49 meta
.setSubject("MLP");
50 meta
.setType(getType().toString());
51 meta
.setImageDocument(false);
52 meta
.setCover(getCover(reset(in
)));
58 public Map
<String
, String
> getCookies() {
59 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
60 cookies
.put("view_mature", "true");
64 private List
<String
> getTags(InputStream in
) {
65 List
<String
> tags
= new ArrayList
<String
>();
68 @SuppressWarnings("resource")
69 Scanner scan
= new Scanner(in
, "UTF-8");
70 scan
.useDelimiter("\\n");
71 while (scan
.hasNext()) {
72 String line
= scan
.next();
73 if (line
.contains("story_category") && !line
.contains("title=")) {
74 int pos
= line
.indexOf('>');
76 line
= line
.substring(pos
+ 1);
77 pos
= line
.indexOf('<');
79 line
= line
.substring(0, pos
);
84 if (!tags
.contains(line
)) {
93 private String
getTitle(InputStream in
) {
94 String line
= getLine(in
, " property=\"og:title\"", 0);
97 for (int i
= 0; i
< 3; i
++) {
98 pos
= line
.indexOf('"', pos
+ 1);
102 line
= line
.substring(pos
+ 1);
103 pos
= line
.indexOf('"');
105 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
113 private String
getAuthor(InputStream in
) {
114 String line
= getLine(in
, " href=\"/user/", 0);
116 int pos
= line
.indexOf('"');
118 line
= line
.substring(pos
+ 1);
119 pos
= line
.indexOf('"');
121 line
= line
.substring(0, pos
);
122 pos
= line
.lastIndexOf('/');
124 line
= line
.substring(pos
+ 1);
125 return line
.replace('+', ' ');
134 private String
getDate(InputStream in
) {
135 String line
= getLine(in
, "<span class=\"date\">", 0);
138 for (int i
= 0; i
< 3; i
++) {
139 pos
= line
.indexOf('>', pos
+ 1);
143 line
= line
.substring(pos
+ 1);
144 pos
= line
.indexOf('<');
146 return line
.substring(0, pos
).trim();
155 protected String
getDesc(URL source
, InputStream in
) {
156 // the og: meta version is the SHORT resume, this is the LONG resume
157 return getLine(in
, "class=\"more_button hidden\"", -1);
160 private BufferedImage
getCover(InputStream in
) {
161 // Note: the 'og:image' is the SMALL cover, not the full version
162 String cover
= getLine(in
, "<div class=\"story_image\">", 1);
164 int pos
= cover
.indexOf('"');
166 cover
= cover
.substring(pos
+ 1);
167 pos
= cover
.indexOf('"');
169 cover
= cover
.substring(0, pos
);
174 return getImage(null, cover
);
178 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
) {
179 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
180 @SuppressWarnings("resource")
181 Scanner scan
= new Scanner(in
, "UTF-8");
182 scan
.useDelimiter("\\n");
183 while (scan
.hasNext()) {
184 String line
= scan
.next();
185 if (line
.contains("class=\"chapter_link\"")
186 || line
.contains("class='chapter_link'")) {
189 int pos
= name
.indexOf('>');
191 name
= name
.substring(pos
+ 1);
192 pos
= name
.indexOf('<');
194 name
= name
.substring(0, pos
);
198 pos
= line
.indexOf('/');
200 line
= line
.substring(pos
); // we take the /, not +1
201 pos
= line
.indexOf('"');
203 line
= line
.substring(0, pos
);
208 final String key
= name
;
209 final URL value
= new URL("http://www.fimfiction.net"
211 urls
.add(new Entry
<String
, URL
>() {
212 public URL
setValue(URL value
) {
216 public String
getKey() {
220 public URL
getValue() {
224 } catch (MalformedURLException e
) {
234 protected String
getChapterContent(URL source
, InputStream in
, int number
) {
235 return getLine(in
, "<div id=\"chapter_container\">", 1);
239 protected boolean supports(URL url
) {
240 return "fimfiction.net".equals(url
.getHost())
241 || "www.fimfiction.net".equals(url
.getHost());