* internal support for grabbing trailer data with TheMovieDB client
This commit is contained in:
parent
3f6487b621
commit
326d397784
@ -1,10 +1,9 @@
|
||||
|
||||
package net.sourceforge.filebot.web;
|
||||
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static net.sourceforge.filebot.web.WebRequest.*;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static java.util.Collections.unmodifiableList;
|
||||
import static net.sourceforge.filebot.web.WebRequest.encodeParameters;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@ -39,7 +38,6 @@ import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
|
||||
public class TMDbClient implements MovieIdentificationService {
|
||||
|
||||
private static final String host = "api.themoviedb.org";
|
||||
@ -50,31 +48,28 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
|
||||
private final String apikey;
|
||||
|
||||
|
||||
public TMDbClient(String apikey) {
|
||||
this.apikey = apikey;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "TheMovieDB";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Icon getIcon() {
|
||||
return ResourceManager.getIcon("search.themoviedb");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Movie> searchMovie(String query, Locale locale) throws IOException {
|
||||
JSONObject response = request("search/movie", singletonMap("query", query), locale, SEARCH_LIMIT);
|
||||
List<Movie> result = new ArrayList<Movie>();
|
||||
|
||||
for (JSONObject it : jsonList(response.get("results"))) {
|
||||
// e.g. {"id":16320,"title":"冲出宁静号","release_date":"2005-09-30","original_title":"Serenity"}
|
||||
// e.g.
|
||||
// {"id":16320,"title":"冲出宁静号","release_date":"2005-09-30","original_title":"Serenity"}
|
||||
String title = (String) it.get("title");
|
||||
if (title == null || title.isEmpty()) {
|
||||
title = (String) it.get("original_title");
|
||||
@ -97,12 +92,10 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public URI getMoviePageLink(int tmdbid) {
|
||||
return URI.create("http://www.themoviedb.org/movie/" + tmdbid);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Movie getMovieDescriptor(int imdbid, Locale locale) throws IOException {
|
||||
String id = String.format("tt%07d", imdbid);
|
||||
@ -118,13 +111,11 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<File, Movie> getMovieDescriptors(Collection<File> movieFiles, Locale locale) throws Exception {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
public MovieInfo getMovieInfo(Movie movie, Locale locale) throws IOException {
|
||||
if (movie.getTmdbId() >= 0) {
|
||||
return getMovieInfo(String.valueOf(movie.getTmdbId()), locale, true);
|
||||
@ -140,7 +131,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public MovieInfo getMovieInfo(String id, Locale locale, boolean extendedInfo) throws IOException {
|
||||
JSONObject response = request("movie/" + id, null, locale, REQUEST_LIMIT);
|
||||
|
||||
@ -195,9 +185,26 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
return new MovieInfo(fields, genres, spokenLanguages, cast);
|
||||
}
|
||||
List<Trailer> trailers = new ArrayList<Trailer>();
|
||||
if (extendedInfo) {
|
||||
JSONObject trailerResponse = request("movie/" + fields.get(MovieProperty.id) + "/trailers", null, null, REQUEST_LIMIT);
|
||||
for (String section : new String[] { "quicktime", "youtube" }) {
|
||||
for (JSONObject it : jsonList(trailerResponse.get(section))) {
|
||||
LinkedHashMap<String, String> sources = new LinkedHashMap<String, String>();
|
||||
if (it.containsKey("sources")) {
|
||||
for (JSONObject s : jsonList(it.get("sources"))) {
|
||||
sources.put(s.get("size").toString(), s.get("source").toString());
|
||||
}
|
||||
} else {
|
||||
sources.put(it.get("size").toString(), it.get("source").toString());
|
||||
}
|
||||
trailers.add(new Trailer(section, it.get("name").toString(), sources));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new MovieInfo(fields, genres, spokenLanguages, cast, trailers);
|
||||
}
|
||||
|
||||
public List<Artwork> getArtwork(String id) throws IOException {
|
||||
// http://api.themoviedb.org/3/movie/11/images
|
||||
@ -224,7 +231,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return artwork;
|
||||
}
|
||||
|
||||
|
||||
public JSONObject request(String resource, Map<String, String> parameters, Locale locale, final FloodLimit limit) throws IOException {
|
||||
// default parameters
|
||||
LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
|
||||
@ -245,7 +251,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return Charset.forName("UTF-8").decode(data).toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected ByteBuffer fetchData(URL url, long lastModified) throws IOException {
|
||||
try {
|
||||
@ -260,7 +265,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Cache getCache() {
|
||||
return CacheManager.getInstance().getCache("web-datasource");
|
||||
@ -274,7 +278,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
protected List<JSONObject> jsonList(final Object array) {
|
||||
return new AbstractList<JSONObject>() {
|
||||
|
||||
@ -283,7 +286,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return (JSONObject) ((JSONArray) array).get(index);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return ((JSONArray) array).size();
|
||||
@ -291,7 +293,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static class MovieInfo implements Serializable {
|
||||
|
||||
public static enum MovieProperty {
|
||||
@ -304,36 +305,32 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
protected String[] spokenLanguages;
|
||||
|
||||
protected Person[] people;
|
||||
|
||||
protected Trailer[] trailers;
|
||||
|
||||
protected MovieInfo() {
|
||||
// used by serializer
|
||||
}
|
||||
|
||||
|
||||
protected MovieInfo(Map<MovieProperty, String> fields, List<String> genres, List<String> spokenLanguages, List<Person> people) {
|
||||
protected MovieInfo(Map<MovieProperty, String> fields, List<String> genres, List<String> spokenLanguages, List<Person> people, List<Trailer> trailers) {
|
||||
this.fields = new EnumMap<MovieProperty, String>(fields);
|
||||
this.genres = genres.toArray(new String[0]);
|
||||
this.spokenLanguages = spokenLanguages.toArray(new String[0]);
|
||||
this.people = people.toArray(new Person[0]);
|
||||
this.trailers = trailers.toArray(new Trailer[0]);
|
||||
}
|
||||
|
||||
|
||||
public String get(Object key) {
|
||||
return fields.get(MovieProperty.valueOf(key.toString()));
|
||||
}
|
||||
|
||||
|
||||
public String get(MovieProperty key) {
|
||||
return fields.get(key);
|
||||
}
|
||||
|
||||
|
||||
public boolean isAdult() {
|
||||
return Boolean.valueOf(get(MovieProperty.adult));
|
||||
}
|
||||
|
||||
|
||||
public List<Locale> getSpokenLanguages() {
|
||||
try {
|
||||
List<Locale> locales = new ArrayList<Locale>();
|
||||
@ -346,17 +343,14 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getOriginalName() {
|
||||
return get(MovieProperty.original_title);
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return get(MovieProperty.title);
|
||||
}
|
||||
|
||||
|
||||
public Integer getId() {
|
||||
try {
|
||||
return new Integer(get(MovieProperty.id));
|
||||
@ -365,7 +359,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Integer getImdbId() {
|
||||
// e.g. tt0379786
|
||||
try {
|
||||
@ -375,7 +368,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public URL getHomepage() {
|
||||
try {
|
||||
return new URL(get(MovieProperty.homepage));
|
||||
@ -384,12 +376,10 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getOverview() {
|
||||
return get(MovieProperty.overview);
|
||||
}
|
||||
|
||||
|
||||
public Integer getVotes() {
|
||||
try {
|
||||
return new Integer(get(MovieProperty.vote_count));
|
||||
@ -398,7 +388,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Double getRating() {
|
||||
try {
|
||||
return new Double(get(MovieProperty.vote_average));
|
||||
@ -407,24 +396,20 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getTagline() {
|
||||
return get(MovieProperty.tagline);
|
||||
}
|
||||
|
||||
|
||||
public String getCertification() {
|
||||
// e.g. PG-13
|
||||
return get(MovieProperty.certification);
|
||||
}
|
||||
|
||||
|
||||
public String getCollection() {
|
||||
// e.g. Star Wars Collection
|
||||
return get(MovieProperty.collection);
|
||||
}
|
||||
|
||||
|
||||
public Date getReleased() {
|
||||
// e.g. 2005-09-30
|
||||
try {
|
||||
@ -434,22 +419,18 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getRuntime() {
|
||||
return get(MovieProperty.runtime);
|
||||
}
|
||||
|
||||
|
||||
public List<String> getGenres() {
|
||||
return unmodifiableList(asList(genres));
|
||||
}
|
||||
|
||||
|
||||
public List<Person> getPeople() {
|
||||
return unmodifiableList(asList(people));
|
||||
}
|
||||
|
||||
|
||||
public List<Person> getCast() {
|
||||
List<Person> actors = new ArrayList<Person>();
|
||||
for (Person person : people) {
|
||||
@ -460,7 +441,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return actors;
|
||||
}
|
||||
|
||||
|
||||
public String getDirector() {
|
||||
for (Person person : people) {
|
||||
if (person.isDirector())
|
||||
@ -469,7 +449,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public String getWriter() {
|
||||
for (Person person : people) {
|
||||
if (person.isWriter())
|
||||
@ -478,7 +457,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public List<String> getActors() {
|
||||
List<String> actors = new ArrayList<String>();
|
||||
for (Person actor : getCast()) {
|
||||
@ -487,7 +465,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
return actors;
|
||||
}
|
||||
|
||||
|
||||
public URL getPoster() {
|
||||
try {
|
||||
return new URL(get(MovieProperty.poster_path));
|
||||
@ -496,6 +473,9 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<Trailer> getTrailers() {
|
||||
return unmodifiableList(asList(trailers));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@ -503,7 +483,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Person implements Serializable {
|
||||
|
||||
public static enum PersonProperty {
|
||||
@ -512,17 +491,14 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
|
||||
protected Map<PersonProperty, String> fields;
|
||||
|
||||
|
||||
protected Person() {
|
||||
// used by serializer
|
||||
}
|
||||
|
||||
|
||||
public Person(Map<PersonProperty, String> fields) {
|
||||
this.fields = new EnumMap<PersonProperty, String>(fields);
|
||||
}
|
||||
|
||||
|
||||
public Person(String name, String character, String job) {
|
||||
fields = new EnumMap<PersonProperty, String>(PersonProperty.class);
|
||||
fields.put(PersonProperty.name, name);
|
||||
@ -530,54 +506,44 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
fields.put(PersonProperty.job, job);
|
||||
}
|
||||
|
||||
|
||||
public String get(Object key) {
|
||||
return fields.get(PersonProperty.valueOf(key.toString()));
|
||||
}
|
||||
|
||||
|
||||
public String get(PersonProperty key) {
|
||||
return fields.get(key);
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return get(PersonProperty.name);
|
||||
}
|
||||
|
||||
|
||||
public String getCharacter() {
|
||||
return get(PersonProperty.character);
|
||||
}
|
||||
|
||||
|
||||
public String getJob() {
|
||||
return get(PersonProperty.job);
|
||||
}
|
||||
|
||||
|
||||
public boolean isActor() {
|
||||
return getJob() == null;
|
||||
}
|
||||
|
||||
|
||||
public boolean isDirector() {
|
||||
return "Director".equals(getJob());
|
||||
}
|
||||
|
||||
|
||||
public boolean isWriter() {
|
||||
return "Writer".equals(getJob());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return fields.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Artwork {
|
||||
|
||||
private String category;
|
||||
@ -588,7 +554,6 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
|
||||
private URL url;
|
||||
|
||||
|
||||
public Artwork(String category, URL url, int width, int height, String language) {
|
||||
this.category = category;
|
||||
this.url = url;
|
||||
@ -597,36 +562,65 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
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;
|
||||
private String name;
|
||||
private Map<String, String> sources;
|
||||
|
||||
public Trailer(String type, String name, Map<String, String> sources) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.sources = sources;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Map<String, String> getSources() {
|
||||
return sources;
|
||||
}
|
||||
|
||||
public String getSource(String size) {
|
||||
return sources.containsKey(size) ? sources.get(size) : sources.values().iterator().next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s %s (%s)", name, sources.keySet(), type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user