added moviedbBrowse in TheMovieDb
modified buildSearchUrl to handle Movie.browse specific url added constants in TheMovieDb made isValidString method private made apiSite and defaultLanguage private
This commit is contained in:
@@ -10,13 +10,14 @@
|
||||
* For any reuse or distribution, you must make clear to others the
|
||||
* license terms of this work.
|
||||
*/
|
||||
|
||||
package com.moviejukebox.themoviedb;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -43,17 +44,25 @@ import com.moviejukebox.themoviedb.tools.WebBrowser;
|
||||
public class TheMovieDb {
|
||||
|
||||
private String apiKey;
|
||||
private static String apiSite = "http://api.themoviedb.org/2.1/";
|
||||
private static String defaultLanguage = "en-US";
|
||||
private static Logger logger;
|
||||
private static LogFormatter tmdbFormatter = new LogFormatter();
|
||||
private static ConsoleHandler tmdbConsoleHandler = new ConsoleHandler();
|
||||
private static final String apiSite = "http://api.themoviedb.org/2.1/";
|
||||
private static final String defaultLanguage = "en-US";
|
||||
private static final String MOVIE_SEARCH = "Movie.search";
|
||||
private static final String MOVIE_BROWSE = "Movie.browse";
|
||||
private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup";
|
||||
private static final String MOVIE_GET_INFO = "Movie.getInfo";
|
||||
private static final String MOVIE_GET_IMAGES = "Movie.getImages";
|
||||
private static final String PERSON_GET_VERSION = "Person.getVersion";
|
||||
private static final String PERSON_GET_INFO = "Person.getInfo";
|
||||
private static final String PERSON_SEARCH = "Person.search";
|
||||
|
||||
public TheMovieDb(String apiKey) {
|
||||
setLogger(Logger.getLogger("TheMovieDB"));
|
||||
setApiKey(apiKey);
|
||||
}
|
||||
|
||||
|
||||
public TheMovieDb(String apiKey, Logger logger) {
|
||||
setLogger(logger);
|
||||
setApiKey(apiKey);
|
||||
@@ -65,7 +74,7 @@ public class TheMovieDb {
|
||||
WebBrowser.setProxyUsername(username);
|
||||
WebBrowser.setProxyPassword(password);
|
||||
}
|
||||
|
||||
|
||||
public void setTimeout(int webTimeoutConnect, int webTimeoutRead) {
|
||||
WebBrowser.setWebTimeoutConnect(webTimeoutConnect);
|
||||
WebBrowser.setWebTimeoutRead(webTimeoutRead);
|
||||
@@ -103,7 +112,13 @@ public class TheMovieDb {
|
||||
* @return The search URL
|
||||
*/
|
||||
private String buildSearchUrl(String prefix, String searchTerm, String language) {
|
||||
String searchUrl = apiSite + prefix + "/" + language + "/xml/" + apiKey + "/" + searchTerm;
|
||||
String searchUrl = apiSite + prefix + "/" + language + "/xml/" + apiKey;
|
||||
if (prefix.equals(MOVIE_BROWSE)) {
|
||||
searchUrl += "?";
|
||||
} else {
|
||||
searchUrl += "/";
|
||||
}
|
||||
searchUrl += searchTerm;
|
||||
logger.finest("Search URL: " + searchUrl);
|
||||
return searchUrl;
|
||||
}
|
||||
@@ -118,7 +133,7 @@ public class TheMovieDb {
|
||||
public List<MovieDB> moviedbSearch(String movieTitle, String language) {
|
||||
MovieDB movie = null;
|
||||
List<MovieDB> movieList = new ArrayList<MovieDB>();
|
||||
|
||||
|
||||
// If the title is null, then exit
|
||||
if (!isValidString(movieTitle)) {
|
||||
return movieList;
|
||||
@@ -127,13 +142,96 @@ public class TheMovieDb {
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Movie.search", URLEncoder.encode(movieTitle, "UTF-8"), language);
|
||||
String searchUrl = buildSearchUrl(MOVIE_SEARCH, URLEncoder.encode(movieTitle, "UTF-8"), language);
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
if (nlMovies == null) {
|
||||
return movieList;
|
||||
}
|
||||
|
||||
|
||||
for (int loop = 0; loop < nlMovies.getLength(); loop++) {
|
||||
Node nMovie = nlMovies.item(loop);
|
||||
if (nMovie.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element eMovie = (Element) nMovie;
|
||||
movie = DOMParser.parseMovieInfo(eMovie);
|
||||
if (movie != null) {
|
||||
movieList.add(movie);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception error) {
|
||||
logger.severe("TheMovieDb Error: " + error.getMessage());
|
||||
}
|
||||
return movieList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Browse the database using the default parameters.
|
||||
* http://api.themoviedb.org/2.1/methods/Movie.browse
|
||||
*
|
||||
* @param orderBy either <code>rating</code>,
|
||||
* <code>release</code> or <code>title</code>
|
||||
* @param order how results are ordered. Either <code>asc</code> or
|
||||
* <code>desc</code>
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return a list of MovieDB objects
|
||||
*/
|
||||
public List<MovieDB> moviedbBrowse(String orderBy, String order, String language) {
|
||||
return this.moviedbBrowse(orderBy, order, new HashMap<String, String>(), language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Browse the database using optional parameters.
|
||||
* http://api.themoviedb.org/2.1/methods/Movie.browse
|
||||
*
|
||||
* @param orderBy either <code>rating</code>,
|
||||
* <code>release</code> or <code>title</code>
|
||||
* @param order how results are ordered. Either <code>asc</code> or
|
||||
* <code>desc</code>
|
||||
* @param parameters a Map of optional parameters. See the complete list
|
||||
* in the url above.
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return a list of MovieDB objects
|
||||
*/
|
||||
public List<MovieDB> moviedbBrowse(String orderBy, String order,
|
||||
Map<String, String> parameters, String language) {
|
||||
|
||||
List<String> validParameters = new ArrayList<String>();
|
||||
validParameters.add("per_page");
|
||||
validParameters.add("page");
|
||||
validParameters.add("query");
|
||||
validParameters.add("min_votes");
|
||||
validParameters.add("rating_min");
|
||||
validParameters.add("rating_max");
|
||||
validParameters.add("genres");
|
||||
validParameters.add("genres_selector");
|
||||
validParameters.add("release_min");
|
||||
validParameters.add("release_max");
|
||||
validParameters.add("year");
|
||||
validParameters.add("certifications");
|
||||
validParameters.add("companies");
|
||||
validParameters.add("countries");
|
||||
|
||||
String url = "order_by=" + orderBy + "&order=" + order;
|
||||
for (String key : validParameters) {
|
||||
if (parameters.containsKey(key)) {
|
||||
url += "&" + key + "=" + parameters.get(key);
|
||||
}
|
||||
}
|
||||
logger.finest("Browse URL : " + url);
|
||||
|
||||
MovieDB movie = null;
|
||||
List<MovieDB> movieList = new ArrayList<MovieDB>();
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl(MOVIE_BROWSE, url, language);
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
if (nlMovies == null) {
|
||||
return movieList;
|
||||
}
|
||||
|
||||
for (int loop = 0; loop < nlMovies.getLength(); loop++) {
|
||||
Node nMovie = nlMovies.item(loop);
|
||||
if (nMovie.getNodeType() == Node.ELEMENT_NODE) {
|
||||
@@ -164,11 +262,11 @@ public class TheMovieDb {
|
||||
if (!isValidString(imdbID)) {
|
||||
return movie;
|
||||
}
|
||||
|
||||
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Movie.imdbLookup", imdbID, language);
|
||||
String searchUrl = buildSearchUrl(MOVIE_IMDB_LOOKUP, imdbID, language);
|
||||
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
@@ -215,25 +313,26 @@ public class TheMovieDb {
|
||||
*/
|
||||
public MovieDB moviedbGetInfo(String tmdbID, MovieDB movie, String language) {
|
||||
// If the tmdbID is invalid, then exit
|
||||
if (!isValidString(tmdbID))
|
||||
if (!isValidString(tmdbID)) {
|
||||
return movie;
|
||||
|
||||
}
|
||||
|
||||
Document doc = null;
|
||||
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Movie.getInfo", tmdbID, language);
|
||||
|
||||
String searchUrl = buildSearchUrl(MOVIE_GET_INFO, tmdbID, language);
|
||||
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
if (doc == null && !language.equalsIgnoreCase(defaultLanguage)) {
|
||||
logger.fine("Trying to get the default version");
|
||||
Thread.dumpStack();
|
||||
searchUrl = buildSearchUrl("Movie.getInfo", tmdbID, defaultLanguage);
|
||||
searchUrl = buildSearchUrl(MOVIE_GET_INFO, tmdbID, defaultLanguage);
|
||||
}
|
||||
|
||||
|
||||
if (doc == null) {
|
||||
return movie;
|
||||
}
|
||||
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
if (nlMovies == null) {
|
||||
return movie;
|
||||
@@ -257,7 +356,7 @@ public class TheMovieDb {
|
||||
movie = moviedbGetInfo(searchTerm, movie, language);
|
||||
return movie;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all the image information from TheMovieDb.
|
||||
* @param searchTerm Can be either the IMDb ID or TMDb ID
|
||||
@@ -267,14 +366,15 @@ public class TheMovieDb {
|
||||
*/
|
||||
public MovieDB moviedbGetImages(String searchTerm, MovieDB movie, String language) {
|
||||
// If the searchTerm is null, then exit
|
||||
if (isValidString(searchTerm))
|
||||
if (isValidString(searchTerm)) {
|
||||
return movie;
|
||||
|
||||
}
|
||||
|
||||
Document doc = null;
|
||||
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Movie.getImages", searchTerm, language);
|
||||
|
||||
String searchUrl = buildSearchUrl(MOVIE_GET_IMAGES, searchTerm, language);
|
||||
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
if (nlMovies == null) {
|
||||
@@ -311,18 +411,18 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
Document doc = null;
|
||||
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Person.search", personName, language);
|
||||
String searchUrl = buildSearchUrl(PERSON_SEARCH, personName, language);
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
person = DOMParser.parsePersonInfo(doc);
|
||||
} catch (Exception error) {
|
||||
logger.severe("ERROR: " + error.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Person.getInfo method is used to retrieve the full filmography, known movies,
|
||||
* images and things like birthplace for a specific person in the TMDb database.
|
||||
@@ -336,20 +436,20 @@ public class TheMovieDb {
|
||||
if (!isValidString(personID)) {
|
||||
return person;
|
||||
}
|
||||
|
||||
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Person.getInfo", personID, language);
|
||||
String searchUrl = buildSearchUrl(PERSON_GET_INFO, personID, language);
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
person = DOMParser.parsePersonInfo(doc);
|
||||
} catch (Exception error) {
|
||||
logger.severe("ERROR: " + error.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Person.getVersion method is used to retrieve the last modified time along with
|
||||
* the current version number of the called object(s). This is useful if you've already
|
||||
@@ -368,34 +468,34 @@ public class TheMovieDb {
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
String searchUrl = buildSearchUrl("Person.getVersion", personID, language);
|
||||
String searchUrl = buildSearchUrl(PERSON_GET_VERSION, personID, language);
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
person = DOMParser.parsePersonGetVersion(doc);
|
||||
} catch (Exception error) {
|
||||
logger.severe("ERROR: " + error.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check the string passed to see if it contains a value.
|
||||
* @param testString The string to test
|
||||
* @return False if the string is empty, null or UNKNOWN, True otherwise
|
||||
*/
|
||||
public static boolean isValidString(String testString) {
|
||||
private static boolean isValidString(String testString) {
|
||||
if (testString == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (testString.equalsIgnoreCase(MovieDB.UNKNOWN)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (testString.trim().equals("")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -410,16 +510,16 @@ public class TheMovieDb {
|
||||
if (movieList == null || movieList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
for (MovieDB moviedb : movieList) {
|
||||
if (compareMovies(moviedb, title, year)) {
|
||||
return moviedb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare the MovieDB object with a title & year
|
||||
* @param moviedb The moviedb object to compare too
|
||||
@@ -428,6 +528,10 @@ public class TheMovieDb {
|
||||
* @return True if there is a match, False otherwise.
|
||||
*/
|
||||
public static boolean compareMovies(MovieDB moviedb, String title, String year) {
|
||||
if (moviedb == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isValidString(title)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user