From cd37c8700275b532a6fbe95eaaabc3a2b459eb36 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 17 Apr 2016 17:32:49 +0000 Subject: [PATCH] Experiment with unifying Artwork interface for all databases --- source/net/filebot/web/TheTVDBClient.java | 179 +++----------------- test/net/filebot/web/TheTVDBClientTest.java | 27 +-- 2 files changed, 28 insertions(+), 178 deletions(-) diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index dba08e2d..24e8c863 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -8,7 +8,6 @@ import static net.filebot.util.XPathUtilities.*; import static net.filebot.web.EpisodeUtilities.*; import static net.filebot.web.WebRequest.*; -import java.io.Serializable; import java.net.URI; import java.net.URL; import java.util.ArrayList; @@ -18,8 +17,11 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Map.Entry; import java.util.Random; +import java.util.logging.Level; +import java.util.stream.Stream; import javax.swing.Icon; @@ -30,10 +32,8 @@ import net.filebot.Cache; import net.filebot.Cache.TypedCache; import net.filebot.CacheType; import net.filebot.ResourceManager; -import net.filebot.util.FileUtilities; -import net.filebot.web.TheTVDBClient.BannerDescriptor.BannerProperty; -public class TheTVDBClient extends AbstractEpisodeListProvider { +public class TheTVDBClient extends AbstractEpisodeListProvider implements ArtworkProvider { private final Map mirrors = MirrorType.newMap(); @@ -324,42 +324,26 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { return URI.create("http://www.thetvdb.com/?tab=seasonall&id=" + searchResult.getId()); } - /** - * Search for a series banner matching the given parameters - * - * @see http://thetvdb.com/wiki/index.php/API:banners.xml - */ - public BannerDescriptor getBanner(SearchResult series, Map filterDescriptor) throws Exception { - EnumMap filter = new EnumMap(BannerProperty.class); - for (Entry it : filterDescriptor.entrySet()) { - if (it.getValue() != null) { - filter.put(BannerProperty.valueOf(it.getKey().toString()), it.getValue().toString()); + @Override + public List getArtwork(int id, String category, Locale locale) throws Exception { + Document dom = getXmlResource(MirrorType.XML, "series/" + id + "/banners.xml"); + URL mirror = getResource(MirrorType.BANNER, ""); + + return streamNodes("//Banner", dom).map(node -> { + try { + String type = getTextContent("BannerType", node); + String subKey = getTextContent("BannerType2", node); + String fileName = getTextContent("BannerPath", node); + String season = getTextContent("Season", node); + String language = getTextContent("Language", node); + Double rating = getDecimal(getTextContent("Rating", node)); + + return new Artwork(this, Stream.of(type, subKey, season), new URL(mirror, fileName), language == null ? null : new Locale(language), rating); + } catch (Exception e) { + debug.log(Level.WARNING, e, e::getMessage); + return null; } - } - - // search for a banner matching the selector - for (BannerDescriptor it : getBannerList(series)) { - if (it.fields.entrySet().containsAll(filter.entrySet())) { - return it; - } - } - - return null; - } - - public List getBannerList(SearchResult series) throws Exception { - return getBannerCache().computeIfAbsent(series.getId(), it -> { - Document dom = getXmlResource(MirrorType.XML, "series/" + series.getId() + "/banners.xml"); - - String bannerMirror = getResource(MirrorType.BANNER, "").toString(); - - return streamNodes("//Banner", dom).map(n -> { - Map map = getEnumMap(n, BannerProperty.class); - map.put(BannerProperty.BannerMirror, bannerMirror); - - return new BannerDescriptor(map); - }).filter(m -> m.getUrl() != null).collect(toList()); - }); + }).filter(Objects::nonNull).filter(it -> it.getCategory().contains(category)).collect(toList()); } protected TypedCache getLookupCache(String type, Locale language) { @@ -367,121 +351,4 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { return Cache.getCache(getName() + "_" + "lookup" + "_" + type + "_" + language, CacheType.Monthly).cast(SearchResult.class); } - protected TypedCache> getBannerCache() { - // banners do not change that often so we can cache them for longer - return Cache.getCache(getName() + "_" + "banner", CacheType.Weekly).castList(BannerDescriptor.class); - } - - public static class BannerDescriptor implements Serializable { - - public static enum BannerProperty { - id, BannerMirror, BannerPath, BannerType, BannerType2, Season, Colors, Language, Rating, RatingCount, SeriesName, ThumbnailPath, VignettePath - } - - protected Map fields; - - protected BannerDescriptor() { - // used by serializer - } - - protected BannerDescriptor(Map fields) { - this.fields = new EnumMap(fields); - } - - public String get(Object key) { - return fields.get(BannerProperty.valueOf(key.toString())); - } - - public String get(BannerProperty key) { - return fields.get(key); - } - - public URL getBannerMirrorUrl(String path) { - try { - return new URL(new URL(get(BannerProperty.BannerMirror)), path); - } catch (Exception e) { - debug.finest(format("Bad banner url: %s", e)); - return null; - } - } - - public URL getUrl() { - return getBannerMirrorUrl(get(BannerProperty.BannerPath)); - } - - public String getExtension() { - return FileUtilities.getExtension(get(BannerProperty.BannerPath)); - } - - public Integer getId() { - try { - return new Integer(get(BannerProperty.id)); - } catch (Exception e) { - return null; - } - } - - public String getBannerType() { - return get(BannerProperty.BannerType); - } - - public String getBannerType2() { - return get(BannerProperty.BannerType2); - } - - public Integer getSeason() { - try { - return new Integer(get(BannerProperty.Season)); - } catch (Exception e) { - return null; - } - } - - public String getColors() { - return get(BannerProperty.Colors); - } - - public Locale getLocale() { - try { - return new Locale(get(BannerProperty.Language)); - } catch (Exception e) { - return null; - } - } - - public Double getRating() { - try { - return new Double(get(BannerProperty.Rating)); - } catch (Exception e) { - return null; - } - } - - public Integer getRatingCount() { - try { - return new Integer(get(BannerProperty.RatingCount)); - } catch (Exception e) { - return null; - } - } - - public boolean hasSeriesName() { - return Boolean.parseBoolean(get(BannerProperty.SeriesName)); - } - - public URL getThumbnailUrl() { - return getBannerMirrorUrl(get(BannerProperty.ThumbnailPath)); - } - - public URL getVignetteUrl() { - return getBannerMirrorUrl(get(BannerProperty.VignettePath)); - } - - @Override - public String toString() { - return fields.toString(); - } - - } - } diff --git a/test/net/filebot/web/TheTVDBClientTest.java b/test/net/filebot/web/TheTVDBClientTest.java index 02c50575..eb6ba1a7 100644 --- a/test/net/filebot/web/TheTVDBClientTest.java +++ b/test/net/filebot/web/TheTVDBClientTest.java @@ -3,14 +3,11 @@ package net.filebot.web; import static org.junit.Assert.*; import java.util.EnumSet; -import java.util.HashMap; import java.util.List; import java.util.Locale; -import java.util.Map; import org.junit.Test; -import net.filebot.web.TheTVDBClient.BannerDescriptor; import net.filebot.web.TheTVDBClient.MirrorType; public class TheTVDBClientTest { @@ -146,28 +143,14 @@ public class TheTVDBClientTest { @Test public void getBanner() throws Exception { - Map filter = new HashMap(); - filter.put("BannerType", "season"); - filter.put("BannerType2", "seasonwide"); - filter.put("Season", "7"); - filter.put("Language", "en"); + Artwork banner = thetvdb.getArtwork(buffy.getId(), "season", Locale.ROOT).stream().filter(it -> { + return it.getCategory().contains("season") && it.getCategory().contains("seasonwide") && it.getCategory().contains("7") && it.getLanguage().equals("en"); + }).findFirst().get(); - BannerDescriptor banner = thetvdb.getBanner(buffy, filter); - - assertEquals(857660, banner.getId(), 0); - assertEquals("season", banner.getBannerType()); - assertEquals("seasonwide", banner.getBannerType2()); + assertEquals("season", banner.getCategory().get(0)); + assertEquals("seasonwide", banner.getCategory().get(1)); assertEquals("http://thetvdb.com/banners/seasonswide/70327-7.jpg", banner.getUrl().toString()); assertEquals(99712, WebRequest.fetch(banner.getUrl()).remaining(), 0); } - @Test - public void getBannerList() throws Exception { - List banners = thetvdb.getBannerList(buffy); - - assertEquals("fanart", banners.get(0).getBannerType()); - assertEquals("1280x720", banners.get(0).getBannerType2()); - assertEquals(460058, WebRequest.fetch(banners.get(0).getUrl()).remaining()); - } - }