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
.setUrl(source
.toString());
46 meta
.setPublisher(getSourceName());
47 meta
.setUuid(source
.toString());
50 meta
.setSubject("MLP");
51 meta
.setType(getType().toString());
52 meta
.setImageDocument(false);
53 meta
.setCover(getCover(reset(in
)));
59 public Map
<String
, String
> getCookies() {
60 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
61 cookies
.put("view_mature", "true");
65 private List
<String
> getTags(InputStream in
) {
66 List
<String
> tags
= new ArrayList
<String
>();
69 @SuppressWarnings("resource")
70 Scanner scan
= new Scanner(in
, "UTF-8");
71 scan
.useDelimiter("\\n");
72 while (scan
.hasNext()) {
73 String line
= scan
.next();
74 if (line
.contains("story_category") && !line
.contains("title=")) {
75 int pos
= line
.indexOf('>');
77 line
= line
.substring(pos
+ 1);
78 pos
= line
.indexOf('<');
80 line
= line
.substring(0, pos
);
85 if (!tags
.contains(line
)) {
94 private String
getTitle(InputStream in
) {
95 String line
= getLine(in
, " property=\"og:title\"", 0);
98 for (int i
= 0; i
< 3; i
++) {
99 pos
= line
.indexOf('"', pos
+ 1);
103 line
= line
.substring(pos
+ 1);
104 pos
= line
.indexOf('"');
106 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
114 private String
getAuthor(InputStream in
) {
115 String line
= getLine(in
, " href=\"/user/", 0);
117 int pos
= line
.indexOf('"');
119 line
= line
.substring(pos
+ 1);
120 pos
= line
.indexOf('"');
122 line
= line
.substring(0, pos
);
123 pos
= line
.lastIndexOf('/');
125 line
= line
.substring(pos
+ 1);
126 return line
.replace('+', ' ');
135 private String
getDate(InputStream in
) {
136 String line
= getLine(in
, "<span class=\"date\">", 0);
139 for (int i
= 0; i
< 3; i
++) {
140 pos
= line
.indexOf('>', pos
+ 1);
144 line
= line
.substring(pos
+ 1);
145 pos
= line
.indexOf('<');
147 return line
.substring(0, pos
).trim();
156 protected String
getDesc(URL source
, InputStream in
) {
157 // the og: meta version is the SHORT resume, this is the LONG resume
158 return getLine(in
, "class=\"more_button hidden\"", -1);
161 private BufferedImage
getCover(InputStream in
) {
162 // Note: the 'og:image' is the SMALL cover, not the full version
163 String cover
= getLine(in
, "<div class=\"story_image\">", 1);
165 int pos
= cover
.indexOf('"');
167 cover
= cover
.substring(pos
+ 1);
168 pos
= cover
.indexOf('"');
170 cover
= cover
.substring(0, pos
);
175 return getImage(this, null, cover
);
179 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
) {
180 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
181 @SuppressWarnings("resource")
182 Scanner scan
= new Scanner(in
, "UTF-8");
183 scan
.useDelimiter("\\n");
184 while (scan
.hasNext()) {
185 String line
= scan
.next();
186 if (line
.contains("class=\"chapter_link\"")
187 || line
.contains("class='chapter_link'")) {
190 int pos
= name
.indexOf('>');
192 name
= name
.substring(pos
+ 1);
193 pos
= name
.indexOf('<');
195 name
= name
.substring(0, pos
);
199 pos
= line
.indexOf('/');
201 line
= line
.substring(pos
); // we take the /, not +1
202 pos
= line
.indexOf('"');
204 line
= line
.substring(0, pos
);
209 final String key
= name
;
210 final URL value
= new URL("http://www.fimfiction.net"
212 urls
.add(new Entry
<String
, URL
>() {
213 public URL
setValue(URL value
) {
217 public String
getKey() {
221 public URL
getValue() {
225 } catch (MalformedURLException e
) {
235 protected String
getChapterContent(URL source
, InputStream in
, int number
) {
236 return getLine(in
, "<div id=\"chapter_container\">", 1);
240 protected boolean supports(URL url
) {
241 return "fimfiction.net".equals(url
.getHost())
242 || "www.fimfiction.net".equals(url
.getHost());