X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FFimfictionApi.java;h=6ef8a2cc70d8bb65e7c32b4b94fce81d96f07bea;hb=23b6932d10d2cc2d58456b0489219109f017b762;hp=591bbb08736c921333b885c19548c51e7d5c78c2;hpb=315f14ae3752d90c683a07fa20f1aa53f6010d6d;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/supported/FimfictionApi.java b/src/be/nikiroo/fanfix/supported/FimfictionApi.java index 591bbb0..6ef8a2c 100644 --- a/src/be/nikiroo/fanfix/supported/FimfictionApi.java +++ b/src/be/nikiroo/fanfix/supported/FimfictionApi.java @@ -23,7 +23,7 @@ import be.nikiroo.utils.Progress; * * @author niki */ -class FimfictionApi extends BasicSupport { +class FimfictionApi extends BasicSupport_Deprecated { private String oauth; private String storyId; private String json; @@ -87,19 +87,15 @@ class FimfictionApi extends BasicSupport { // Selectors, so to download all I need and only what I need String storyContent = "fields[story]=title,description,date_published,cover_image"; String authorContent = "fields[author]=name"; - String chapterContent = "fields[chapter]=chapter_number,title,content,authors_note"; - String contentContent = "fields[content]=html"; - String authorsNoteContent = "fields[authors_note]=html"; + String chapterContent = "fields[chapter]=chapter_number,title,content_html,authors_note_html"; String includes = "author,chapters,tags"; String urlString = String.format( "https://www.fimfiction.net/api/v2/stories/%s?" // - + "%s&%s&"// + "%s&%s&%s&" // + "include=%s", // storyId, // - storyContent, authorContent, // - chapterContent, contentContent, authorsNoteContent,// + storyContent, authorContent, chapterContent,// includes); // URL params must be URL-encoded: "[ ]" <-> "%5B %5D" @@ -132,12 +128,16 @@ class FimfictionApi extends BasicSupport { meta.setPublisher(getSourceName()); meta.setUuid(source.toString()); meta.setLuid(""); - meta.setLang("EN"); + meta.setLang("en"); meta.setSubject("MLP"); meta.setType(getType().toString()); meta.setImageDocument(false); - meta.setCover(getImage(this, null, - getKeyJson(json, 0, "type", "story", "cover_image", "full"))); + + String coverImageLink = + getKeyJson(json, 0, "type", "story", "cover_image", "full"); + if (!coverImageLink.trim().isEmpty()) { + meta.setCover(getImage(this, null, coverImageLink.trim())); + } return meta; } @@ -150,7 +150,7 @@ class FimfictionApi extends BasicSupport { while (pos >= 0) { pos = indexOfJsonAfter(json, pos, "type", "story_tag"); if (pos >= 0) { - tags.add(getKeyJson(json, pos, "name")); + tags.add(getKeyJson(json, pos, "name").trim()); } } @@ -159,7 +159,8 @@ class FimfictionApi extends BasicSupport { @Override protected String getDesc(URL source, InputStream in) { - return getKeyJson(json, 0, "type", "story", "description"); + String desc = getKeyJson(json, 0, "type", "story", "description"); + return unbbcode(desc); } @Override @@ -179,12 +180,16 @@ class FimfictionApi extends BasicSupport { final int number = Integer.parseInt(json.substring(posNumber, posComa).trim()); final String title = getKeyJson(json, pos, "title"); - String notes = getKeyJson(json, pos, "authors_note", "html"); - String content = getKeyJson(json, pos, "content", "html"); - + String notes = getKeyJson(json, pos, "authors_note_html"); + String content = getKeyJson(json, pos, "content_html"); + + if (!notes.trim().isEmpty()) { + notes = "
* * *
" + notes; + } + chapterNames.put(number, title); chapterContents - .put(number, content + "
* * *
" + notes); + .put(number, content + notes); urls.add(new Entry() { @Override @@ -220,6 +225,24 @@ class FimfictionApi extends BasicSupport { || "www.fimfiction.net".equals(url.getHost()); } + /** + * Generate a new token from the client ID and secret. + *

+ * Note that those tokens are long-lived, and it would be badly seen to + * create a lot of them without due cause. + *

+ * So, please cache and re-use them. + * + * @param clientId + * the client ID offered on FimFiction + * @param clientSecret + * the client secret that goes with it + * + * @return a new generated token linked to that client ID + * + * @throws IOException + * in case of I/O errors + */ static private String generateOAuth(String clientId, String clientSecret) throws IOException { URL url = new URL("https://www.fimfiction.net/api/v2/token"); @@ -231,20 +254,16 @@ class FimfictionApi extends BasicSupport { null, null); String jsonToken = IOUtils.readSmallStream(in); + in.close(); // Extract token type and token from: { - // token_type = "bearer", + // token_type = "Bearer", // access_token = "xxxxxxxxxxxxxx" // } String token = getKeyText(jsonToken, "\"access_token\"", "\"", "\""); String tokenType = getKeyText(jsonToken, "\"token_type\"", "\"", "\""); - // TODO: remove this once the bug is fixed on the server side - if ("bearer".equals(tokenType)) { - tokenType = "Bearer"; - } - return tokenType + " " + token; } @@ -273,14 +292,54 @@ class FimfictionApi extends BasicSupport { } // afters: [name, value] pairs (or "" for any of them), can end without - // value + // value but will then be empty, not NULL static private String getKeyJson(String json, int startAt, String... afterKeys) { int pos = indexOfJsonAfter(json, startAt, afterKeys); if (pos < 0) { - return null; + return ""; + } + + String result = ""; + String wip = json.substring(pos); + + pos = nextUnescapedQuote(wip, 0); + if (pos >= 0) { + wip = wip.substring(pos + 1); + pos = nextUnescapedQuote(wip, 0); + if (pos >= 0) { + result = wip.substring(0, pos); + } } + + result = result.replace("\\t", "\t") + .replace("\\\"", "\""); + + return result; + } + + // next " but don't take \" into account + static private int nextUnescapedQuote(String result, int pos) { + while (pos >= 0) { + pos = result.indexOf("\"", pos); + if (pos == 0 || (pos > 0 && result.charAt(pos - 1) != '\\')) { + break; + } + + if (pos < result.length()) { + pos++; + } + } + + return pos; + } - return getKeyText(json.substring(pos), "\"", null, "\""); + // quick & dirty filter + static private String unbbcode(String bbcode) { + String text = bbcode.replace("\\r\\n", "
") // + .replace("[i]", "_").replace("[/i]", "_") // + .replace("[b]", "*").replace("[/b]", "*") // + .replaceAll("\\[[^\\]]*\\]", ""); + return text; } }