1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.AbstractMap
;
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
.Image
;
18 import be
.nikiroo
.utils
.Progress
;
19 import be
.nikiroo
.utils
.StringUtils
;
22 * Support class for <a href="http://www.fimfiction.net/">FimFiction.net</a>
23 * stories, a website dedicated to My Little Pony.
27 class Fimfiction
extends BasicSupport_Deprecated
{
29 protected boolean isHtml() {
34 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
35 MetaData meta
= new MetaData();
37 meta
.setTitle(getTitle(reset(in
)));
38 meta
.setAuthor(getAuthor(reset(in
)));
39 meta
.setDate(getDate(reset(in
)));
40 meta
.setTags(getTags(reset(in
)));
41 meta
.setSource(getType().getSourceName());
42 meta
.setUrl(source
.toString());
43 meta
.setPublisher(getType().getSourceName());
44 meta
.setUuid(source
.toString());
47 meta
.setSubject("MLP");
48 meta
.setType(getType().toString());
49 meta
.setImageDocument(false);
50 meta
.setCover(getCover(reset(in
)));
56 public Map
<String
, String
> getCookies() {
57 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
58 cookies
.put("view_mature", "true");
62 private List
<String
> getTags(InputStream in
) {
63 List
<String
> tags
= new ArrayList
<String
>();
66 @SuppressWarnings("resource")
67 Scanner scan
= new Scanner(in
, "UTF-8");
68 scan
.useDelimiter("\\n");
69 boolean started
= false;
70 while (scan
.hasNext()) {
71 String line
= scan
.next();
74 started
= line
.contains("\"story_container\"");
77 if (started
&& line
.contains("class=\"tag-")) {
78 if (line
.contains("index.php")) {
79 break; // end of *this story* tags
82 String keyword
= "title=\"";
83 Scanner tagScanner
= new Scanner(line
);
84 tagScanner
.useDelimiter(keyword
);
85 if (tagScanner
.hasNext()) {
86 tagScanner
.next();// Ignore first one
88 while (tagScanner
.hasNext()) {
89 String tag
= tagScanner
.next();
90 if (tag
.contains("\"")) {
91 tag
= tag
.split("\"")[0];
92 tag
= StringUtils
.unhtml(tag
).trim();
93 if (!tag
.isEmpty() && !tags
.contains(tag
)) {
105 private String
getTitle(InputStream in
) {
106 String line
= getLine(in
, " property=\"og:title\"", 0);
109 for (int i
= 0; i
< 3; i
++) {
110 pos
= line
.indexOf('"', pos
+ 1);
114 line
= line
.substring(pos
+ 1);
115 pos
= line
.indexOf('"');
117 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
125 private String
getAuthor(InputStream in
) {
126 String line
= getLine(in
, " href=\"/user/", 0);
128 int pos
= line
.indexOf('"');
130 line
= line
.substring(pos
+ 1);
131 pos
= line
.indexOf('"');
133 line
= line
.substring(0, pos
);
134 pos
= line
.lastIndexOf('/');
136 line
= line
.substring(pos
+ 1);
137 return line
.replace('+', ' ');
146 private String
getDate(InputStream in
) {
147 String line
= getLine(in
, "<span class=\"date\">", 0);
150 for (int i
= 0; i
< 3; i
++) {
151 pos
= line
.indexOf('>', pos
+ 1);
155 line
= line
.substring(pos
+ 1);
156 pos
= line
.indexOf('<');
158 return line
.substring(0, pos
).trim();
167 protected String
getDesc(URL source
, InputStream in
) {
168 // the og: meta version is the SHORT resume, this is the LONG resume
169 return getLine(in
, "class=\"description-text bbcode\"", 1);
172 private Image
getCover(InputStream in
) {
173 // Note: the 'og:image' is the SMALL cover, not the full version
174 String cover
= getLine(in
, "class=\"story_container__story_image\"", 1);
176 int pos
= cover
.indexOf('"');
178 cover
= cover
.substring(pos
+ 1);
179 pos
= cover
.indexOf('"');
181 cover
= cover
.substring(0, pos
);
186 return getImage(this, null, cover
);
190 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
192 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
193 @SuppressWarnings("resource")
194 Scanner scan
= new Scanner(in
, "UTF-8");
195 scan
.useDelimiter("\\n");
196 boolean started
= false;
197 while (scan
.hasNext()) {
198 String line
= scan
.next().trim();
201 started
= line
.equals("<!--Chapters-->");
203 if (line
.equals("</form>")) {
207 if (line
.startsWith("<a href=")
208 || line
.contains("class=\"chapter-title\"")) {
211 int pos
= name
.indexOf('>');
213 name
= name
.substring(pos
+ 1);
214 pos
= name
.indexOf('<');
216 name
= name
.substring(0, pos
);
220 pos
= line
.indexOf('/');
222 line
= line
.substring(pos
); // we take the /, not +1
223 pos
= line
.indexOf('"');
225 line
= line
.substring(0, pos
);
230 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(name
,
231 new URL("http://www.fimfiction.net" + line
)));
232 } catch (MalformedURLException e
) {
233 Instance
.getInstance().getTraceHandler().error(e
);
243 protected String
getChapterContent(URL source
, InputStream in
, int number
,
245 return getLine(in
, "<div class=\"bbcode\">", 1);
249 protected boolean supports(URL url
) {
250 return "fimfiction.net".equals(url
.getHost())
251 || "www.fimfiction.net".equals(url
.getHost());