Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
68686a37 | 3 | import java.io.IOException; |
08fe2e33 NR |
4 | import java.io.InputStream; |
5 | import java.net.MalformedURLException; | |
6 | import java.net.URL; | |
ce297a79 | 7 | import java.util.AbstractMap; |
08fe2e33 NR |
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; | |
68686a37 | 16 | import be.nikiroo.fanfix.data.MetaData; |
16a81ef7 | 17 | import be.nikiroo.utils.Image; |
ed08c171 | 18 | import be.nikiroo.utils.Progress; |
68686a37 | 19 | import be.nikiroo.utils.StringUtils; |
08fe2e33 NR |
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 | */ | |
0ffa4754 | 27 | class Fimfiction extends BasicSupport_Deprecated { |
08fe2e33 NR |
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 | |
68686a37 NR |
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()); | |
2206ef66 | 47 | meta.setUrl(source.toString()); |
68686a37 NR |
48 | meta.setPublisher(getSourceName()); |
49 | meta.setUuid(source.toString()); | |
50 | meta.setLuid(""); | |
276f95c6 | 51 | meta.setLang("en"); |
68686a37 NR |
52 | meta.setSubject("MLP"); |
53 | meta.setType(getType().toString()); | |
54 | meta.setImageDocument(false); | |
55 | meta.setCover(getCover(reset(in))); | |
56 | ||
57 | return meta; | |
08fe2e33 NR |
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 | ||
68686a37 | 67 | private List<String> getTags(InputStream in) { |
08fe2e33 NR |
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"); | |
c3c79003 | 74 | boolean started = false; |
08fe2e33 NR |
75 | while (scan.hasNext()) { |
76 | String line = scan.next(); | |
c3c79003 NR |
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 | |
08fe2e33 NR |
85 | } |
86 | ||
e1168b3c NR |
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 | } | |
c3c79003 | 101 | } |
08fe2e33 | 102 | } |
e1168b3c | 103 | tagScanner.close(); |
08fe2e33 NR |
104 | } |
105 | } | |
106 | ||
107 | return tags; | |
108 | } | |
109 | ||
68686a37 | 110 | private String getTitle(InputStream in) { |
08fe2e33 NR |
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) { | |
68686a37 | 122 | return StringUtils.unhtml(line.substring(0, pos)).trim(); |
08fe2e33 NR |
123 | } |
124 | } | |
125 | } | |
126 | ||
127 | return null; | |
128 | } | |
129 | ||
68686a37 | 130 | private String getAuthor(InputStream in) { |
08fe2e33 NR |
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 | ||
68686a37 | 151 | private String getDate(InputStream in) { |
08fe2e33 NR |
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 | |
83f66cbb | 174 | return getLine(in, "class=\"description-text bbcode\"", 1); |
08fe2e33 NR |
175 | } |
176 | ||
16a81ef7 | 177 | private Image getCover(InputStream in) { |
08fe2e33 | 178 | // Note: the 'og:image' is the SMALL cover, not the full version |
83f66cbb | 179 | String cover = getLine(in, "class=\"story_container__story_image\"", 1); |
08fe2e33 NR |
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 | ||
333f0e7b | 191 | return getImage(this, null, cover); |
08fe2e33 NR |
192 | } |
193 | ||
194 | @Override | |
ed08c171 NR |
195 | protected List<Entry<String, URL>> getChapters(URL source, InputStream in, |
196 | Progress pg) { | |
08fe2e33 NR |
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"); | |
83f66cbb | 201 | boolean started = false; |
08fe2e33 | 202 | while (scan.hasNext()) { |
83f66cbb NR |
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 | ||
e1168b3c NR |
212 | if (line.startsWith("<a href=") |
213 | || line.contains("class=\"chapter-title\"")) { | |
83f66cbb NR |
214 | // Chapter name |
215 | String name = line; | |
216 | int pos = name.indexOf('>'); | |
08fe2e33 | 217 | if (pos >= 0) { |
83f66cbb NR |
218 | name = name.substring(pos + 1); |
219 | pos = name.indexOf('<'); | |
220 | if (pos >= 0) { | |
221 | name = name.substring(0, pos); | |
222 | } | |
08fe2e33 | 223 | } |
83f66cbb NR |
224 | // Chapter content |
225 | pos = line.indexOf('/'); | |
08fe2e33 | 226 | if (pos >= 0) { |
83f66cbb NR |
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 | } | |
08fe2e33 | 232 | } |
08fe2e33 | 233 | |
83f66cbb | 234 | try { |
ce297a79 NR |
235 | urls.add(new AbstractMap.SimpleEntry<String, URL>(name, |
236 | new URL("http://www.fimfiction.net" + line))); | |
83f66cbb | 237 | } catch (MalformedURLException e) { |
62c63b07 | 238 | Instance.getTraceHandler().error(e); |
83f66cbb | 239 | } |
08fe2e33 NR |
240 | } |
241 | } | |
242 | } | |
243 | ||
244 | return urls; | |
245 | } | |
246 | ||
247 | @Override | |
ed08c171 NR |
248 | protected String getChapterContent(URL source, InputStream in, int number, |
249 | Progress pg) { | |
83f66cbb | 250 | return getLine(in, "<div class=\"bbcode\">", 1); |
08fe2e33 NR |
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 | } |