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