diff --git a/source/net/filebot/web/Artwork.java b/source/net/filebot/web/Artwork.java new file mode 100644 index 00000000..acb387a1 --- /dev/null +++ b/source/net/filebot/web/Artwork.java @@ -0,0 +1,72 @@ +package net.filebot.web; + +import static java.util.Arrays.*; +import static java.util.Collections.*; + +import java.io.Serializable; +import java.net.URL; +import java.util.List; +import java.util.Locale; + +public class Artwork implements Serializable { + + private String database; + + private String[] category; + private URL url; + + private String language; + private double rating; + + public Artwork() { + // used by serializer + } + + public Artwork(Datasource database, List category, URL url, Locale language, double rating) { + this.database = database.getIdentifier(); + this.category = category.toArray(new String[0]); + this.url = url; + this.language = language.getLanguage(); + this.rating = rating; + } + + public String getDatabase() { + return database; + } + + public List getCategory() { + return unmodifiableList(asList(category)); + } + + public URL getUrl() { + return url; + } + + public String getLanguage() { + return language; + } + + public double getRating() { + return rating; + } + + @Override + public int hashCode() { + return url.hashCode(); + } + + @Override + public boolean equals(Object other) { + if (other instanceof Artwork) { + Artwork artwork = (Artwork) other; + return url.sameFile(artwork.url); + } + return false; + } + + @Override + public String toString() { + return asList(database, asList(category), url, language, rating).toString(); + } + +} diff --git a/source/net/filebot/web/ArtworkProvider.java b/source/net/filebot/web/ArtworkProvider.java new file mode 100644 index 00000000..d34f388d --- /dev/null +++ b/source/net/filebot/web/ArtworkProvider.java @@ -0,0 +1,10 @@ +package net.filebot.web; + +import java.util.List; +import java.util.Locale; + +public interface ArtworkProvider { + + List getArtwork(int id, String category, Locale locale) throws Exception; + +} diff --git a/source/net/filebot/web/FanartTVClient.java b/source/net/filebot/web/FanartTVClient.java index df900a83..a9a68dc8 100644 --- a/source/net/filebot/web/FanartTVClient.java +++ b/source/net/filebot/web/FanartTVClient.java @@ -1,6 +1,8 @@ package net.filebot.web; +import static java.util.Arrays.*; import static java.util.stream.Collectors.*; +import static net.filebot.Logging.*; import static net.filebot.util.JsonUtilities.*; import java.io.Serializable; @@ -9,6 +11,8 @@ import java.util.EnumMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; import javax.swing.Icon; @@ -16,7 +20,7 @@ import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.web.FanartTVClient.FanartDescriptor.FanartProperty; -public class FanartTVClient implements Datasource { +public class FanartTVClient implements Datasource, ArtworkProvider { private String apikey; @@ -149,4 +153,27 @@ public class FanartTVClient implements Datasource { } } + @Override + public List getArtwork(int id, String category, Locale locale) throws Exception { + Cache cache = Cache.getCache(getName(), CacheType.Weekly); + Object json = cache.json(category + '/' + id, s -> getResource(s)).expire(Cache.ONE_WEEK).get(); + + return asMap(json).entrySet().stream().flatMap(type -> { + return streamJsonObjects(type.getValue()).map(it -> { + try { + String url = getString(it, "url"); + String lang = getString(it, "lang"); + Double likes = getDecimal(it, "likes"); + String season = getString(it, "season"); + String discType = getString(it, "disc_type"); + + return new Artwork(this, asList(category, type.getKey().toString(), season, discType), new URL(url), new Locale(lang), likes == null ? 0 : likes); + } catch (Exception e) { + debug.log(Level.WARNING, e, e::getMessage); + return null; + } + }); + }).filter(Objects::nonNull).collect(toList()); + } + } diff --git a/source/net/filebot/web/TheTVDBClient2.java b/source/net/filebot/web/TheTVDBClient2.java index 9eb4d339..3f437ce0 100644 --- a/source/net/filebot/web/TheTVDBClient2.java +++ b/source/net/filebot/web/TheTVDBClient2.java @@ -11,7 +11,6 @@ import static net.filebot.util.StringUtilities.*; 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.nio.ByteBuffer; @@ -22,6 +21,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.logging.Level; import java.util.stream.Stream; import javax.swing.Icon; @@ -30,7 +30,7 @@ import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.ResourceManager; -public class TheTVDBClient2 extends AbstractEpisodeListProvider { +public class TheTVDBClient2 extends AbstractEpisodeListProvider implements ArtworkProvider { private String apikey; @@ -60,7 +60,7 @@ public class TheTVDBClient2 extends AbstractEpisodeListProvider { } protected Object requestJson(String path, Locale locale, Duration expirationTime) throws Exception { - Cache cache = Cache.getCache(locale == null ? getName() : getName() + "_" + locale.getLanguage(), CacheType.Monthly); + Cache cache = Cache.getCache(locale == null || locale == Locale.ROOT ? getName() : getName() + "_" + locale.getLanguage(), CacheType.Monthly); return cache.json(path, this::getEndpoint).fetch(fetchIfModified(() -> getRequestHeader(locale))).expire(expirationTime).get(); } @@ -224,71 +224,26 @@ public class TheTVDBClient2 extends AbstractEpisodeListProvider { return URI.create("http://www.thetvdb.com/?tab=seasonall&id=" + searchResult.getId()); } - public List getImages(SearchResult series, String keyType) throws Exception { - Object json = requestJson("series/" + series.getId() + "/images/query?keyType=" + keyType, null, Cache.ONE_WEEK); + @Override + public List getArtwork(int id, String category, Locale locale) throws Exception { + Object json = requestJson("series/" + id + "/images/query?keyType=" + category, locale, Cache.ONE_WEEK); + + // TheTVDB API v2 does not have a dedicated banner mirror + URL mirror = new URL("http://thetvdb.com/banners/"); return streamJsonObjects(json, "data").map(it -> { - Integer id = getInteger(it, "id"); - String subKey = getString(it, "subKey"); - String fileName = getString(it, "fileName"); - String resolution = getString(it, "resolution"); - Double rating = getDecimal(getString(it, "ratingsInfo"), "average"); - - return new Image(id, keyType, subKey, fileName, resolution, rating); - }).collect(toList()); - } - - public static class Image implements Serializable { - - private Integer id; - private String keyType; - private String subKey; - private String fileName; - private String resolution; - private Double rating; - - protected Image() { - // used by serializer - } - - public Image(Integer id, String keyType, String subKey, String fileName, String resolution, Double rating) { - this.id = id; - this.keyType = keyType; - this.subKey = subKey; - this.fileName = fileName; - this.resolution = resolution; - this.rating = rating; - } - - public Integer getId() { - return id; - } - - public String getKeyType() { - return keyType; - } - - public String getSubKey() { - return subKey; - } - - public String getFileName() { - return fileName; - } - - public String getResolution() { - return resolution; - } - - public Double getRating() { - return rating; - } - - @Override - public String toString() { - return "[id=" + id + ", keyType=" + keyType + ", subKey=" + subKey + ", fileName=" + fileName + ", resolution=" + resolution + ", rating=" + rating + "]"; - } + try { + String subKey = getString(it, "subKey"); + String fileName = getString(it, "fileName"); + String resolution = getString(it, "resolution"); + Double rating = getDecimal(getString(it, "ratingsInfo"), "average"); + return new Artwork(this, asList(category, subKey, resolution), new URL(mirror, fileName), locale, rating == null ? 0 : rating); + } catch (Exception e) { + debug.log(Level.WARNING, e, e::getMessage); + return null; + } + }).filter(Objects::nonNull).collect(toList()); } } diff --git a/test/net/filebot/web/TheTVDBClient2Test.java b/test/net/filebot/web/TheTVDBClient2Test.java index b1de6fd0..42fe70de 100644 --- a/test/net/filebot/web/TheTVDBClient2Test.java +++ b/test/net/filebot/web/TheTVDBClient2Test.java @@ -8,8 +8,6 @@ import java.util.Locale; import org.junit.Test; -import net.filebot.web.TheTVDBClient2.Image; - public class TheTVDBClient2Test { TheTVDBClient2 thetvdb = new TheTVDBClient2("BA864DEE427E384A"); @@ -140,12 +138,10 @@ public class TheTVDBClient2Test { @Test public void getImages() throws Exception { - Image i = thetvdb.getImages(buffy, "fanart").get(0); + Artwork i = thetvdb.getArtwork(buffy.getId(), "fanart", Locale.ENGLISH).get(0); - assertEquals("fanart", i.getKeyType()); - assertEquals(null, i.getSubKey()); - assertEquals("1280x720", i.getResolution()); - assertEquals("fanart/original/70327-1.jpg", i.getFileName()); + assertEquals("[fanart, null, 1280x720]", i.getCategory().toString()); + assertEquals("http://thetvdb.com/banners/fanart/original/70327-1.jpg", i.getUrl().toString()); } }