fix changelog headers
[fanfix.git] / Fimfiction.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.supported;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.MalformedURLException;
6import java.net.URL;
7import java.util.AbstractMap;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.Map.Entry;
13import java.util.Scanner;
14
15import be.nikiroo.fanfix.Instance;
16import be.nikiroo.fanfix.data.MetaData;
17import be.nikiroo.utils.Image;
18import be.nikiroo.utils.Progress;
19import 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 */
27class Fimfiction extends BasicSupport_Deprecated {
28 @Override
29 protected boolean isHtml() {
30 return true;
31 }
32
33 @Override
34 public String getSourceName() {
35 return "FimFiction.net";
36 }
37
38 @Override
39 protected MetaData getMeta(URL source, InputStream in) throws IOException {
40 MetaData meta = new MetaData();
41
42 meta.setTitle(getTitle(reset(in)));
43 meta.setAuthor(getAuthor(reset(in)));
44 meta.setDate(getDate(reset(in)));
45 meta.setTags(getTags(reset(in)));
46 meta.setSource(getSourceName());
47 meta.setUrl(source.toString());
48 meta.setPublisher(getSourceName());
49 meta.setUuid(source.toString());
50 meta.setLuid("");
51 meta.setLang("en");
52 meta.setSubject("MLP");
53 meta.setType(getType().toString());
54 meta.setImageDocument(false);
55 meta.setCover(getCover(reset(in)));
56
57 return meta;
58 }
59
60 @Override
61 public Map<String, String> getCookies() {
62 Map<String, String> cookies = new HashMap<String, String>();
63 cookies.put("view_mature", "true");
64 return cookies;
65 }
66
67 private List<String> getTags(InputStream in) {
68 List<String> tags = new ArrayList<String>();
69 tags.add("MLP");
70
71 @SuppressWarnings("resource")
72 Scanner scan = new Scanner(in, "UTF-8");
73 scan.useDelimiter("\\n");
74 boolean started = false;
75 while (scan.hasNext()) {
76 String line = scan.next();
77
78 if (!started) {
79 started = line.contains("\"story_container\"");
80 }
81
82 if (started && line.contains("class=\"tag-")) {
83 if (line.contains("index.php")) {
84 break; // end of *this story* tags
85 }
86
87 String keyword = "title=\"";
88 Scanner tagScanner = new Scanner(line);
89 tagScanner.useDelimiter(keyword);
90 if (tagScanner.hasNext()) {
91 tagScanner.next();// Ignore first one
92 }
93 while (tagScanner.hasNext()) {
94 String tag = tagScanner.next();
95 if (tag.contains("\"")) {
96 tag = tag.split("\"")[0];
97 tag = StringUtils.unhtml(tag).trim();
98 if (!tag.isEmpty() && !tags.contains(tag)) {
99 tags.add(tag);
100 }
101 }
102 }
103 tagScanner.close();
104 }
105 }
106
107 return tags;
108 }
109
110 private String getTitle(InputStream in) {
111 String line = getLine(in, " property=\"og:title\"", 0);
112 if (line != null) {
113 int pos = -1;
114 for (int i = 0; i < 3; i++) {
115 pos = line.indexOf('"', pos + 1);
116 }
117
118 if (pos >= 0) {
119 line = line.substring(pos + 1);
120 pos = line.indexOf('"');
121 if (pos >= 0) {
122 return StringUtils.unhtml(line.substring(0, pos)).trim();
123 }
124 }
125 }
126
127 return null;
128 }
129
130 private String getAuthor(InputStream in) {
131 String line = getLine(in, " href=\"/user/", 0);
132 if (line != null) {
133 int pos = line.indexOf('"');
134 if (pos >= 0) {
135 line = line.substring(pos + 1);
136 pos = line.indexOf('"');
137 if (pos >= 0) {
138 line = line.substring(0, pos);
139 pos = line.lastIndexOf('/');
140 if (pos >= 0) {
141 line = line.substring(pos + 1);
142 return line.replace('+', ' ');
143 }
144 }
145 }
146 }
147
148 return null;
149 }
150
151 private String getDate(InputStream in) {
152 String line = getLine(in, "<span class=\"date\">", 0);
153 if (line != null) {
154 int pos = -1;
155 for (int i = 0; i < 3; i++) {
156 pos = line.indexOf('>', pos + 1);
157 }
158
159 if (pos >= 0) {
160 line = line.substring(pos + 1);
161 pos = line.indexOf('<');
162 if (pos >= 0) {
163 return line.substring(0, pos).trim();
164 }
165 }
166 }
167
168 return null;
169 }
170
171 @Override
172 protected String getDesc(URL source, InputStream in) {
173 // the og: meta version is the SHORT resume, this is the LONG resume
174 return getLine(in, "class=\"description-text bbcode\"", 1);
175 }
176
177 private Image getCover(InputStream in) {
178 // Note: the 'og:image' is the SMALL cover, not the full version
179 String cover = getLine(in, "class=\"story_container__story_image\"", 1);
180 if (cover != null) {
181 int pos = cover.indexOf('"');
182 if (pos >= 0) {
183 cover = cover.substring(pos + 1);
184 pos = cover.indexOf('"');
185 if (pos >= 0) {
186 cover = cover.substring(0, pos);
187 }
188 }
189 }
190
191 return getImage(this, null, cover);
192 }
193
194 @Override
195 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
196 Progress pg) {
197 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
198 @SuppressWarnings("resource")
199 Scanner scan = new Scanner(in, "UTF-8");
200 scan.useDelimiter("\\n");
201 boolean started = false;
202 while (scan.hasNext()) {
203 String line = scan.next().trim();
204
205 if (!started) {
206 started = line.equals("<!--Chapters-->");
207 } else {
208 if (line.equals("</form>")) {
209 break;
210 }
211
212 if (line.startsWith("<a href=")
213 || line.contains("class=\"chapter-title\"")) {
214 // Chapter name
215 String name = line;
216 int pos = name.indexOf('>');
217 if (pos >= 0) {
218 name = name.substring(pos + 1);
219 pos = name.indexOf('<');
220 if (pos >= 0) {
221 name = name.substring(0, pos);
222 }
223 }
224 // Chapter content
225 pos = line.indexOf('/');
226 if (pos >= 0) {
227 line = line.substring(pos); // we take the /, not +1
228 pos = line.indexOf('"');
229 if (pos >= 0) {
230 line = line.substring(0, pos);
231 }
232 }
233
234 try {
235 urls.add(new AbstractMap.SimpleEntry<String, URL>(name,
236 new URL("http://www.fimfiction.net" + line)));
237 } catch (MalformedURLException e) {
238 Instance.getTraceHandler().error(e);
239 }
240 }
241 }
242 }
243
244 return urls;
245 }
246
247 @Override
248 protected String getChapterContent(URL source, InputStream in, int number,
249 Progress pg) {
250 return getLine(in, "<div class=\"bbcode\">", 1);
251 }
252
253 @Override
254 protected boolean supports(URL url) {
255 return "fimfiction.net".equals(url.getHost())
256 || "www.fimfiction.net".equals(url.getHost());
257 }
258}