+ * 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;
}
}