1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 08:18:52 -05:00

Experiment with unifying Artwork interface for all databases

This commit is contained in:
Reinhard Pointner 2016-04-17 12:40:37 +00:00
parent 91639b40e5
commit b54908475b
5 changed files with 32 additions and 190 deletions

View File

@ -5,8 +5,11 @@ import static java.util.Collections.*;
import java.io.Serializable;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;
public class Artwork implements Serializable {
@ -22,12 +25,12 @@ public class Artwork implements Serializable {
// used by serializer
}
public Artwork(Datasource database, List<String> category, URL url, Locale language, double rating) {
public Artwork(Datasource database, Stream<?> category, URL url, Locale language, Double rating) {
this.database = database.getIdentifier();
this.category = category.toArray(new String[0]);
this.category = category.filter(Objects::nonNull).map(Object::toString).toArray(String[]::new);
this.url = url;
this.language = language.getLanguage();
this.rating = rating;
this.language = language == null || language.getLanguage().isEmpty() ? null : language.getLanguage();
this.rating = rating == null ? 0 : rating;
}
public String getDatabase() {
@ -66,7 +69,7 @@ public class Artwork implements Serializable {
@Override
public String toString() {
return asList(database, asList(category), url, language, rating).toString();
return asList(String.join("/", category), language, new DecimalFormat("0.##").format(rating), url).toString();
}
}

View File

@ -1,24 +1,20 @@
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;
import java.net.URL;
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 java.util.stream.Stream;
import javax.swing.Icon;
import net.filebot.Cache;
import net.filebot.CacheType;
import net.filebot.web.FanartTVClient.FanartDescriptor.FanartProperty;
public class FanartTVClient implements Datasource, ArtworkProvider {
@ -38,121 +34,11 @@ public class FanartTVClient implements Datasource, ArtworkProvider {
return null;
}
public List<FanartDescriptor> getSeriesArtwork(int tvdbid) throws Exception {
return getArtwork("tv", String.valueOf(tvdbid));
}
public List<FanartDescriptor> getMovieArtwork(int tmdbid) throws Exception {
return getArtwork("movies", String.valueOf(tmdbid));
}
public List<FanartDescriptor> getArtwork(String category, String id) throws Exception {
String path = category + '/' + id;
Cache cache = Cache.getCache(getName(), CacheType.Weekly);
Object json = cache.json(path, s -> getResource(s)).expire(Cache.ONE_WEEK).get();
return asMap(json).entrySet().stream().flatMap(type -> {
return streamJsonObjects(type.getValue()).map(item -> {
Map<FanartProperty, String> map = getEnumMap(item, FanartProperty.class);
map.put(FanartProperty.type, type.getKey().toString());
return new FanartDescriptor(map);
}).filter(art -> art.getUrl() != null);
}).collect(toList());
}
public URL getResource(String path) throws Exception {
// e.g. http://webservice.fanart.tv/v3/movies/17645?api_key=6fa42b0ef3b5f3aab6a7edaa78675ac2
return new URL("http://webservice.fanart.tv/v3/" + path + "?api_key=" + apikey);
}
public static class FanartDescriptor implements Serializable {
public static enum FanartProperty {
type, id, url, lang, likes, season, disc, disc_type
}
protected Map<FanartProperty, String> properties;
protected FanartDescriptor() {
// used by serializer
}
protected FanartDescriptor(Map<FanartProperty, String> fields) {
this.properties = new EnumMap<FanartProperty, String>(fields);
}
public String get(Object key) {
return properties.get(FanartProperty.valueOf(key.toString()));
}
public String get(FanartProperty key) {
return properties.get(key);
}
public String getType() {
return properties.get(FanartProperty.type);
}
public Integer getId() {
try {
return new Integer(properties.get(FanartProperty.id));
} catch (Exception e) {
return null;
}
}
public URL getUrl() {
try {
return new URL(properties.get(FanartProperty.url));
} catch (Exception e) {
return null;
}
}
public Integer getLikes() {
try {
return new Integer(properties.get(FanartProperty.likes));
} catch (Exception e) {
return null;
}
}
public Locale getLanguage() {
try {
return new Locale(properties.get(FanartProperty.lang));
} catch (Exception e) {
return null;
}
}
public Integer getSeason() {
try {
return new Integer(properties.get(FanartProperty.season));
} catch (Exception e) {
return null;
}
}
public Integer getDiskNumber() {
try {
return new Integer(properties.get(FanartProperty.disc));
} catch (Exception e) {
return null;
}
}
public String getDiskType() {
return properties.get(FanartProperty.disc_type);
}
@Override
public String toString() {
return properties.toString();
}
}
@Override
public List<Artwork> getArtwork(int id, String category, Locale locale) throws Exception {
Cache cache = Cache.getCache(getName(), CacheType.Weekly);
@ -162,12 +48,12 @@ public class FanartTVClient implements Datasource, ArtworkProvider {
return streamJsonObjects(type.getValue()).map(it -> {
try {
String url = getString(it, "url");
String lang = getString(it, "lang");
Locale language = getStringValue(it, "lang", Locale::new);
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);
return new Artwork(this, Stream.of(type.getKey(), season, discType), new URL(url), language, likes);
} catch (Exception e) {
debug.log(Level.WARNING, e, e::getMessage);
return null;

View File

@ -39,7 +39,7 @@ import net.filebot.ResourceManager;
import net.filebot.web.TMDbClient.MovieInfo.MovieProperty;
import net.filebot.web.TMDbClient.Person.PersonProperty;
public class TMDbClient implements MovieIdentificationService {
public class TMDbClient implements MovieIdentificationService, ArtworkProvider {
private static final String host = "api.themoviedb.org";
private static final String version = "3";
@ -270,28 +270,26 @@ public class TMDbClient implements MovieIdentificationService {
return new MovieInfo(fields, alternativeTitles, genres, certifications, spokenLanguages, productionCountries, productionCompanies, cast, trailers);
}
public List<Artwork> getArtwork(String id) throws Exception {
// http://api.themoviedb.org/3/movie/11/images
@Override
public List<Artwork> getArtwork(int id, String category, Locale locale) throws Exception {
Object config = request("configuration", emptyMap(), Locale.ROOT, REQUEST_LIMIT);
String baseUrl = getString(getMap(config, "images"), "secure_base_url");
URL baseUrl = new URL(getString(getMap(config, "images"), "secure_base_url"));
Object images = request("movie/" + id + "/images", emptyMap(), Locale.ROOT, REQUEST_LIMIT);
return Stream.of("backdrops", "posters").flatMap(section -> {
Stream<Artwork> artwork = streamJsonObjects(images, section).map(it -> {
try {
String url = baseUrl + "original" + getString(it, "file_path");
int width = getDecimal(it, "width").intValue();
int height = getDecimal(it, "height").intValue();
String lang = getString(it, "iso_639_1");
return new Artwork(section, new URL(url), width, height, lang);
} catch (Exception e) {
debug.warning(format("Bad artwork: %s => %s", it, e));
return null;
}
});
return artwork;
}).collect(toList());
return streamJsonObjects(images, category).map(it -> {
try {
String path = "original" + getString(it, "file_path");
String width = getString(it, "width");
String height = getString(it, "height");
Locale language = getStringValue(it, "iso_639_1", Locale::new);
return new Artwork(this, Stream.of(category, String.join("x", width, height)), new URL(baseUrl, path), language, null);
} catch (Exception e) {
debug.log(Level.WARNING, e, e::getMessage);
return null;
}
}).filter(Objects::nonNull).collect(toList());
}
protected Object request(String resource, Map<String, Object> parameters, Locale locale, final FloodLimit limit) throws Exception {
@ -645,50 +643,6 @@ public class TMDbClient implements MovieIdentificationService {
}
}
public static class Artwork {
private String category;
private String language;
private int width;
private int height;
private URL url;
public Artwork(String category, URL url, int width, int height, String language) {
this.category = category;
this.url = url;
this.width = width;
this.height = height;
this.language = language;
}
public String getCategory() {
return category;
}
public String getLanguage() {
return language;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public URL getUrl() {
return url;
}
@Override
public String toString() {
return String.format("{category: %s, width: %s, height: %s, language: %s, url: %s}", category, width, height, language, url);
}
}
public static class Trailer {
private String type;

View File

@ -238,7 +238,7 @@ public class TheTVDBClient2 extends AbstractEpisodeListProvider implements Artwo
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);
return new Artwork(this, Stream.of(category, subKey, resolution), new URL(mirror, fileName), locale, rating);
} catch (Exception e) {
debug.log(Level.WARNING, e, e::getMessage);
return null;

View File

@ -15,7 +15,6 @@ import org.junit.Test;
import net.filebot.Cache;
import net.filebot.CacheType;
import net.filebot.CachedResource;
import net.filebot.web.TMDbClient.Artwork;
import net.filebot.web.TMDbClient.MovieInfo;
public class TMDbClientTest {
@ -89,9 +88,9 @@ public class TMDbClientTest {
@Test
public void getArtwork() throws Exception {
List<Artwork> artwork = tmdb.getArtwork("tt0418279");
assertEquals("backdrops", artwork.get(0).getCategory());
assertEquals("https://image.tmdb.org/t/p/original/ac0HwGJIU3GxjjGujlIjLJmAGPR.jpg", artwork.get(0).getUrl().toString());
Artwork a = tmdb.getArtwork(16320, "backdrops", Locale.ROOT).get(0);
assertEquals("[backdrops, 1920x1080]", a.getCategory().toString());
assertEquals("https://image.tmdb.org/t/p/original/424MxHQe5Hfu92hTeRvZb5Giv0X.jpg", a.getUrl().toString());
}
@Ignore