From 53e5e48d5f6f55c2b0dc9f772eaafed5b6ea5f39 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 2 May 2014 08:00:43 +0000 Subject: [PATCH] * use y:2014 year filter when querying TheMovieDB if possible --- source/net/filebot/web/TMDbClient.java | 15 +++++++++++++++ test/net/filebot/web/TMDbClientTest.java | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index 35e9d353..1dd57469 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -26,6 +26,8 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.swing.Icon; @@ -63,6 +65,19 @@ public class TMDbClient implements MovieIdentificationService { @Override public List searchMovie(String query, Locale locale) throws IOException { + // query by name with year filter if possible + Matcher nameYear = Pattern.compile("(.+)\\b(19\\d{2}|20\\d{2})$").matcher(query); + if (nameYear.matches()) { + return searchMovie(nameYear.group(1).trim(), Integer.parseInt(nameYear.group(2)), locale); + } else { + return searchMovie(query, -1, locale); + } + } + + 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(); + JSONObject response = request("search/movie", singletonMap("query", query), locale, SEARCH_LIMIT); List result = new ArrayList(); diff --git a/test/net/filebot/web/TMDbClientTest.java b/test/net/filebot/web/TMDbClientTest.java index 489045e7..4f9608ed 100644 --- a/test/net/filebot/web/TMDbClientTest.java +++ b/test/net/filebot/web/TMDbClientTest.java @@ -10,6 +10,7 @@ import java.util.Locale; import net.filebot.web.TMDbClient.Artwork; import net.filebot.web.TMDbClient.MovieInfo; +import org.junit.Ignore; import org.junit.Test; public class TMDbClientTest { @@ -27,6 +28,17 @@ public class TMDbClientTest { assertEquals(16320, movie.getTmdbId()); } + @Test + public void searchByNameWithYear() throws Exception { + List result = tmdb.searchMovie("Up 2009", Locale.ENGLISH); + Movie movie = result.get(0); + + assertEquals("Up", movie.getName()); + assertEquals(2009, movie.getYear()); + assertEquals(-1, movie.getImdbId()); + assertEquals(14160, movie.getTmdbId()); + } + @Test public void searchByNameGermanResults() throws Exception { List result = tmdb.searchMovie("East of Eden", Locale.GERMAN); @@ -66,6 +78,7 @@ public class TMDbClientTest { assertEquals("http://image.tmdb.org/t/p/original/dXTeZELpoVMDOTTLnNoCpsCngwW.jpg", artwork.get(0).getUrl().toString()); } + @Ignore @Test public void floodLimit() throws Exception { for (Locale it : Locale.getAvailableLocales()) {