From 7e5388e7fa0ca9d9bcf3fea2684090c624f09358 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 8 Mar 2016 09:54:29 +0000 Subject: [PATCH] Refactor --- source/net/filebot/web/FanartTVClient.java | 61 ++++++++++------------ 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/source/net/filebot/web/FanartTVClient.java b/source/net/filebot/web/FanartTVClient.java index d1d8a345..050ca8ec 100644 --- a/source/net/filebot/web/FanartTVClient.java +++ b/source/net/filebot/web/FanartTVClient.java @@ -1,23 +1,23 @@ package net.filebot.web; -import static java.nio.charset.StandardCharsets.*; import static java.util.Collections.*; +import static java.util.stream.Collectors.*; import static net.filebot.util.JsonUtilities.*; import java.io.Serializable; import java.net.URL; -import java.nio.ByteBuffer; -import java.util.Arrays; import java.util.EnumMap; import java.util.List; import java.util.Locale; import java.util.Map; -import net.filebot.web.FanartTVClient.FanartDescriptor.FanartProperty; -import net.sf.ehcache.Cache; -import net.sf.ehcache.CacheManager; +import javax.swing.Icon; -public class FanartTVClient { +import net.filebot.Cache; +import net.filebot.CacheType; +import net.filebot.web.FanartTVClient.FanartDescriptor.FanartProperty; + +public class FanartTVClient implements Datasource { private String apikey; @@ -25,6 +25,16 @@ public class FanartTVClient { this.apikey = apikey; } + @Override + public String getName() { + return "FanartTV"; + } + + @Override + public Icon getIcon() { + return null; + } + public List getSeriesArtwork(int tvdbid) throws Exception { return getArtwork("tv", String.valueOf(tvdbid)); } @@ -34,37 +44,24 @@ public class FanartTVClient { } public List getArtwork(String category, String id) throws Exception { - String resource = getResource(category, id); + String path = category + '/' + id; - // cache results - CachedResource data = new CachedResource(resource, FanartDescriptor[].class, CachedResource.ONE_WEEK) { + Cache cache = Cache.getCache(getName(), CacheType.Weekly); + Object json = cache.json(path, s -> getResource(s)).expire(Cache.ONE_WEEK); - @Override - public FanartDescriptor[] process(ByteBuffer data) throws Exception { - Object json = readJson(UTF_8.decode(data)); + return asMap(json).entrySet().stream().flatMap(it -> { + return streamJsonObjects(it.getValue()).map(item -> { + Map map = mapStringValues(item, FanartProperty.class); + map.put(FanartProperty.type, it.getKey().toString()); - return asMap(json).entrySet().stream().flatMap(it -> { - return streamJsonObjects(it.getValue()).map(item -> { - Map map = mapStringValues(item, FanartProperty.class); - map.put(FanartProperty.type, it.getKey().toString()); - - return new FanartDescriptor(map); - }).filter(art -> art.getProperties().size() > 1); - }).toArray(FanartDescriptor[]::new); - } - - @Override - protected Cache getCache() { - return CacheManager.getInstance().getCache("web-datasource-lv2"); - } - }; - - return Arrays.asList(data.get()); + return new FanartDescriptor(map); + }).filter(a -> a.getProperties().size() > 1); + }).collect(toList()); } - public String getResource(String category, String id) { + public URL getResource(String path) throws Exception { // e.g. http://webservice.fanart.tv/v3/movies/17645?api_key=6fa42b0ef3b5f3aab6a7edaa78675ac2 - return "http://webservice.fanart.tv/v3/" + category + "/" + id + "?api_key=" + apikey; + return new URL("http://webservice.fanart.tv/v3/" + path + "?api_key=" + apikey); } public static class FanartDescriptor implements Serializable {