This commit is contained in:
Reinhard Pointner 2016-03-08 09:54:29 +00:00
parent 1ae3f5d2b8
commit 7e5388e7fa
1 changed files with 29 additions and 32 deletions

View File

@ -1,23 +1,23 @@
package net.filebot.web; package net.filebot.web;
import static java.nio.charset.StandardCharsets.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static net.filebot.util.JsonUtilities.*; import static net.filebot.util.JsonUtilities.*;
import java.io.Serializable; import java.io.Serializable;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import net.filebot.web.FanartTVClient.FanartDescriptor.FanartProperty; import javax.swing.Icon;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
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; private String apikey;
@ -25,6 +25,16 @@ public class FanartTVClient {
this.apikey = apikey; this.apikey = apikey;
} }
@Override
public String getName() {
return "FanartTV";
}
@Override
public Icon getIcon() {
return null;
}
public List<FanartDescriptor> getSeriesArtwork(int tvdbid) throws Exception { public List<FanartDescriptor> getSeriesArtwork(int tvdbid) throws Exception {
return getArtwork("tv", String.valueOf(tvdbid)); return getArtwork("tv", String.valueOf(tvdbid));
} }
@ -34,37 +44,24 @@ public class FanartTVClient {
} }
public List<FanartDescriptor> getArtwork(String category, String id) throws Exception { public List<FanartDescriptor> getArtwork(String category, String id) throws Exception {
String resource = getResource(category, id); String path = category + '/' + id;
// cache results Cache cache = Cache.getCache(getName(), CacheType.Weekly);
CachedResource<FanartDescriptor[]> data = new CachedResource<FanartDescriptor[]>(resource, FanartDescriptor[].class, CachedResource.ONE_WEEK) { Object json = cache.json(path, s -> getResource(s)).expire(Cache.ONE_WEEK);
@Override return asMap(json).entrySet().stream().flatMap(it -> {
public FanartDescriptor[] process(ByteBuffer data) throws Exception { return streamJsonObjects(it.getValue()).map(item -> {
Object json = readJson(UTF_8.decode(data)); Map<FanartProperty, String> map = mapStringValues(item, FanartProperty.class);
map.put(FanartProperty.type, it.getKey().toString());
return asMap(json).entrySet().stream().flatMap(it -> { return new FanartDescriptor(map);
return streamJsonObjects(it.getValue()).map(item -> { }).filter(a -> a.getProperties().size() > 1);
Map<FanartProperty, String> map = mapStringValues(item, FanartProperty.class); }).collect(toList());
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());
} }
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 // 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 { public static class FanartDescriptor implements Serializable {