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
.Progress
;
18 import be
.nikiroo
.utils
.StringUtils
;
21 * Support class for <a href="http://www.fimfiction.net/">FimFiction.net</a>
22 * stories, a website dedicated to My Little Pony.
26 class Fimfiction
extends BasicSupport
{
28 protected boolean isHtml() {
33 public String
getSourceName() {
34 return "FimFiction.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("MLP");
52 meta
.setType(getType().toString());
53 meta
.setImageDocument(false);
54 meta
.setCover(getCover(reset(in
)));
60 public Map
<String
, String
> getCookies() {
61 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
62 cookies
.put("view_mature", "true");
66 private List
<String
> getTags(InputStream in
) {
67 List
<String
> tags
= new ArrayList
<String
>();
70 @SuppressWarnings("resource")
71 Scanner scan
= new Scanner(in
, "UTF-8");
72 scan
.useDelimiter("\\n");
73 while (scan
.hasNext()) {
74 String line
= scan
.next();
75 if (line
.contains("story_category") && !line
.contains("title=")) {
76 int pos
= line
.indexOf('>');
78 line
= line
.substring(pos
+ 1);
79 pos
= line
.indexOf('<');
81 line
= line
.substring(0, pos
);
86 if (!tags
.contains(line
)) {
95 private String
getTitle(InputStream in
) {
96 String line
= getLine(in
, " property=\"og:title\"", 0);
99 for (int i
= 0; i
< 3; i
++) {
100 pos
= line
.indexOf('"', pos
+ 1);
104 line
= line
.substring(pos
+ 1);
105 pos
= line
.indexOf('"');
107 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
115 private String
getAuthor(InputStream in
) {
116 String line
= getLine(in
, " href=\"/user/", 0);
118 int pos
= line
.indexOf('"');
120 line
= line
.substring(pos
+ 1);
121 pos
= line
.indexOf('"');
123 line
= line
.substring(0, pos
);
124 pos
= line
.lastIndexOf('/');
126 line
= line
.substring(pos
+ 1);
127 return line
.replace('+', ' ');
136 private String
getDate(InputStream in
) {
137 String line
= getLine(in
, "<span class=\"date\">", 0);
140 for (int i
= 0; i
< 3; i
++) {
141 pos
= line
.indexOf('>', pos
+ 1);
145 line
= line
.substring(pos
+ 1);
146 pos
= line
.indexOf('<');
148 return line
.substring(0, pos
).trim();
157 protected String
getDesc(URL source
, InputStream in
) {
158 // the og: meta version is the SHORT resume, this is the LONG resume
159 return getLine(in
, "class=\"more_button hidden\"", -1);
162 private BufferedImage
getCover(InputStream in
) {
163 // Note: the 'og:image' is the SMALL cover, not the full version
164 String cover
= getLine(in
, "<div class=\"story_image\">", 1);
166 int pos
= cover
.indexOf('"');
168 cover
= cover
.substring(pos
+ 1);
169 pos
= cover
.indexOf('"');
171 cover
= cover
.substring(0, pos
);
176 return getImage(this, null, cover
);
180 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
182 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
183 @SuppressWarnings("resource")
184 Scanner scan
= new Scanner(in
, "UTF-8");
185 scan
.useDelimiter("\\n");
186 while (scan
.hasNext()) {
187 String line
= scan
.next();
188 if (line
.contains("class=\"chapter_link\"")
189 || line
.contains("class='chapter_link'")) {
192 int pos
= name
.indexOf('>');
194 name
= name
.substring(pos
+ 1);
195 pos
= name
.indexOf('<');
197 name
= name
.substring(0, pos
);
201 pos
= line
.indexOf('/');
203 line
= line
.substring(pos
); // we take the /, not +1
204 pos
= line
.indexOf('"');
206 line
= line
.substring(0, pos
);
211 final String key
= name
;
212 final URL value
= new URL("http://www.fimfiction.net"
214 urls
.add(new Entry
<String
, URL
>() {
215 public URL
setValue(URL value
) {
219 public String
getKey() {
223 public URL
getValue() {
227 } catch (MalformedURLException e
) {
237 protected String
getChapterContent(URL source
, InputStream in
, int number
,
239 return getLine(in
, "<div id=\"chapter_container\">", 1);
243 protected boolean supports(URL url
) {
244 return "fimfiction.net".equals(url
.getHost())
245 || "www.fimfiction.net".equals(url
.getHost());