Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / supported / Fimfiction.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.AbstractMap;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Map.Entry;
13 import java.util.Scanner;
14
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;
20
21 /**
22 * Support class for <a href="http://www.fimfiction.net/">FimFiction.net</a>
23 * stories, a website dedicated to My Little Pony.
24 *
25 * @author niki
26 */
27 class Fimfiction extends BasicSupport_Deprecated {
28 @Override
29 protected boolean isHtml() {
30 return true;
31 }
32
33 @Override
34 protected MetaData getMeta(URL source, InputStream in) throws IOException {
35 MetaData meta = new MetaData();
36
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());
43 meta.setLuid("");
44 meta.setLang("en");
45 meta.setSubject("MLP");
46 meta.setImageDocument(false);
47 meta.setCover(getCover(reset(in)));
48
49 return meta;
50 }
51
52 @Override
53 public Map<String, String> getCookies() {
54 Map<String, String> cookies = new HashMap<String, String>();
55 cookies.put("view_mature", "true");
56 return cookies;
57 }
58
59 private List<String> getTags(InputStream in) {
60 List<String> tags = new ArrayList<String>();
61 tags.add("MLP");
62
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();
69
70 if (!started) {
71 started = line.contains("\"story_container\"");
72 }
73
74 if (started && line.contains("class=\"tag-")) {
75 if (line.contains("index.php")) {
76 break; // end of *this story* tags
77 }
78
79 String keyword = "title=\"";
80 Scanner tagScanner = new Scanner(line);
81 tagScanner.useDelimiter(keyword);
82 if (tagScanner.hasNext()) {
83 tagScanner.next();// Ignore first one
84 }
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)) {
91 tags.add(tag);
92 }
93 }
94 }
95 tagScanner.close();
96 }
97 }
98
99 return tags;
100 }
101
102 private String getTitle(InputStream in) {
103 String line = getLine(in, " property=\"og:title\"", 0);
104 if (line != null) {
105 int pos = -1;
106 for (int i = 0; i < 3; i++) {
107 pos = line.indexOf('"', pos + 1);
108 }
109
110 if (pos >= 0) {
111 line = line.substring(pos + 1);
112 pos = line.indexOf('"');
113 if (pos >= 0) {
114 return StringUtils.unhtml(line.substring(0, pos)).trim();
115 }
116 }
117 }
118
119 return null;
120 }
121
122 private String getAuthor(InputStream in) {
123 String line = getLine(in, " href=\"/user/", 0);
124 if (line != null) {
125 int pos = line.indexOf('"');
126 if (pos >= 0) {
127 line = line.substring(pos + 1);
128 pos = line.indexOf('"');
129 if (pos >= 0) {
130 line = line.substring(0, pos);
131 pos = line.lastIndexOf('/');
132 if (pos >= 0) {
133 line = line.substring(pos + 1);
134 return line.replace('+', ' ');
135 }
136 }
137 }
138 }
139
140 return null;
141 }
142
143 private String getDate(InputStream in) {
144 String line = getLine(in, "<span class=\"date\">", 0);
145 if (line != null) {
146 int pos = -1;
147 for (int i = 0; i < 3; i++) {
148 pos = line.indexOf('>', pos + 1);
149 }
150
151 if (pos >= 0) {
152 line = line.substring(pos + 1);
153 pos = line.indexOf('<');
154 if (pos >= 0) {
155 return line.substring(0, pos).trim();
156 }
157 }
158 }
159
160 return null;
161 }
162
163 @Override
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);
167 }
168
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);
172 if (cover != null) {
173 int pos = cover.indexOf('"');
174 if (pos >= 0) {
175 cover = cover.substring(pos + 1);
176 pos = cover.indexOf('"');
177 if (pos >= 0) {
178 cover = cover.substring(0, pos);
179 }
180 }
181 }
182
183 return getImage(this, null, cover);
184 }
185
186 @Override
187 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
188 Progress pg) {
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();
196
197 if (!started) {
198 started = line.equals("<!--Chapters-->");
199 } else {
200 if (line.equals("</form>")) {
201 break;
202 }
203
204 if (line.startsWith("<a href=")
205 || line.contains("class=\"chapter-title\"")) {
206 // Chapter name
207 String name = line;
208 int pos = name.indexOf('>');
209 if (pos >= 0) {
210 name = name.substring(pos + 1);
211 pos = name.indexOf('<');
212 if (pos >= 0) {
213 name = name.substring(0, pos);
214 }
215 }
216 // Chapter content
217 pos = line.indexOf('/');
218 if (pos >= 0) {
219 line = line.substring(pos); // we take the /, not +1
220 pos = line.indexOf('"');
221 if (pos >= 0) {
222 line = line.substring(0, pos);
223 }
224 }
225
226 try {
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);
231 }
232 }
233 }
234 }
235
236 return urls;
237 }
238
239 @Override
240 protected String getChapterContent(URL source, InputStream in, int number,
241 Progress pg) {
242 return getLine(in, "<div class=\"bbcode\">", 1);
243 }
244
245 @Override
246 protected boolean supports(URL url) {
247 return "fimfiction.net".equals(url.getHost())
248 || "www.fimfiction.net".equals(url.getHost());
249 }
250 }