1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-02 08:25:02 -04:00

Experiment with unifying Artwork interface for all databases

This commit is contained in:
Reinhard Pointner 2016-04-17 17:32:49 +00:00
parent b54908475b
commit cd37c87002
2 changed files with 28 additions and 178 deletions

View File

@ -8,7 +8,6 @@ import static net.filebot.util.XPathUtilities.*;
import static net.filebot.web.EpisodeUtilities.*; import static net.filebot.web.EpisodeUtilities.*;
import static net.filebot.web.WebRequest.*; import static net.filebot.web.WebRequest.*;
import java.io.Serializable;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@ -18,8 +17,11 @@ import java.util.LinkedHashMap;
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 java.util.Objects;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
import java.util.stream.Stream;
import javax.swing.Icon; import javax.swing.Icon;
@ -30,10 +32,8 @@ import net.filebot.Cache;
import net.filebot.Cache.TypedCache; import net.filebot.Cache.TypedCache;
import net.filebot.CacheType; import net.filebot.CacheType;
import net.filebot.ResourceManager; 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<MirrorType, String> mirrors = MirrorType.newMap(); private final Map<MirrorType, String> mirrors = MirrorType.newMap();
@ -324,42 +324,26 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
return URI.create("http://www.thetvdb.com/?tab=seasonall&id=" + searchResult.getId()); return URI.create("http://www.thetvdb.com/?tab=seasonall&id=" + searchResult.getId());
} }
/** @Override
* Search for a series banner matching the given parameters public List<Artwork> getArtwork(int id, String category, Locale locale) throws Exception {
* Document dom = getXmlResource(MirrorType.XML, "series/" + id + "/banners.xml");
* @see http://thetvdb.com/wiki/index.php/API:banners.xml URL mirror = getResource(MirrorType.BANNER, "");
*/
public BannerDescriptor getBanner(SearchResult series, Map<?, ?> filterDescriptor) throws Exception { return streamNodes("//Banner", dom).map(node -> {
EnumMap<BannerProperty, String> filter = new EnumMap<BannerProperty, String>(BannerProperty.class); try {
for (Entry<?, ?> it : filterDescriptor.entrySet()) { String type = getTextContent("BannerType", node);
if (it.getValue() != null) { String subKey = getTextContent("BannerType2", node);
filter.put(BannerProperty.valueOf(it.getKey().toString()), it.getValue().toString()); 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;
} }
} }).filter(Objects::nonNull).filter(it -> it.getCategory().contains(category)).collect(toList());
// 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<BannerDescriptor> 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<BannerProperty, String> map = getEnumMap(n, BannerProperty.class);
map.put(BannerProperty.BannerMirror, bannerMirror);
return new BannerDescriptor(map);
}).filter(m -> m.getUrl() != null).collect(toList());
});
} }
protected TypedCache<SearchResult> getLookupCache(String type, Locale language) { protected TypedCache<SearchResult> 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); return Cache.getCache(getName() + "_" + "lookup" + "_" + type + "_" + language, CacheType.Monthly).cast(SearchResult.class);
} }
protected TypedCache<List<BannerDescriptor>> 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<BannerProperty, String> fields;
protected BannerDescriptor() {
// used by serializer
}
protected BannerDescriptor(Map<BannerProperty, String> fields) {
this.fields = new EnumMap<BannerProperty, String>(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();
}
}
} }

View File

@ -3,14 +3,11 @@ package net.filebot.web;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import net.filebot.web.TheTVDBClient.BannerDescriptor;
import net.filebot.web.TheTVDBClient.MirrorType; import net.filebot.web.TheTVDBClient.MirrorType;
public class TheTVDBClientTest { public class TheTVDBClientTest {
@ -146,28 +143,14 @@ public class TheTVDBClientTest {
@Test @Test
public void getBanner() throws Exception { public void getBanner() throws Exception {
Map<String, String> filter = new HashMap<String, String>(); Artwork banner = thetvdb.getArtwork(buffy.getId(), "season", Locale.ROOT).stream().filter(it -> {
filter.put("BannerType", "season"); return it.getCategory().contains("season") && it.getCategory().contains("seasonwide") && it.getCategory().contains("7") && it.getLanguage().equals("en");
filter.put("BannerType2", "seasonwide"); }).findFirst().get();
filter.put("Season", "7");
filter.put("Language", "en");
BannerDescriptor banner = thetvdb.getBanner(buffy, filter); assertEquals("season", banner.getCategory().get(0));
assertEquals("seasonwide", banner.getCategory().get(1));
assertEquals(857660, banner.getId(), 0);
assertEquals("season", banner.getBannerType());
assertEquals("seasonwide", banner.getBannerType2());
assertEquals("http://thetvdb.com/banners/seasonswide/70327-7.jpg", banner.getUrl().toString()); assertEquals("http://thetvdb.com/banners/seasonswide/70327-7.jpg", banner.getUrl().toString());
assertEquals(99712, WebRequest.fetch(banner.getUrl()).remaining(), 0); assertEquals(99712, WebRequest.fetch(banner.getUrl()).remaining(), 0);
} }
@Test
public void getBannerList() throws Exception {
List<BannerDescriptor> 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());
}
} }