diff --git a/source/net/filebot/Cache.java b/source/net/filebot/Cache.java index 96657468..4276c0c1 100644 --- a/source/net/filebot/Cache.java +++ b/source/net/filebot/Cache.java @@ -21,7 +21,7 @@ public class Cache { public static final Duration ONE_WEEK = Duration.ofDays(7); public static Cache getCache(String name, CacheType type) { - return CacheManager.getInstance().getCache(name.toLowerCase(), type); + return CacheManager.getInstance().getCache(name, type); } public CachedResource2 text(String key, Transform resource) { diff --git a/source/net/filebot/CacheManager.java b/source/net/filebot/CacheManager.java index 776643c8..4072a346 100644 --- a/source/net/filebot/CacheManager.java +++ b/source/net/filebot/CacheManager.java @@ -35,7 +35,7 @@ public class CacheManager { } public Cache getCache(String name, CacheType type) { - String cacheName = name + "_" + type.ordinal(); + String cacheName = name.toLowerCase() + "_" + type.ordinal(); if (!manager.cacheExists(cacheName)) { debug.config("Create cache: " + cacheName); manager.addCache(new net.sf.ehcache.Cache(type.getConfiguration(cacheName))); diff --git a/source/net/filebot/CachedResource2.java b/source/net/filebot/CachedResource2.java index 43661965..a46b819e 100644 --- a/source/net/filebot/CachedResource2.java +++ b/source/net/filebot/CachedResource2.java @@ -123,8 +123,8 @@ public class CachedResource2 { } @FunctionalInterface - public interface Permit

{ - boolean acquirePermit(URL resource) throws Exception; + public interface Permit { + void acquire(URL resource) throws Exception; } public static Transform getText(Charset charset) { @@ -194,12 +194,10 @@ public class CachedResource2 { }; } - public static Fetch withPermit(Fetch fetch, Permit permit) { + public static Fetch withPermit(Fetch fetch, Permit permit) { return (url, lastModified) -> { - if (permit.acquirePermit(url)) { - return fetch.fetch(url, lastModified); - } - return null; + permit.acquire(url); + return fetch.fetch(url, lastModified); }; } diff --git a/source/net/filebot/web/OMDbClient.java b/source/net/filebot/web/OMDbClient.java index 1fa0d925..9f0f56cd 100644 --- a/source/net/filebot/web/OMDbClient.java +++ b/source/net/filebot/web/OMDbClient.java @@ -80,7 +80,7 @@ public class OMDbClient implements MovieIdentificationService { param.put("y", movieYear); } - Map response = request(param); + Object response = request(param); List result = new ArrayList(); for (Object it : getArray(response, "Search")) { @@ -134,13 +134,11 @@ public class OMDbClient implements MovieIdentificationService { throw new UnsupportedOperationException(); } - public Map request(Map parameters) throws Exception { + public Object request(Map parameters) throws Exception { + Cache cache = Cache.getCache(getName(), CacheType.Weekly); String key = '?' + encodeParameters(parameters, true); - Cache cache = Cache.getCache(getName(), CacheType.Weekly); - Object json = cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfModified(), r -> REQUEST_LIMIT.acquirePermit() != null)).expire(Cache.ONE_WEEK).get(); - - return asMap(json); + return cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfModified(), r -> REQUEST_LIMIT.acquirePermit())).expire(Cache.ONE_WEEK).get(); } public URL getResource(String file) throws Exception { diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index 210fd333..556d64cd 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -87,7 +87,7 @@ public class TMDbClient implements MovieIdentificationService { param.put("year", movieYear); } - Map response = request("search/movie", param, locale, SEARCH_LIMIT); + Object response = request("search/movie", param, locale, SEARCH_LIMIT); // e.g. {"id":16320,"title":"冲出宁静号","release_date":"2005-09-30","original_title":"Serenity"} return streamJsonObjects(response, "results").map(it -> { @@ -110,7 +110,7 @@ public class TMDbClient implements MovieIdentificationService { if (extendedInfo) { try { - Map titles = request("movie/" + id + "/alternative_titles", null, null, REQUEST_LIMIT); + Object titles = request("movie/" + id + "/alternative_titles", null, null, REQUEST_LIMIT); streamJsonObjects(titles, "titles").map(n -> { return getString(n, "title"); }).filter(t -> t != null && t.length() >= 3).forEach(alternativeTitles::add); @@ -169,7 +169,7 @@ public class TMDbClient implements MovieIdentificationService { } public MovieInfo getMovieInfo(String id, Locale locale, boolean extendedInfo) throws Exception { - Map response = request("movie/" + id, extendedInfo ? singletonMap("append_to_response", "alternative_titles,releases,casts,trailers") : null, locale, REQUEST_LIMIT); + Object response = request("movie/" + id, extendedInfo ? singletonMap("append_to_response", "alternative_titles,releases,casts,trailers") : null, locale, REQUEST_LIMIT); Map fields = mapStringValues(response, MovieProperty.class); @@ -270,10 +270,10 @@ public class TMDbClient implements MovieIdentificationService { public List getArtwork(String id) throws Exception { // http://api.themoviedb.org/3/movie/11/images - Map config = request("configuration", null, null, REQUEST_LIMIT); + Object config = request("configuration", null, null, REQUEST_LIMIT); String baseUrl = getString(getMap(config, "images"), "base_url"); - Map images = request("movie/" + id + "/images", null, null, REQUEST_LIMIT); + Object images = request("movie/" + id + "/images", null, null, REQUEST_LIMIT); return Stream.of("backdrops", "posters").flatMap(section -> { Stream artwork = streamJsonObjects(images, section).map(it -> { @@ -292,7 +292,7 @@ public class TMDbClient implements MovieIdentificationService { }).collect(toList()); } - public Map request(String resource, Map parameters, Locale locale, final FloodLimit limit) throws Exception { + public Object request(String resource, Map parameters, Locale locale, final FloodLimit limit) throws Exception { // default parameters LinkedHashMap data = new LinkedHashMap(); if (parameters != null) { @@ -313,17 +313,16 @@ public class TMDbClient implements MovieIdentificationService { } data.put("api_key", apikey); + Cache cache = Cache.getCache(getName(), CacheType.Monthly); + Cache etagStorage = Cache.getCache("etag", CacheType.Monthly); String key = resource + '?' + encodeParameters(data, true); - Cache etagStorage = Cache.getCache("etag", CacheType.Monthly); - Cache cache = Cache.getCache(getName(), CacheType.Monthly); - Object json = cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfNoneMatch(etagStorage), r -> limit.acquirePermit() != null)).expire(Cache.ONE_WEEK).get(); + Object json = cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfNoneMatch(etagStorage), r -> limit.acquirePermit())).expire(Cache.ONE_WEEK).get(); - Map root = asMap(json); - if (root.isEmpty()) { + if (asMap(json).isEmpty()) { throw new FileNotFoundException(String.format("Resource is empty: %s => %s", json, getResource(key))); } - return root; + return json; } public URL getResource(String file) throws Exception {