From 23df26fad4bef15a77e808e7ea7d5c97a9ef7eef Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 7 Mar 2019 18:07:34 +0700 Subject: [PATCH] Add net.filebot.TheMovieDB.url and net.filebot.TheMovieDB.adult system properties for advanced customization of TheMovieDB API access --- source/net/filebot/WebServices.java | 6 ++-- source/net/filebot/web/TMDbClient.java | 42 +++++++++++++++++--------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/source/net/filebot/WebServices.java b/source/net/filebot/WebServices.java index a809a59f..6f6fff50 100644 --- a/source/net/filebot/WebServices.java +++ b/source/net/filebot/WebServices.java @@ -53,7 +53,7 @@ public final class WebServices { // movie sources public static final OMDbClient OMDb = new OMDbClient(getApiKey("omdb")); - public static final TMDbClient TheMovieDB = new TMDbClientWithLocalSearch(getApiKey("themoviedb"), Boolean.parseBoolean(System.getProperty("net.filebot.WebServices.TheMovieDB.adult"))); + public static final TMDbClient TheMovieDB = new TMDbClientWithLocalSearch(getApiKey("themoviedb")); // episode sources public static final TVMazeClient TVmaze = new TVMazeClient(); @@ -131,8 +131,8 @@ public final class WebServices { public static class TMDbClientWithLocalSearch extends TMDbClient { - public TMDbClientWithLocalSearch(String apikey, boolean adult) { - super(apikey, adult); + public TMDbClientWithLocalSearch(String apikey) { + super(apikey); } // local TheMovieDB search index diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index 969154b7..317076d6 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -37,20 +37,35 @@ import net.filebot.ResourceManager; public class TMDbClient implements MovieIdentificationService, ArtworkProvider { + private static URL getEndpoint() { + try { + return new URL(System.getProperty("net.filebot.TheMovieDB.url", "https://api.themoviedb.org/3/")); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + private static boolean isAdultEnabled() { + return Boolean.parseBoolean(System.getProperty("net.filebot.TheMovieDB.adult")); + } + // X-RateLimit: 40 requests per 10 seconds => https://developers.themoviedb.org/3/getting-started/request-rate-limiting private static final FloodLimit REQUEST_LIMIT = new FloodLimit(35, 10, TimeUnit.SECONDS); - private final String host = "api.themoviedb.org"; - private final String version = "3"; + private final URL api; + private final String apikey; + private final boolean adult; - private String apikey; - private boolean adult; - - public TMDbClient(String apikey, boolean adult) { + public TMDbClient(URL api, String apikey, boolean adult) { + this.api = api; this.apikey = apikey; this.adult = adult; } + public TMDbClient(String apikey) { + this(getEndpoint(), apikey, isAdultEnabled()); + } + @Override public String getIdentifier() { return "TheMovieDB"; @@ -381,18 +396,17 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider { return json; } - protected URL getResource(String path, String language) throws Exception { - StringBuilder file = new StringBuilder(); - file.append('/').append(version); - file.append('/').append(path); - file.append(path.lastIndexOf('?') < 0 ? '?' : '&'); + protected URL getResource(String resource, String language) throws Exception { + StringBuilder path = new StringBuilder(); + path.append(resource); + path.append(resource.lastIndexOf('?') < 0 ? '?' : '&'); if (language != null) { - file.append("language=").append(language).append('&'); + path.append("language=").append(language).append('&'); } - file.append("api_key=").append(apikey); + path.append("api_key=").append(apikey); - return new URL("https", host, file.toString()); + return new URL(api, path.toString()); } protected String getLanguageCode(Locale locale) {