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
.setUrl(source
.toString());
42 meta
.setUuid(source
.toString());
45 meta
.setSubject("MLP");
46 meta
.setImageDocument(false);
47 meta
.setCover(getCover(reset(in
)));
53 public Map
<String
, String
> getCookies() {
54 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
55 cookies
.put("view_mature", "true");
59 private List
<String
> getTags(InputStream in
) {
60 List
<String
> tags
= new ArrayList
<String
>();
63 @SuppressWarnings("resource")
64 Scanner scan
= new Scanner(in
, "UTF-8");
65 scan
.useDelimiter("\\n");
66 boolean started
= false;
67 while (scan
.hasNext()) {
68 String line
= scan
.next();
71 started
= line
.contains("\"story_container\"");
74 if (started
&& line
.contains("class=\"tag-")) {
75 if (line
.contains("index.php")) {
76 break; // end of *this story* tags
79 String keyword
= "title=\"";
80 Scanner tagScanner
= new Scanner(line
);
81 tagScanner
.useDelimiter(keyword
);
82 if (tagScanner
.hasNext()) {
83 tagScanner
.next();// Ignore first one
85 while (tagScanner
.hasNext()) {
86 String tag
= tagScanner
.next();
87 if (tag
.contains("\"")) {
88 tag
= tag
.split("\"")[0];
89 tag
= StringUtils
.unhtml(tag
).trim();
90 if (!tag
.isEmpty() && !tags
.contains(tag
)) {
102 private String
getTitle(InputStream in
) {
103 String line
= getLine(in
, " property=\"og:title\"", 0);
106 for (int i
= 0; i
< 3; i
++) {
107 pos
= line
.indexOf('"', pos
+ 1);
111 line
= line
.substring(pos
+ 1);
112 pos
= line
.indexOf('"');
114 return StringUtils
.unhtml(line
.substring(0, pos
)).trim();
122 private String
getAuthor(InputStream in
) {
123 String line
= getLine(in
, " href=\"/user/", 0);
125 int pos
= line
.indexOf('"');
127 line
= line
.substring(pos
+ 1);
128 pos
= line
.indexOf('"');
130 line
= line
.substring(0, pos
);
131 pos
= line
.lastIndexOf('/');
133 line
= line
.substring(pos
+ 1);
134 return line
.replace('+', ' ');
143 private String
getDate(InputStream in
) {
144 String line
= getLine(in
, "<span class=\"date\">", 0);
147 for (int i
= 0; i
< 3; i
++) {
148 pos
= line
.indexOf('>', pos
+ 1);
152 line
= line
.substring(pos
+ 1);
153 pos
= line
.indexOf('<');
155 return line
.substring(0, pos
).trim();
164 protected String
getDesc(URL source
, InputStream in
) {
165 // the og: meta version is the SHORT resume, this is the LONG resume
166 return getLine(in
, "class=\"description-text bbcode\"", 1);
169 private Image
getCover(InputStream in
) {
170 // Note: the 'og:image' is the SMALL cover, not the full version
171 String cover
= getLine(in
, "class=\"story_container__story_image\"", 1);
173 int pos
= cover
.indexOf('"');
175 cover
= cover
.substring(pos
+ 1);
176 pos
= cover
.indexOf('"');
178 cover
= cover
.substring(0, pos
);
183 return getImage(this, null, cover
);
187 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
189 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
190 @SuppressWarnings("resource")
191 Scanner scan
= new Scanner(in
, "UTF-8");
192 scan
.useDelimiter("\\n");
193 boolean started
= false;
194 while (scan
.hasNext()) {
195 String line
= scan
.next().trim();
198 started
= line
.equals("<!--Chapters-->");
200 if (line
.equals("</form>")) {
204 if (line
.startsWith("<a href=")
205 || line
.contains("class=\"chapter-title\"")) {
208 int pos
= name
.indexOf('>');
210 name
= name
.substring(pos
+ 1);
211 pos
= name
.indexOf('<');
213 name
= name
.substring(0, pos
);
217 pos
= line
.indexOf('/');
219 line
= line
.substring(pos
); // we take the /, not +1
220 pos
= line
.indexOf('"');
222 line
= line
.substring(0, pos
);
227 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(name
,
228 new URL("http://www.fimfiction.net" + line
)));
229 } catch (MalformedURLException e
) {
230 Instance
.getInstance().getTraceHandler().error(e
);
240 protected String
getChapterContent(URL source
, InputStream in
, int number
,
242 return getLine(in
, "<div class=\"bbcode\">", 1);
246 protected boolean supports(URL url
) {
247 return "fimfiction.net".equals(url
.getHost())
248 || "www.fimfiction.net".equals(url
.getHost());