1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 08:48:51 -05:00

* update for Fanart TV API v3

This commit is contained in:
Reinhard Pointner 2015-11-03 03:43:57 +00:00
parent de0c7a627e
commit 63988e8865

View File

@ -1,11 +1,6 @@
package net.filebot.web; package net.filebot.web;
import static net.filebot.util.XPathUtilities.*;
import static net.filebot.web.WebRequest.*;
import java.io.File;
import java.io.Serializable; import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -20,8 +15,8 @@ import net.filebot.web.FanartTVClient.FanartDescriptor.FanartProperty;
import net.sf.ehcache.Cache; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import org.w3c.dom.Document; import com.cedarsoftware.util.io.JsonObject;
import org.w3c.dom.Node; import com.cedarsoftware.util.io.JsonReader;
public class FanartTVClient { public class FanartTVClient {
@ -32,44 +27,48 @@ public class FanartTVClient {
} }
public List<FanartDescriptor> getSeriesArtwork(int tvdbid) throws Exception { public List<FanartDescriptor> getSeriesArtwork(int tvdbid) throws Exception {
return getSeriesArtwork(String.valueOf(tvdbid), "all", 1, 2); return getArtwork("tv", String.valueOf(tvdbid));
}
public List<FanartDescriptor> getSeriesArtwork(String id, String type, int sort, int limit) throws Exception {
return getArtwork("series", id, type, sort, limit);
} }
public List<FanartDescriptor> getMovieArtwork(int tmdbid) throws Exception { public List<FanartDescriptor> getMovieArtwork(int tmdbid) throws Exception {
return getMovieArtwork(String.valueOf(tmdbid), "all", 1, 2); return getArtwork("movies", String.valueOf(tmdbid));
} }
public List<FanartDescriptor> getMovieArtwork(String id, String type, int sort, int limit) throws Exception { public List<FanartDescriptor> getArtwork(String category, String id) throws Exception {
return getArtwork("movie", id, type, sort, limit); String resource = getResource(category, id);
}
public List<FanartDescriptor> getArtwork(String category, String id, String type, int sort, int limit) throws Exception {
String resource = getResource(category, id, "xml", type, sort, limit);
// cache results // cache results
CachedResource<FanartDescriptor[]> data = new CachedResource<FanartDescriptor[]>(resource, FanartDescriptor[].class) { CachedResource<FanartDescriptor[]> data = new CachedResource<FanartDescriptor[]>(resource, FanartDescriptor[].class) {
@Override @Override
public FanartDescriptor[] process(ByteBuffer data) throws Exception { public FanartDescriptor[] process(ByteBuffer data) throws Exception {
Document dom = getDocument(Charset.forName("UTF-8").decode(data).toString()); String json = Charset.forName("UTF-8").decode(data).toString();
Map<?, ?> maps = JsonReader.jsonToMaps(json);
List<FanartDescriptor> fanart = new ArrayList<FanartDescriptor>(); List<FanartDescriptor> fanart = new ArrayList<FanartDescriptor>();
for (Node node : selectNodes("//*[@url]", dom)) { maps.forEach((k, v) -> {
// e.g. <seasonthumb id="3481" url="http://fanart.tv/fanart/tv/70327/seasonthumb/3481/Buffy (6).jpg" lang="en" likes="0" season="6"/> if (v instanceof JsonObject) {
Map<FanartProperty, String> fields = new EnumMap<FanartProperty, String>(FanartProperty.class); JsonObject<?, ?> mapping = (JsonObject<?, ?>) v;
fields.put(FanartProperty.type, node.getNodeName()); if (mapping.isArray()) {
for (FanartProperty prop : FanartProperty.values()) { for (Object i : mapping.getArray()) {
String value = getAttribute(prop.name(), node); if (i instanceof Map) {
if (value != null) { Map<?, ?> item = (Map<?, ?>) i;
fields.put(prop, value); Map<FanartProperty, String> fields = new EnumMap<FanartProperty, String>(FanartProperty.class);
fields.put(FanartProperty.type, k.toString());
for (FanartProperty prop : FanartProperty.values()) {
Object value = item.get(prop.name());
if (value != null) {
fields.put(prop, value.toString());
}
}
if (fields.size() > 1) {
fanart.add(new FanartDescriptor(fields));
}
}
}
} }
} }
fanart.add(new FanartDescriptor(fields)); });
}
return fanart.toArray(new FanartDescriptor[0]); return fanart.toArray(new FanartDescriptor[0]);
} }
@ -83,15 +82,15 @@ public class FanartTVClient {
return Arrays.asList(data.get()); return Arrays.asList(data.get());
} }
public String getResource(String category, String id, String format, String type, int sort, int limit) throws MalformedURLException { public String getResource(String category, String id) {
// e.g. http://fanart.tv/webservice/series/780b986b22c35e6f7a134a2f392c2deb/70327/xml/all/1/2 // e.g. http://webservice.fanart.tv/v3/movies/17645?api_key=6fa42b0ef3b5f3aab6a7edaa78675ac2
return String.format("http://api.fanart.tv/webservice/%s/%s/%s/%s/%s/%s/%s", category, apikey, id, format, type, sort, limit); return "http://webservice.fanart.tv/v3/" + category + "/" + id + "?api_key=" + apikey;
} }
public static class FanartDescriptor implements Serializable { public static class FanartDescriptor implements Serializable {
public static enum FanartProperty { public static enum FanartProperty {
type, id, url, lang, likes, season, disc_type type, id, url, lang, likes, season, disc, disc_type
} }
protected Map<FanartProperty, String> fields; protected Map<FanartProperty, String> fields;
@ -124,13 +123,9 @@ public class FanartTVClient {
} }
} }
public String getName() {
return new File(getUrl().getFile()).getName();
}
public URL getUrl() { public URL getUrl() {
try { try {
return new URL(fields.get(FanartProperty.url).replaceAll(" ", "%20")); // work around server-side url encoding issues return new URL(fields.get(FanartProperty.url));
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
@ -160,6 +155,14 @@ public class FanartTVClient {
} }
} }
public Integer getDiskNumber() {
try {
return new Integer(fields.get(FanartProperty.disc));
} catch (Exception e) {
return null;
}
}
public String getDiskType() { public String getDiskType() {
return fields.get(FanartProperty.disc_type); return fields.get(FanartProperty.disc_type);
} }