X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=supported%2FFimfictionApi.java;h=cdb132147679b3a912e662a112cd988273382cc0;hp=c97ecf7d3e9ea2ff8a7cc782eea5bca660f9f265;hb=258e065f81071a861711ef935dca3ec5563f4360;hpb=0fc81e6465aa9c1f1dfc19b532082220d609768a diff --git a/supported/FimfictionApi.java b/supported/FimfictionApi.java index c97ecf7..cdb1321 100644 --- a/supported/FimfictionApi.java +++ b/supported/FimfictionApi.java @@ -37,22 +37,17 @@ class FimfictionApi extends BasicSupport { private Map chapterContents; public FimfictionApi() throws IOException { - if (Instance.getConfig().getBoolean( - Config.LOGIN_FIMFICTION_APIKEY_FORCE_HTML, false)) { - throw new IOException( - "Configuration is set to force HTML scrapping"); + if (Instance.getInstance().getConfig().getBoolean(Config.LOGIN_FIMFICTION_APIKEY_FORCE_HTML, false)) { + throw new IOException("Configuration is set to force HTML scrapping"); } - String oauth = Instance.getConfig().getString( - Config.LOGIN_FIMFICTION_APIKEY_TOKEN); + String oauth = Instance.getInstance().getConfig().getString(Config.LOGIN_FIMFICTION_APIKEY_TOKEN); if (oauth == null || oauth.isEmpty()) { - String clientId = Instance.getConfig().getString( - Config.LOGIN_FIMFICTION_APIKEY_CLIENT_ID) - + ""; - String clientSecret = Instance.getConfig().getString( - Config.LOGIN_FIMFICTION_APIKEY_CLIENT_SECRET) + String clientId = Instance.getInstance().getConfig().getString(Config.LOGIN_FIMFICTION_APIKEY_CLIENT_ID) + ""; + String clientSecret = Instance.getInstance().getConfig() + .getString(Config.LOGIN_FIMFICTION_APIKEY_CLIENT_SECRET) + ""; if (clientId.trim().isEmpty() || clientSecret.trim().isEmpty()) { throw new IOException("API key required for the beta API v2"); @@ -60,9 +55,8 @@ class FimfictionApi extends BasicSupport { oauth = generateOAuth(clientId, clientSecret); - Instance.getConfig().setString( - Config.LOGIN_FIMFICTION_APIKEY_TOKEN, oauth); - Instance.getConfig().updateFile(); + Instance.getInstance().getConfig().setString(Config.LOGIN_FIMFICTION_APIKEY_TOKEN, oauth); + Instance.getInstance().getConfig().updateFile(); } this.oauth = oauth; @@ -116,7 +110,7 @@ class FimfictionApi extends BasicSupport { urlString = urlString.replace("[", "%5B").replace("]", "%5D"); URL url = new URL(urlString); - InputStream jsonIn = Instance.getCache().open(url, this, false); + InputStream jsonIn = Instance.getInstance().getCache().open(url, this, false); try { return IOUtils.readSmallStream(jsonIn); } finally { @@ -130,16 +124,14 @@ class FimfictionApi extends BasicSupport { meta.setTitle(getKeyJson(json, 0, "type", "story", "title")); meta.setAuthor(getKeyJson(json, 0, "type", "user", "name")); - meta.setDate(getKeyJson(json, 0, "type", "story", "date_published")); + meta.setDate(bsHelper.formatDate( + getKeyJson(json, 0, "type", "story", "date_published"))); meta.setTags(getTags()); - meta.setSource(getType().getSourceName()); meta.setUrl(getSource().toString()); - meta.setPublisher(getType().getSourceName()); meta.setUuid(getSource().toString()); meta.setLuid(""); meta.setLang("en"); meta.setSubject("MLP"); - meta.setType(getType().toString()); meta.setImageDocument(false); String coverImageLink = getKeyJson(json, 0, "type", "story", @@ -150,17 +142,21 @@ class FimfictionApi extends BasicSupport { // No need to use the oauth, cookies... for the cover // Plus: it crashes on Android because of the referer try { - InputStream in = Instance.getCache().open(coverImageUrl, null, - true); + InputStream in = Instance.getInstance().getCache().open(coverImageUrl, null, true); try { - meta.setCover(new Image(in)); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + meta.setCover(img); } finally { in.close(); } } catch (IOException e) { - Instance.getTraceHandler().error( - new IOException( - "Cannot get the story cover, ignoring...", e)); + Instance.getInstance().getTraceHandler() + .error(new IOException("Cannot get the story cover, ignoring...", e)); } } @@ -258,8 +254,7 @@ class FimfictionApi extends BasicSupport { params.put("client_id", clientId); params.put("client_secret", clientSecret); params.put("grant_type", "client_credentials"); - InputStream in = Instance.getCache().openNoCache(url, null, params, - null, null); + InputStream in = Instance.getInstance().getCache().openNoCache(url, null, params, null, null); String jsonToken = IOUtils.readSmallStream(in); in.close();