X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FFimfictionApi.java;h=b2d75df98b9c06437c21bf776843f32ad75b2675;hb=a8209dd0972f751e59153dd80a53f3062042897a;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..b2d75df 100644 --- a/src/be/nikiroo/fanfix/supported/FimfictionApi.java +++ b/src/be/nikiroo/fanfix/supported/FimfictionApi.java @@ -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 @@ -220,6 +221,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"); @@ -233,18 +252,13 @@ class FimfictionApi extends BasicSupport { String jsonToken = IOUtils.readSmallStream(in); // 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; } @@ -281,6 +295,43 @@ class FimfictionApi extends BasicSupport { return null; } - return getKeyText(json.substring(pos), "\"", null, "\""); + String result = null; + 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); + } + } + + 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; + } + + // 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; } }