1
0
mirror of https://github.com/mitb-archive/filebot synced 2025-01-10 21:38:04 -05:00

Experiment with unifying Artwork interface for all databases

This commit is contained in:
Reinhard Pointner 2016-04-17 11:34:22 +00:00
parent 0c94aed7e7
commit 91639b40e5
5 changed files with 133 additions and 73 deletions

View File

@ -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<String> 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<String> 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();
}
}

View File

@ -0,0 +1,10 @@
package net.filebot.web;
import java.util.List;
import java.util.Locale;
public interface ArtworkProvider {
List<Artwork> getArtwork(int id, String category, Locale locale) throws Exception;
}

View File

@ -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<Artwork> 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());
}
}

View File

@ -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<Image> getImages(SearchResult series, String keyType) throws Exception {
Object json = requestJson("series/" + series.getId() + "/images/query?keyType=" + keyType, null, Cache.ONE_WEEK);
@Override
public List<Artwork> 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());
}
}

View File

@ -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());
}
}