update from master
[nikiroo-utils.git] / supported / FimfictionApi.java
CommitLineData
315f14ae
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.URL;
ce297a79 6import java.util.AbstractMap;
315f14ae
NR
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.Map.Entry;
ce297a79 12import java.util.TreeMap;
315f14ae 13
826e4569
NR
14import org.jsoup.nodes.Document;
15
315f14ae
NR
16import be.nikiroo.fanfix.Instance;
17import be.nikiroo.fanfix.bundles.Config;
18import be.nikiroo.fanfix.data.MetaData;
826e4569 19import be.nikiroo.fanfix.data.Story;
315f14ae 20import be.nikiroo.utils.IOUtils;
826e4569 21import be.nikiroo.utils.Image;
315f14ae
NR
22import be.nikiroo.utils.Progress;
23
24/**
25 * Support class for <a href="http://www.fimfiction.net/">FimFiction.net</a>
26 * stories, a website dedicated to My Little Pony.
27 * <p>
28 * This version uses the new, official API of FimFiction.
29 *
30 * @author niki
31 */
826e4569 32class FimfictionApi extends BasicSupport {
315f14ae 33 private String oauth;
315f14ae
NR
34 private String json;
35
36 private Map<Integer, String> chapterNames;
37 private Map<Integer, String> chapterContents;
38
39 public FimfictionApi() throws IOException {
d66deb8d
NR
40 if (Instance.getInstance().getConfig().getBoolean(Config.LOGIN_FIMFICTION_APIKEY_FORCE_HTML, false)) {
41 throw new IOException("Configuration is set to force HTML scrapping");
315f14ae
NR
42 }
43
d66deb8d 44 String oauth = Instance.getInstance().getConfig().getString(Config.LOGIN_FIMFICTION_APIKEY_TOKEN);
315f14ae
NR
45
46 if (oauth == null || oauth.isEmpty()) {
d66deb8d 47 String clientId = Instance.getInstance().getConfig().getString(Config.LOGIN_FIMFICTION_APIKEY_CLIENT_ID)
315f14ae 48 + "";
d66deb8d
NR
49 String clientSecret = Instance.getInstance().getConfig()
50 .getString(Config.LOGIN_FIMFICTION_APIKEY_CLIENT_SECRET) + "";
315f14ae
NR
51
52 if (clientId.trim().isEmpty() || clientSecret.trim().isEmpty()) {
53 throw new IOException("API key required for the beta API v2");
54 }
55
56 oauth = generateOAuth(clientId, clientSecret);
57
d66deb8d
NR
58 Instance.getInstance().getConfig().setString(Config.LOGIN_FIMFICTION_APIKEY_TOKEN, oauth);
59 Instance.getInstance().getConfig().updateFile();
315f14ae
NR
60 }
61
62 this.oauth = oauth;
63 }
64
826e4569
NR
65 @Override
66 protected Document loadDocument(URL source) throws IOException {
67 json = getJsonData();
68 return null;
69 }
70
315f14ae
NR
71 @Override
72 public String getOAuth() {
73 return oauth;
74 }
75
76 @Override
77 protected boolean isHtml() {
78 return true;
79 }
80
826e4569
NR
81 /**
82 * Extract the full JSON data we will later use to build the {@link Story}.
83 *
84 * @return the data in a JSON format
85 *
86 * @throws IOException
87 * in case of I/O error
88 */
89 private String getJsonData() throws IOException {
315f14ae
NR
90 // extract the ID from:
91 // https://www.fimfiction.net/story/123456/name-of-story
826e4569
NR
92 String storyId = getKeyText(getSource().toString(), "/story/", null,
93 "/");
315f14ae
NR
94
95 // Selectors, so to download all I need and only what I need
96 String storyContent = "fields[story]=title,description,date_published,cover_image";
97 String authorContent = "fields[author]=name";
77e28d38 98 String chapterContent = "fields[chapter]=chapter_number,title,content_html,authors_note_html";
315f14ae
NR
99 String includes = "author,chapters,tags";
100
101 String urlString = String.format(
102 "https://www.fimfiction.net/api/v2/stories/%s?" //
315f14ae
NR
103 + "%s&%s&%s&" //
104 + "include=%s", //
105 storyId, //
77e28d38 106 storyContent, authorContent, chapterContent,//
315f14ae
NR
107 includes);
108
109 // URL params must be URL-encoded: "[ ]" <-> "%5B %5D"
110 urlString = urlString.replace("[", "%5B").replace("]", "%5D");
111
112 URL url = new URL(urlString);
d66deb8d 113 InputStream jsonIn = Instance.getInstance().getCache().open(url, this, false);
315f14ae 114 try {
826e4569 115 return IOUtils.readSmallStream(jsonIn);
315f14ae
NR
116 } finally {
117 jsonIn.close();
118 }
119 }
120
121 @Override
826e4569 122 protected MetaData getMeta() throws IOException {
315f14ae
NR
123 MetaData meta = new MetaData();
124
125 meta.setTitle(getKeyJson(json, 0, "type", "story", "title"));
126 meta.setAuthor(getKeyJson(json, 0, "type", "user", "name"));
bff19b54
NR
127 meta.setDate(bsHelper.formatDate(
128 getKeyJson(json, 0, "type", "story", "date_published")));
315f14ae 129 meta.setTags(getTags());
727108fe 130 meta.setSource(getType().getSourceName());
826e4569 131 meta.setUrl(getSource().toString());
727108fe 132 meta.setPublisher(getType().getSourceName());
826e4569 133 meta.setUuid(getSource().toString());
315f14ae 134 meta.setLuid("");
276f95c6 135 meta.setLang("en");
315f14ae
NR
136 meta.setSubject("MLP");
137 meta.setType(getType().toString());
138 meta.setImageDocument(false);
ce297a79
NR
139
140 String coverImageLink = getKeyJson(json, 0, "type", "story",
141 "cover_image", "full");
fce43164 142 if (!coverImageLink.trim().isEmpty()) {
a5b42441
NR
143 URL coverImageUrl = new URL(coverImageLink.trim());
144
9a098d45
NR
145 // No need to use the oauth, cookies... for the cover
146 // Plus: it crashes on Android because of the referer
826e4569 147 try {
d66deb8d 148 InputStream in = Instance.getInstance().getCache().open(coverImageUrl, null, true);
9a098d45 149 try {
002972e9
NR
150 Image img = new Image(in);
151 if (img.getSize() == 0) {
152 img.close();
153 throw new IOException(
154 "Empty image not accepted");
155 }
156 meta.setCover(img);
9a098d45
NR
157 } finally {
158 in.close();
159 }
160 } catch (IOException e) {
d66deb8d
NR
161 Instance.getInstance().getTraceHandler()
162 .error(new IOException("Cannot get the story cover, ignoring...", e));
826e4569 163 }
fce43164 164 }
315f14ae
NR
165
166 return meta;
167 }
168
169 private List<String> getTags() {
170 List<String> tags = new ArrayList<String>();
171 tags.add("MLP");
172
173 int pos = 0;
174 while (pos >= 0) {
175 pos = indexOfJsonAfter(json, pos, "type", "story_tag");
176 if (pos >= 0) {
fce43164 177 tags.add(getKeyJson(json, pos, "name").trim());
315f14ae
NR
178 }
179 }
180
181 return tags;
182 }
183
184 @Override
826e4569 185 protected String getDesc() {
d9a94285 186 String desc = getKeyJson(json, 0, "type", "story", "description");
a8209dd0 187 return unbbcode(desc);
315f14ae
NR
188 }
189
190 @Override
826e4569 191 protected List<Entry<String, URL>> getChapters(Progress pg) {
ce297a79
NR
192 chapterNames = new TreeMap<Integer, String>();
193 chapterContents = new TreeMap<Integer, String>();
315f14ae
NR
194
195 int pos = 0;
196 while (pos >= 0) {
197 pos = indexOfJsonAfter(json, pos, "type", "chapter");
198 if (pos >= 0) {
199 int posNumber = indexOfJsonAfter(json, pos, "chapter_number");
200 int posComa = json.indexOf(",", posNumber);
201 final int number = Integer.parseInt(json.substring(posNumber,
202 posComa).trim());
203 final String title = getKeyJson(json, pos, "title");
77e28d38
NR
204 String notes = getKeyJson(json, pos, "authors_note_html");
205 String content = getKeyJson(json, pos, "content_html");
ce297a79 206
fce43164
NR
207 if (!notes.trim().isEmpty()) {
208 notes = "<br/>* * *<br/>" + notes;
209 }
ce297a79 210
315f14ae 211 chapterNames.put(number, title);
ce297a79 212 chapterContents.put(number, content + notes);
315f14ae
NR
213 }
214 }
215
ce297a79
NR
216 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
217 for (String title : chapterNames.values()) {
1c0d0058 218 urls.add(new AbstractMap.SimpleEntry<String, URL>(title, null));
ce297a79
NR
219 }
220
315f14ae
NR
221 return urls;
222 }
223
224 @Override
826e4569 225 protected String getChapterContent(URL source, int number, Progress pg) {
315f14ae
NR
226 return chapterContents.get(number);
227 }
228
229 @Override
230 protected boolean supports(URL url) {
231 return "fimfiction.net".equals(url.getHost())
232 || "www.fimfiction.net".equals(url.getHost());
233 }
234
a8209dd0
NR
235 /**
236 * Generate a new token from the client ID and secret.
237 * <p>
238 * Note that those tokens are long-lived, and it would be badly seen to
239 * create a lot of them without due cause.
240 * <p>
241 * So, please cache and re-use them.
242 *
243 * @param clientId
244 * the client ID offered on FimFiction
245 * @param clientSecret
246 * the client secret that goes with it
247 *
248 * @return a new generated token linked to that client ID
249 *
250 * @throws IOException
251 * in case of I/O errors
252 */
315f14ae
NR
253 static private String generateOAuth(String clientId, String clientSecret)
254 throws IOException {
255 URL url = new URL("https://www.fimfiction.net/api/v2/token");
256 Map<String, String> params = new HashMap<String, String>();
257 params.put("client_id", clientId);
258 params.put("client_secret", clientSecret);
259 params.put("grant_type", "client_credentials");
d66deb8d 260 InputStream in = Instance.getInstance().getCache().openNoCache(url, null, params, null, null);
315f14ae
NR
261
262 String jsonToken = IOUtils.readSmallStream(in);
581d42c0 263 in.close();
315f14ae
NR
264
265 // Extract token type and token from: {
a8209dd0 266 // token_type = "Bearer",
315f14ae
NR
267 // access_token = "xxxxxxxxxxxxxx"
268 // }
269
315f14ae 270 String tokenType = getKeyText(jsonToken, "\"token_type\"", "\"", "\"");
826e4569 271 String token = getKeyText(jsonToken, "\"access_token\"", "\"", "\"");
315f14ae 272
315f14ae
NR
273 return tokenType + " " + token;
274 }
275
276 // afters: [name, value] pairs (or "" for any of them), can end without
277 // value
278 static private int indexOfJsonAfter(String json, int startAt,
279 String... afterKeys) {
280 ArrayList<String> afters = new ArrayList<String>();
281 boolean name = true;
282 for (String key : afterKeys) {
283 if (key != null && !key.isEmpty()) {
284 afters.add("\"" + key + "\"");
285 } else {
286 afters.add("\"");
287 afters.add("\"");
288 }
289
290 if (name) {
291 afters.add(":");
292 }
293
294 name = !name;
295 }
296
297 return indexOfAfter(json, startAt, afters.toArray(new String[] {}));
298 }
299
300 // afters: [name, value] pairs (or "" for any of them), can end without
fce43164 301 // value but will then be empty, not NULL
315f14ae
NR
302 static private String getKeyJson(String json, int startAt,
303 String... afterKeys) {
304 int pos = indexOfJsonAfter(json, startAt, afterKeys);
305 if (pos < 0) {
fce43164 306 return "";
315f14ae
NR
307 }
308
fce43164 309 String result = "";
37fdbdef
NR
310 String wip = json.substring(pos);
311
312 pos = nextUnescapedQuote(wip, 0);
313 if (pos >= 0) {
314 wip = wip.substring(pos + 1);
315 pos = nextUnescapedQuote(wip, 0);
316 if (pos >= 0) {
317 result = wip.substring(0, pos);
318 }
319 }
ce297a79
NR
320
321 result = result.replace("\\t", "\t").replace("\\\"", "\"");
322
37fdbdef
NR
323 return result;
324 }
325
326 // next " but don't take \" into account
327 static private int nextUnescapedQuote(String result, int pos) {
328 while (pos >= 0) {
329 pos = result.indexOf("\"", pos);
330 if (pos == 0 || (pos > 0 && result.charAt(pos - 1) != '\\')) {
331 break;
332 }
333
334 if (pos < result.length()) {
335 pos++;
336 }
337 }
338
339 return pos;
315f14ae 340 }
a8209dd0
NR
341
342 // quick & dirty filter
343 static private String unbbcode(String bbcode) {
344 String text = bbcode.replace("\\r\\n", "<br/>") //
345 .replace("[i]", "_").replace("[/i]", "_") //
346 .replace("[b]", "*").replace("[/b]", "*") //
347 .replaceAll("\\[[^\\]]*\\]", "");
348 return text;
349 }
826e4569
NR
350
351 /**
352 * Return the text between the key and the endKey (and optional subKey can
353 * be passed, in this case we will look for the key first, then take the
354 * text between the subKey and the endKey).
355 *
356 * @param in
357 * the input
358 * @param key
359 * the key to match (also supports "^" at start to say
360 * "only if it starts with" the key)
361 * @param subKey
362 * the sub key or NULL if none
363 * @param endKey
364 * the end key or NULL for "up to the end"
365 * @return the text or NULL if not found
366 */
367 static private String getKeyText(String in, String key, String subKey,
368 String endKey) {
369 String result = null;
370
371 String line = in;
372 if (line != null && line.contains(key)) {
373 line = line.substring(line.indexOf(key) + key.length());
374 if (subKey == null || subKey.isEmpty() || line.contains(subKey)) {
375 if (subKey != null) {
376 line = line.substring(line.indexOf(subKey)
377 + subKey.length());
378 }
379 if (endKey == null || line.contains(endKey)) {
380 if (endKey != null) {
381 line = line.substring(0, line.indexOf(endKey));
382 result = line;
383 }
384 }
385 }
386 }
387
388 return result;
389 }
390
391 /**
392 * Return the first index after all the given "afters" have been found in
393 * the {@link String}, or -1 if it was not possible.
394 *
395 * @param in
396 * the input
397 * @param startAt
398 * start at this position in the string
399 * @param afters
400 * the sub-keys to find before checking for key/endKey
401 *
402 * @return the text or NULL if not found
403 */
404 static private int indexOfAfter(String in, int startAt, String... afters) {
405 int pos = -1;
406 if (in != null && !in.isEmpty()) {
407 pos = startAt;
408 if (afters != null) {
409 for (int i = 0; pos >= 0 && i < afters.length; i++) {
410 String subKey = afters[i];
411 if (!subKey.isEmpty()) {
412 pos = in.indexOf(subKey, pos);
413 if (pos >= 0) {
414 pos += subKey.length();
415 }
416 }
417 }
418 }
419 }
420
421 return pos;
422 }
315f14ae 423}