diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index d22740b2..967b2901 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -75,15 +75,18 @@ public class TMDbClient implements MovieIdentificationService { } public List searchMovie(String movieName, int movieYear, Locale locale) throws IOException { - // use y:2014 year filter if possible - String query = (movieYear > 0) ? String.format("%s y:%d", movieName, movieYear) : movieName.toString(); - // ignore queries that are too short to yield good results if (movieName.length() < 3 && !(movieName.length() > 1 && movieYear > 0)) { return emptyList(); } - JSONObject response = request("search/movie", singletonMap("query", query), locale, SEARCH_LIMIT); + Map param = new LinkedHashMap(2); + param.put("query", movieName); + if (movieYear > 0) { + param.put("year", movieYear); + } + + JSONObject response = request("search/movie", param, locale, SEARCH_LIMIT); List result = new ArrayList(); for (JSONObject it : jsonList(response.get("results"))) { @@ -132,7 +135,7 @@ public class TMDbClient implements MovieIdentificationService { result.add(new Movie(title, alternativeTitles.toArray(new String[0]), year, -1, id, locale)); } catch (Exception e) { // only print 'missing release date' warnings for matching movie titles - if (query.equalsIgnoreCase(title) || query.equalsIgnoreCase(originalTitle)) { + if (movieName.equalsIgnoreCase(title) || movieName.equalsIgnoreCase(originalTitle)) { Logger.getLogger(TMDbClient.class.getName()).log(Level.WARNING, String.format("Ignore movie [%s]: %s", title, e.getMessage())); } } @@ -156,7 +159,7 @@ public class TMDbClient implements MovieIdentificationService { String id = byIMDB ? String.format("tt%07d", imdbtmdbid) : String.valueOf(imdbtmdbid); try { - MovieInfo info = getMovieInfo(id, locale, false, false); + MovieInfo info = getMovieInfo(id, locale, false); return new Movie(info.getName(), new String[0], info.getReleased().getYear(), info.getImdbId(), info.getId(), locale); } catch (FileNotFoundException e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Movie not found: " + id); @@ -174,21 +177,21 @@ public class TMDbClient implements MovieIdentificationService { public MovieInfo getMovieInfo(Movie movie, Locale locale, boolean extendedInfo) throws IOException { if (movie.getTmdbId() >= 0) { - return getMovieInfo(String.valueOf(movie.getTmdbId()), locale, extendedInfo, extendedInfo); + return getMovieInfo(String.valueOf(movie.getTmdbId()), locale, extendedInfo); } else if (movie.getImdbId() >= 0) { - return getMovieInfo(String.format("tt%07d", movie.getImdbId()), locale, extendedInfo, extendedInfo); + return getMovieInfo(String.format("tt%07d", movie.getImdbId()), locale, extendedInfo); } else { for (Movie result : searchMovie(movie.getName(), locale)) { if (movie.getName().equalsIgnoreCase(result.getName()) && movie.getYear() == result.getYear()) { - return getMovieInfo(String.valueOf(result.getTmdbId()), locale, extendedInfo, extendedInfo); + return getMovieInfo(String.valueOf(result.getTmdbId()), locale, extendedInfo); } } } return null; } - public MovieInfo getMovieInfo(String id, Locale locale, boolean includeAlternativeTitles, boolean includeExtendedInfo) throws IOException { - JSONObject response = request("movie/" + id, null, locale, REQUEST_LIMIT); + public MovieInfo getMovieInfo(String id, Locale locale, boolean extendedInfo) throws IOException { + JSONObject response = request("movie/" + id, extendedInfo ? singletonMap("append_to_response", "alternative_titles,releases,casts,trailers") : null, locale, REQUEST_LIMIT); Map fields = new EnumMap(MovieProperty.class); for (MovieProperty key : MovieProperty.values()) { @@ -219,58 +222,49 @@ public class TMDbClient implements MovieIdentificationService { } List alternativeTitles = new ArrayList(); - if (includeAlternativeTitles) { - String countryCode = locale.getCountry().isEmpty() ? "US" : locale.getCountry(); - JSONObject titles = request("movie/" + fields.get(MovieProperty.id) + "/alternative_titles", null, null, REQUEST_LIMIT); - for (JSONObject it : jsonList(titles.get("titles"))) { - if (countryCode.equals(it.get("iso_3166_1"))) { - alternativeTitles.add((String) it.get("title")); - } + String countryCode = locale.getCountry().isEmpty() ? "US" : locale.getCountry(); + JSONObject titles = (JSONObject) response.get("alternative_titles"); + for (JSONObject it : jsonList(titles.get("titles"))) { + if (countryCode.equals(it.get("iso_3166_1"))) { + alternativeTitles.add((String) it.get("title")); } } - if (includeExtendedInfo) { - String countryCode = locale.getCountry().isEmpty() ? "US" : locale.getCountry(); - JSONObject releases = request("movie/" + fields.get(MovieProperty.id) + "/releases", null, null, REQUEST_LIMIT); - for (JSONObject it : jsonList(releases.get("countries"))) { - if (countryCode.equals(it.get("iso_3166_1"))) { - fields.put(MovieProperty.certification, (String) it.get("certification")); - } + JSONObject releases = (JSONObject) response.get("releases"); + for (JSONObject it : jsonList(releases.get("countries"))) { + if (countryCode.equals(it.get("iso_3166_1"))) { + fields.put(MovieProperty.certification, (String) it.get("certification")); } } List cast = new ArrayList(); - if (includeExtendedInfo) { - JSONObject castResponse = request("movie/" + fields.get(MovieProperty.id) + "/casts", null, null, REQUEST_LIMIT); - for (String section : new String[] { "cast", "crew" }) { - for (JSONObject it : jsonList(castResponse.get(section))) { - Map person = new EnumMap(PersonProperty.class); - for (PersonProperty key : PersonProperty.values()) { - Object value = it.get(key.name()); - if (value != null) { - person.put(key, value.toString()); - } + JSONObject castResponse = (JSONObject) response.get("casts"); + for (String section : new String[] { "cast", "crew" }) { + for (JSONObject it : jsonList(castResponse.get(section))) { + Map person = new EnumMap(PersonProperty.class); + for (PersonProperty key : PersonProperty.values()) { + Object value = it.get(key.name()); + if (value != null) { + person.put(key, value.toString()); } - cast.add(new Person(person)); } + cast.add(new Person(person)); } } List trailers = new ArrayList(); - if (includeExtendedInfo) { - 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 sources = new LinkedHashMap(); - 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()); + JSONObject trailerResponse = (JSONObject) response.get("trailers"); + for (String section : new String[] { "quicktime", "youtube" }) { + for (JSONObject it : jsonList(trailerResponse.get(section))) { + Map sources = new LinkedHashMap(); + if (it.containsKey("sources")) { + for (JSONObject s : jsonList(it.get("sources"))) { + sources.put(s.get("size").toString(), s.get("source").toString()); } - trailers.add(new Trailer(section, it.get("name").toString(), sources)); + } else { + sources.put(it.get("size").toString(), it.get("source").toString()); } + trailers.add(new Trailer(section, it.get("name").toString(), sources)); } } @@ -302,9 +296,9 @@ public class TMDbClient implements MovieIdentificationService { return artwork; } - public JSONObject request(String resource, Map parameters, Locale locale, final FloodLimit limit) throws IOException { + public JSONObject request(String resource, Map parameters, Locale locale, final FloodLimit limit) throws IOException { // default parameters - LinkedHashMap data = new LinkedHashMap(); + LinkedHashMap data = new LinkedHashMap(); if (parameters != null) { data.putAll(parameters); }