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 boolean started
= false;
74 while (scan
.hasNext()) {
75 String line
= scan
.next();
78 started
= line
.contains("\"story_container\"");
81 if (started
&& line
.contains("class=\"tag-")) {
82 if (line
.contains("index.php")) {
83 break; // end of *this story* tags
86 String keyword
= "title=\"";
87 Scanner tagScanner
= new Scanner(line
);
88 tagScanner
.useDelimiter(keyword
);
89 if (tagScanner
.hasNext()) {
90 tagScanner
.next();// Ignore first one
92 while (tagScanner
.hasNext()) {
93 String tag
= tagScanner
.next();
94 if (tag
.contains("\"")) {
95 tag
= tag
.split("\"")[0];
96 tag
= StringUtils
.unhtml(tag
).trim();
97 if (!tag
.isEmpty() && !tags
.contains(tag
)) {
109 private String
getTitle(InputStream in
) {
110 String line
= getLine(in
, " property=\"og:title\"", 0);
113 for (int i
= 0; i
< 3; i
++) {
114 pos
= line
.indexOf('"', pos
+ 1);
118 line
= line
.substring(pos
+ 1);
119 pos
= line
.indexOf('"');
121 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
129 private String
getAuthor(InputStream in
) {
130 String line
= getLine(in
, " href=\"/user/", 0);
132 int pos
= line
.indexOf('"');
134 line
= line
.substring(pos
+ 1);
135 pos
= line
.indexOf('"');
137 line
= line
.substring(0, pos
);
138 pos
= line
.lastIndexOf('/');
140 line
= line
.substring(pos
+ 1);
141 return line
.replace('+', ' ');
150 private String
getDate(InputStream in
) {
151 String line
= getLine(in
, "<span class=\"date\">", 0);
154 for (int i
= 0; i
< 3; i
++) {
155 pos
= line
.indexOf('>', pos
+ 1);
159 line
= line
.substring(pos
+ 1);
160 pos
= line
.indexOf('<');
162 return line
.substring(0, pos
).trim();
171 protected String
getDesc(URL source
, InputStream in
) {
172 // the og: meta version is the SHORT resume, this is the LONG resume
173 return getLine(in
, "class=\"description-text bbcode\"", 1);
176 private BufferedImage
getCover(InputStream in
) {
177 // Note: the 'og:image' is the SMALL cover, not the full version
178 String cover
= getLine(in
, "class=\"story_container__story_image\"", 1);
180 int pos
= cover
.indexOf('"');
182 cover
= cover
.substring(pos
+ 1);
183 pos
= cover
.indexOf('"');
185 cover
= cover
.substring(0, pos
);
190 return getImage(this, null, cover
);
194 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
196 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
197 @SuppressWarnings("resource")
198 Scanner scan
= new Scanner(in
, "UTF-8");
199 scan
.useDelimiter("\\n");
200 boolean started
= false;
201 while (scan
.hasNext()) {
202 String line
= scan
.next().trim();
205 started
= line
.equals("<!--Chapters-->");
207 if (line
.equals("</form>")) {
211 if (line
.startsWith("<a href=")
212 || line
.contains("class=\"chapter-title\"")) {
215 int pos
= name
.indexOf('>');
217 name
= name
.substring(pos
+ 1);
218 pos
= name
.indexOf('<');
220 name
= name
.substring(0, pos
);
224 pos
= line
.indexOf('/');
226 line
= line
.substring(pos
); // we take the /, not +1
227 pos
= line
.indexOf('"');
229 line
= line
.substring(0, pos
);
234 final String key
= name
;
235 final URL value
= new URL("http://www.fimfiction.net"
237 urls
.add(new Entry
<String
, URL
>() {
239 public URL
setValue(URL value
) {
244 public String
getKey() {
249 public URL
getValue() {
253 } catch (MalformedURLException e
) {
264 protected String
getChapterContent(URL source
, InputStream in
, int number
,
266 return getLine(in
, "<div class=\"bbcode\">", 1);
270 protected boolean supports(URL url
) {
271 return "fimfiction.net".equals(url
.getHost())
272 || "www.fimfiction.net".equals(url
.getHost());