Added getTranslations method
This commit is contained in:
@@ -25,6 +25,7 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.moviejukebox.themoviedb.model.Category;
|
||||
import com.moviejukebox.themoviedb.model.Language;
|
||||
import com.moviejukebox.themoviedb.model.MovieDB;
|
||||
import com.moviejukebox.themoviedb.model.Person;
|
||||
import com.moviejukebox.themoviedb.tools.MovieDbParser;
|
||||
@@ -41,25 +42,140 @@ import com.moviejukebox.themoviedb.tools.WebBrowser;
|
||||
*/
|
||||
public class TheMovieDb {
|
||||
|
||||
/**
|
||||
* Compare the MovieDB object with a title & year
|
||||
* @param moviedb The moviedb object to compare too
|
||||
* @param title The title of the movie to compare
|
||||
* @param year The year of the movie to compare
|
||||
* @return True if there is a match, False otherwise.
|
||||
*/
|
||||
public static boolean compareMovies(MovieDB moviedb, String title, String year) {
|
||||
if ((moviedb == null) || (!isValidString(title))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isValidString(year)) {
|
||||
if (isValidString(moviedb.getReleaseDate())) {
|
||||
// Compare with year
|
||||
String movieYear = moviedb.getReleaseDate().substring(0, 4);
|
||||
if (movieYear.equals(year)) {
|
||||
if (moviedb.getOriginalName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (moviedb.getTitle().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try matching the alternative name too
|
||||
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Compare without year
|
||||
if (moviedb.getOriginalName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (moviedb.getTitle().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try matching the alternative name too
|
||||
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Search a list of movies and return the one that matches the title & year
|
||||
* @param movieList The list of movies to search
|
||||
* @param title The title to search for
|
||||
* @param year The year of the title to search for
|
||||
* @return The matching movie
|
||||
*/
|
||||
public static MovieDB findMovie(Collection<MovieDB> movieList, String title, String year) {
|
||||
if ((movieList == null) || (movieList.isEmpty()) || (!isValidString(title))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (MovieDB moviedb : movieList) {
|
||||
if (compareMovies(moviedb, title, year)) {
|
||||
return moviedb;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private static boolean isValidString(String testString) {
|
||||
if ((testString == null)
|
||||
|| (testString.trim().equals(""))
|
||||
|| (testString.equalsIgnoreCase(MovieDB.UNKNOWN))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private String apiKey;
|
||||
private static Logger logger = null;
|
||||
private static LogFormatter tmdbFormatter = new LogFormatter();
|
||||
|
||||
/*
|
||||
* API Methods
|
||||
* http://api.themoviedb.org/2.1
|
||||
* Note: This is currently a read-only interface and as such, no write methods exist.
|
||||
*/
|
||||
|
||||
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 GENRES_GET_LIST = "Genres.getList";
|
||||
private static final String MOVIE_BROWSE = "Movie.browse";
|
||||
private static final String MOVIE_GET_INFO = "Movie.getInfo";
|
||||
private static final String MOVIE_GET_IMAGES = "Movie.getImages";
|
||||
private static final String MOVIE_GET_LATEST = "Movie.getLatest";
|
||||
private static final String MOVIE_GET_VERSION = "Movie.getVersion";
|
||||
private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup";
|
||||
private static final String MOVIE_SEARCH = "Movie.search";
|
||||
private static final String PERSON_GET_INFO = "Person.getInfo";
|
||||
private static final String PERSON_GET_LATEST = "Person.getLatest";
|
||||
private static final String PERSON_GET_VERSION = "Person.getVersion";
|
||||
private static final String PERSON_SEARCH = "Person.search";
|
||||
/*
|
||||
* Media
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private static final String MEDIA_GET_INFO = "Media.getInfo";
|
||||
|
||||
/*
|
||||
* Movies
|
||||
*/
|
||||
private static final String MOVIE_BROWSE = "Movie.browse";
|
||||
private static final String MOVIE_GET_IMAGES = "Movie.getImages";
|
||||
private static final String MOVIE_GET_INFO = "Movie.getInfo";
|
||||
private static final String MOVIE_GET_LATEST = "Movie.getLatest";
|
||||
private static final String MOVIE_GET_TRANSLATIONS = "Movie.getTranslations";
|
||||
private static final String MOVIE_GET_VERSION = "Movie.getVersion";
|
||||
private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup";
|
||||
private static final String MOVIE_SEARCH = "Movie.search";
|
||||
|
||||
/*
|
||||
* People
|
||||
*/
|
||||
private static final String PERSON_GET_INFO = "Person.getInfo";
|
||||
private static final String PERSON_GET_LATEST = "Person.getLatest";
|
||||
private static final String PERSON_GET_VERSION = "Person.getVersion";
|
||||
private static final String PERSON_SEARCH = "Person.search";
|
||||
|
||||
/*
|
||||
* Misc
|
||||
*/
|
||||
private static final String GENRES_GET_LIST = "Genres.getList";
|
||||
|
||||
public static Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with default logger.
|
||||
* @param apiKey
|
||||
*/
|
||||
public TheMovieDb(String apiKey) {
|
||||
setLogger(Logger.getLogger("TheMovieDB"));
|
||||
if (!isValidString(apiKey)) {
|
||||
@@ -77,44 +193,55 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set proxy parameters.
|
||||
* @param host proxy host URL
|
||||
* @param port proxy port
|
||||
* @param username proxy username
|
||||
* @param password proxy password
|
||||
* Build comma separated ids for Movie.getLatest and Movie.getVersion.
|
||||
* @param ids a List of ids
|
||||
* @return
|
||||
*/
|
||||
public void setProxy(String host, String port, String username, String password) {
|
||||
WebBrowser.setProxyHost(host);
|
||||
WebBrowser.setProxyPort(port);
|
||||
WebBrowser.setProxyUsername(username);
|
||||
WebBrowser.setProxyPassword(password);
|
||||
private String buildIds(List<String> ids) {
|
||||
String s = "";
|
||||
for (int i = 0; i < ids.size(); i++) {
|
||||
if (i == 0) {
|
||||
s += ids.get(i);
|
||||
continue;
|
||||
}
|
||||
s += "," + ids.get(i);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set web browser timeout.
|
||||
* @param webTimeoutConnect
|
||||
* @param webTimeoutRead
|
||||
* Build the URL that is used to get the XML from TMDb.
|
||||
*
|
||||
* @param prefix The search prefix before the movie title
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @param searchTerm The search key to use, e.g. movie title or IMDb ID
|
||||
* @return The search URL
|
||||
*/
|
||||
public void setTimeout(int webTimeoutConnect, int webTimeoutRead) {
|
||||
WebBrowser.setWebTimeoutConnect(webTimeoutConnect);
|
||||
WebBrowser.setWebTimeoutRead(webTimeoutRead);
|
||||
}
|
||||
private String buildUrl(String prefix, String searchTerm, String language) {
|
||||
String url = apiSite + prefix + "/" + language + "/xml/" + apiKey;
|
||||
|
||||
public static Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public void setLogger(Logger logger) {
|
||||
if (logger == null) {
|
||||
return;
|
||||
if (!isValidString(searchTerm)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
TheMovieDb.logger = logger;
|
||||
tmdbConsoleHandler.setFormatter(tmdbFormatter);
|
||||
tmdbConsoleHandler.setLevel(Level.FINE);
|
||||
logger.addHandler(tmdbConsoleHandler);
|
||||
logger.setUseParentHandlers(false);
|
||||
logger.setLevel(Level.ALL);
|
||||
String encodedSearchTerm;
|
||||
|
||||
try {
|
||||
encodedSearchTerm = URLEncoder.encode(searchTerm, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
encodedSearchTerm = searchTerm;
|
||||
}
|
||||
|
||||
if (prefix.equals(MOVIE_BROWSE)) {
|
||||
url += "?";
|
||||
} else {
|
||||
url += "/";
|
||||
}
|
||||
|
||||
url += encodedSearchTerm;
|
||||
|
||||
logger.finest("Search URL: " + url);
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,12 +253,12 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the TMDb API key.
|
||||
* @param apiKey a valid TMDb API key.
|
||||
* Retrieve a list of valid genres within TMDb.
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
tmdbFormatter.addApiKey(apiKey);
|
||||
public List<Category> getCategories(String language) {
|
||||
return MovieDbParser.parseCategories(this.buildUrl(GENRES_GET_LIST, "", language));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,36 +269,8 @@ public class TheMovieDb {
|
||||
return defaultLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the database using the movie title passed
|
||||
*
|
||||
* @param movieTitle The title to search for
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @return A movie bean with the data extracted
|
||||
*/
|
||||
public List<MovieDB> moviedbSearch(String movieTitle, String language) {
|
||||
// If the title is null, then exit
|
||||
if (!isValidString(movieTitle)) {
|
||||
return new ArrayList<MovieDB>();
|
||||
}
|
||||
|
||||
String searchUrl = buildUrl(MOVIE_SEARCH, movieTitle, language);
|
||||
return MovieDbParser.parseMovies(searchUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
public List<Language> getTranslations(String movieId, String language) {
|
||||
return MovieDbParser.parseLanguages(this.buildUrl(MOVIE_GET_TRANSLATIONS, movieId, language));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,35 +326,51 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the database using the IMDb reference
|
||||
*
|
||||
* @param imdbID IMDb reference, must include the "tt" at the start
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @return A movie bean with the data extracted
|
||||
* 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 MovieDB moviedbImdbLookup(String imdbID, String language) {
|
||||
MovieDB movie = new MovieDB();
|
||||
public List<MovieDB> moviedbBrowse(String orderBy, String order, String language) {
|
||||
return this.moviedbBrowse(orderBy, order, new HashMap<String, String>(), language);
|
||||
}
|
||||
|
||||
// If the imdbID is null, then exit
|
||||
if (!isValidString(imdbID)) {
|
||||
/**
|
||||
* The Movie.getImages method is used to retrieve all of the backdrops and
|
||||
* posters for a particular movie. This is useful to scan for updates, or
|
||||
* new images if that's all you're after.
|
||||
* @param movieId the TMDb or IMDB ID (starting with tt) of the movie you
|
||||
* are searching for.
|
||||
* @param movie a MovieDB object
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public MovieDB moviedbGetImages(String movieId, MovieDB movie, String language) {
|
||||
// If the searchTerm is null, then exit
|
||||
if (!isValidString(movieId)) {
|
||||
return movie;
|
||||
}
|
||||
|
||||
String searchUrl = buildUrl(MOVIE_IMDB_LOOKUP, imdbID, language);
|
||||
String searchUrl = buildUrl(MOVIE_GET_IMAGES, movieId, language);
|
||||
return MovieDbParser.parseMovie(searchUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes a null MovieDB object to the full function
|
||||
*
|
||||
* @param tmdbID TheMovieDB ID of the movie to get the information for
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @return A movie bean with all of the information
|
||||
* The Movie.getImages method is used to retrieve all of the backdrops and
|
||||
* posters for a particular movie. This is useful to scan for updates, or
|
||||
* new images if that's all you're after.
|
||||
* @param movieId the TMDb or IMDB ID (starting with tt) of the movie you
|
||||
* are searching for.
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public MovieDB moviedbGetInfo(String tmdbID, String language) {
|
||||
MovieDB movie = null;
|
||||
movie = moviedbGetInfo(tmdbID, movie, language);
|
||||
return movie;
|
||||
public MovieDB moviedbGetImages(String movieId, String language) {
|
||||
return moviedbGetImages(movieId, new MovieDB(), language);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -287,6 +402,19 @@ public class TheMovieDb {
|
||||
return movie;
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes a null MovieDB object to the full function
|
||||
*
|
||||
* @param tmdbID TheMovieDB ID of the movie to get the information for
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @return A movie bean with all of the information
|
||||
*/
|
||||
public MovieDB moviedbGetInfo(String tmdbID, String language) {
|
||||
MovieDB movie = null;
|
||||
movie = moviedbGetInfo(tmdbID, movie, language);
|
||||
return movie;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Movie.getLatest method is a simple method. It returns the ID of the
|
||||
* last movie created in the database. This is useful if you are scanning
|
||||
@@ -300,25 +428,6 @@ public class TheMovieDb {
|
||||
return MovieDbParser.parseLatestMovie(buildUrl(MOVIE_GET_LATEST, "", language));
|
||||
}
|
||||
|
||||
/**
|
||||
* The Movie.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 called the object sometime in the past and
|
||||
* simply want to do a quick check for updates. <br/>
|
||||
* The MovieDB object returned only has its title, TMDb id, IMDB id,
|
||||
* version and last modified date initialized.
|
||||
* @param movieId the TMDb ID or IMDB ID of the movie
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public MovieDB moviedbGetVersion(String movieId, String language) {
|
||||
List<MovieDB> movies = this.moviedbGetVersion(Arrays.asList(movieId), language);
|
||||
if (movies.isEmpty()) {
|
||||
return new MovieDB();
|
||||
}
|
||||
return movies.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Movie.getVersion method is used to retrieve the last modified time
|
||||
* along with the current version number of the called object(s). This is
|
||||
@@ -346,53 +455,58 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
/**
|
||||
* The Movie.getImages method is used to retrieve all of the backdrops and
|
||||
* posters for a particular movie. This is useful to scan for updates, or
|
||||
* new images if that's all you're after.
|
||||
* @param movieId the TMDb or IMDB ID (starting with tt) of the movie you
|
||||
* are searching for.
|
||||
* The Movie.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 called the object sometime in the past and
|
||||
* simply want to do a quick check for updates. <br/>
|
||||
* The MovieDB object returned only has its title, TMDb id, IMDB id,
|
||||
* version and last modified date initialized.
|
||||
* @param movieId the TMDb ID or IMDB ID of the movie
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public MovieDB moviedbGetImages(String movieId, String language) {
|
||||
return moviedbGetImages(movieId, new MovieDB(), language);
|
||||
public MovieDB moviedbGetVersion(String movieId, String language) {
|
||||
List<MovieDB> movies = this.moviedbGetVersion(Arrays.asList(movieId), language);
|
||||
if (movies.isEmpty()) {
|
||||
return new MovieDB();
|
||||
}
|
||||
return movies.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Movie.getImages method is used to retrieve all of the backdrops and
|
||||
* posters for a particular movie. This is useful to scan for updates, or
|
||||
* new images if that's all you're after.
|
||||
* @param movieId the TMDb or IMDB ID (starting with tt) of the movie you
|
||||
* are searching for.
|
||||
* @param movie a MovieDB object
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
* Searches the database using the IMDb reference
|
||||
*
|
||||
* @param imdbID IMDb reference, must include the "tt" at the start
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @return A movie bean with the data extracted
|
||||
*/
|
||||
public MovieDB moviedbGetImages(String movieId, MovieDB movie, String language) {
|
||||
// If the searchTerm is null, then exit
|
||||
if (!isValidString(movieId)) {
|
||||
public MovieDB moviedbImdbLookup(String imdbID, String language) {
|
||||
MovieDB movie = new MovieDB();
|
||||
|
||||
// If the imdbID is null, then exit
|
||||
if (!isValidString(imdbID)) {
|
||||
return movie;
|
||||
}
|
||||
|
||||
String searchUrl = buildUrl(MOVIE_GET_IMAGES, movieId, language);
|
||||
String searchUrl = buildUrl(MOVIE_IMDB_LOOKUP, imdbID, language);
|
||||
return MovieDbParser.parseMovie(searchUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Person.search method is used to search for an actor, actress or production member.
|
||||
* http://api.themoviedb.org/2.1/methods/Person.search
|
||||
* Searches the database using the movie title passed
|
||||
*
|
||||
* @param personName
|
||||
* @param language
|
||||
* @return
|
||||
* @param movieTitle The title to search for
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @return A movie bean with the data extracted
|
||||
*/
|
||||
public Person personSearch(String personName, String language) {
|
||||
if (!isValidString(personName)) {
|
||||
return new Person();
|
||||
public List<MovieDB> moviedbSearch(String movieTitle, String language) {
|
||||
// If the title is null, then exit
|
||||
if (!isValidString(movieTitle)) {
|
||||
return new ArrayList<MovieDB>();
|
||||
}
|
||||
|
||||
String searchUrl = buildUrl(PERSON_SEARCH, personName, language);
|
||||
return MovieDbParser.parsePersonInfo(searchUrl);
|
||||
String searchUrl = buildUrl(MOVIE_SEARCH, movieTitle, language);
|
||||
return MovieDbParser.parseMovies(searchUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -423,6 +537,25 @@ public class TheMovieDb {
|
||||
return MovieDbParser.parseLatestPerson(buildUrl(PERSON_GET_LATEST, "", language));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 called the object sometime in the past and
|
||||
* simply want to do a quick check for updates.
|
||||
* @param personIDs one or multiple Person TMDb ids
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public List<Person> personGetVersion(List<String> personIDs, String language) {
|
||||
if ((personIDs == null) || (personIDs.isEmpty())) {
|
||||
logger.warning("There are no Person ids!");
|
||||
return new ArrayList<Person>();
|
||||
}
|
||||
|
||||
String searchUrl = buildUrl(PERSON_GET_VERSION, this.buildIds(personIDs), language);
|
||||
return MovieDbParser.parsePersonGetVersion(searchUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -448,166 +581,65 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 called the object sometime in the past and
|
||||
* simply want to do a quick check for updates.
|
||||
* @param personIDs one or multiple Person TMDb ids
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* The Person.search method is used to search for an actor, actress or production member.
|
||||
* http://api.themoviedb.org/2.1/methods/Person.search
|
||||
*
|
||||
* @param personName
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public List<Person> personGetVersion(List<String> personIDs, String language) {
|
||||
if ((personIDs == null) || (personIDs.isEmpty())) {
|
||||
logger.warning("There are no Person ids!");
|
||||
return new ArrayList<Person>();
|
||||
public Person personSearch(String personName, String language) {
|
||||
if (!isValidString(personName)) {
|
||||
return new Person();
|
||||
}
|
||||
|
||||
String searchUrl = buildUrl(PERSON_GET_VERSION, this.buildIds(personIDs), language);
|
||||
return MovieDbParser.parsePersonGetVersion(searchUrl);
|
||||
String searchUrl = buildUrl(PERSON_SEARCH, personName, language);
|
||||
return MovieDbParser.parsePersonInfo(searchUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of valid genres within TMDb.
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
* Set the TMDb API key.
|
||||
* @param apiKey a valid TMDb API key.
|
||||
*/
|
||||
public List<Category> getCategories(String language) {
|
||||
return MovieDbParser.parseCategories(this.buildUrl(GENRES_GET_LIST, "", language));
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
tmdbFormatter.addApiKey(apiKey);
|
||||
}
|
||||
|
||||
public void setLogger(Logger logger) {
|
||||
if (logger == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
TheMovieDb.logger = logger;
|
||||
tmdbConsoleHandler.setFormatter(tmdbFormatter);
|
||||
tmdbConsoleHandler.setLevel(Level.FINE);
|
||||
logger.addHandler(tmdbConsoleHandler);
|
||||
logger.setUseParentHandlers(false);
|
||||
logger.setLevel(Level.ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search a list of movies and return the one that matches the title & year
|
||||
* @param movieList The list of movies to search
|
||||
* @param title The title to search for
|
||||
* @param year The year of the title to search for
|
||||
* @return The matching movie
|
||||
* Set proxy parameters.
|
||||
* @param host proxy host URL
|
||||
* @param port proxy port
|
||||
* @param username proxy username
|
||||
* @param password proxy password
|
||||
*/
|
||||
public static MovieDB findMovie(Collection<MovieDB> movieList, String title, String year) {
|
||||
if ((movieList == null) || (movieList.isEmpty()) || (!isValidString(title))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (MovieDB moviedb : movieList) {
|
||||
if (compareMovies(moviedb, title, year)) {
|
||||
return moviedb;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
public void setProxy(String host, String port, String username, String password) {
|
||||
WebBrowser.setProxyHost(host);
|
||||
WebBrowser.setProxyPort(port);
|
||||
WebBrowser.setProxyUsername(username);
|
||||
WebBrowser.setProxyPassword(password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the MovieDB object with a title & year
|
||||
* @param moviedb The moviedb object to compare too
|
||||
* @param title The title of the movie to compare
|
||||
* @param year The year of the movie to compare
|
||||
* @return True if there is a match, False otherwise.
|
||||
* Set web browser timeout.
|
||||
* @param webTimeoutConnect
|
||||
* @param webTimeoutRead
|
||||
*/
|
||||
public static boolean compareMovies(MovieDB moviedb, String title, String year) {
|
||||
if ((moviedb == null) || (!isValidString(title))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isValidString(year)) {
|
||||
if (isValidString(moviedb.getReleaseDate())) {
|
||||
// Compare with year
|
||||
String movieYear = moviedb.getReleaseDate().substring(0, 4);
|
||||
if (movieYear.equals(year)) {
|
||||
if (moviedb.getOriginalName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (moviedb.getTitle().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try matching the alternative name too
|
||||
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Compare without year
|
||||
if (moviedb.getOriginalName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (moviedb.getTitle().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try matching the alternative name too
|
||||
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the URL that is used to get the XML from TMDb.
|
||||
*
|
||||
* @param prefix The search prefix before the movie title
|
||||
* @param language The two digit language code. E.g. en=English
|
||||
* @param searchTerm The search key to use, e.g. movie title or IMDb ID
|
||||
* @return The search URL
|
||||
*/
|
||||
private String buildUrl(String prefix, String searchTerm, String language) {
|
||||
String url = apiSite + prefix + "/" + language + "/xml/" + apiKey;
|
||||
|
||||
if (!isValidString(searchTerm)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
String encodedSearchTerm;
|
||||
|
||||
try {
|
||||
encodedSearchTerm = URLEncoder.encode(searchTerm, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
encodedSearchTerm = searchTerm;
|
||||
}
|
||||
|
||||
if (prefix.equals(MOVIE_BROWSE)) {
|
||||
url += "?";
|
||||
} else {
|
||||
url += "/";
|
||||
}
|
||||
|
||||
url += encodedSearchTerm;
|
||||
|
||||
logger.finest("Search URL: " + url);
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build comma separated ids for Movie.getLatest and Movie.getVersion.
|
||||
* @param ids a List of ids
|
||||
* @return
|
||||
*/
|
||||
private String buildIds(List<String> ids) {
|
||||
String s = "";
|
||||
for (int i = 0; i < ids.size(); i++) {
|
||||
if (i == 0) {
|
||||
s += ids.get(i);
|
||||
continue;
|
||||
}
|
||||
s += "," + ids.get(i);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private static boolean isValidString(String testString) {
|
||||
if ((testString == null)
|
||||
|| (testString.trim().equals(""))
|
||||
|| (testString.equalsIgnoreCase(MovieDB.UNKNOWN))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
public void setTimeout(int webTimeoutConnect, int webTimeoutRead) {
|
||||
WebBrowser.setWebTimeoutConnect(webTimeoutConnect);
|
||||
WebBrowser.setWebTimeoutRead(webTimeoutRead);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
package com.moviejukebox.themoviedb.model;
|
||||
|
||||
/**
|
||||
* Category from the MovieDB.org
|
||||
* Category from TheMovieDB.org
|
||||
*
|
||||
* @author Stuart.Boston
|
||||
*
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2004-2010 YAMJ Members
|
||||
* http://code.google.com/p/moviejukebox/people/list
|
||||
*
|
||||
* Web: http://code.google.com/p/moviejukebox/
|
||||
*
|
||||
* This software is licensed under a Creative Commons License
|
||||
* See this page: http://code.google.com/p/moviejukebox/wiki/License
|
||||
*
|
||||
* For any reuse or distribution, you must make clear to others the
|
||||
* license terms of this work.
|
||||
*/
|
||||
package com.moviejukebox.themoviedb.model;
|
||||
|
||||
/**
|
||||
* Language from TheMovieDB.org
|
||||
* @author stuart.boston
|
||||
*
|
||||
*/
|
||||
public class Language {
|
||||
|
||||
private static final String UNKNOWN = MovieDB.UNKNOWN;
|
||||
|
||||
private String isoCode = UNKNOWN; // The iso 639.1 Language code
|
||||
private String englishName = UNKNOWN;
|
||||
private String nativeName = UNKNOWN;
|
||||
|
||||
public Language() {
|
||||
this.isoCode = UNKNOWN;
|
||||
this.englishName = UNKNOWN;
|
||||
this.nativeName = UNKNOWN;
|
||||
}
|
||||
|
||||
public Language(String isoCode, String englishName, String nativeName) {
|
||||
this.isoCode = isoCode;
|
||||
this.englishName = englishName;
|
||||
this.nativeName = nativeName;
|
||||
}
|
||||
|
||||
public String getEnglishName() {
|
||||
return englishName;
|
||||
}
|
||||
|
||||
public String getIsoCode() {
|
||||
return isoCode;
|
||||
}
|
||||
|
||||
public String getNativeName() {
|
||||
return nativeName;
|
||||
}
|
||||
|
||||
public void setEnglishName(String englishName) {
|
||||
this.englishName = englishName;
|
||||
}
|
||||
|
||||
public void setIsoCode(String isoCode) {
|
||||
this.isoCode = isoCode;
|
||||
}
|
||||
|
||||
public void setNativeName(String nativeName) {
|
||||
this.nativeName = nativeName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import com.moviejukebox.themoviedb.model.Artwork;
|
||||
import com.moviejukebox.themoviedb.model.Category;
|
||||
import com.moviejukebox.themoviedb.model.Country;
|
||||
import com.moviejukebox.themoviedb.model.Filmography;
|
||||
import com.moviejukebox.themoviedb.model.Language;
|
||||
import com.moviejukebox.themoviedb.model.MovieDB;
|
||||
import com.moviejukebox.themoviedb.model.Person;
|
||||
import com.moviejukebox.themoviedb.model.Studio;
|
||||
@@ -35,46 +36,145 @@ public class MovieDbParser {
|
||||
static Logger logger = TheMovieDb.getLogger();
|
||||
|
||||
/**
|
||||
* Returns a list of MovieDB object parsed from the DOM Document
|
||||
* even if there is only one movie
|
||||
* @param doc DOM Document
|
||||
* Retrieve a list of valid genres within TMDb.
|
||||
* @param doc a DOM document
|
||||
* @return
|
||||
*/
|
||||
public static List<MovieDB> parseMovies(String searchUrl) {
|
||||
List<MovieDB> movies = new ArrayList<MovieDB>();
|
||||
public static List<Category> parseCategories(String searchUrl) {
|
||||
Document doc = null;
|
||||
List<Category> categories = new ArrayList<Category>();
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
} catch (Exception error) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
NodeList genres = doc.getElementsByTagName("genre");
|
||||
if ((genres == null) || genres.getLength() == 0) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
for (int i = 0; i < genres.getLength(); i++) {
|
||||
Node node = genres.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
Category category = new Category();
|
||||
category.setName(element.getAttribute("name"));
|
||||
category.setId(DOMHelper.getValueFromElement(element, "id"));
|
||||
category.setUrl(DOMHelper.getValueFromElement(element, "url"));
|
||||
categories.add(category);
|
||||
}
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
public static List<Language> parseLanguages(String url) {
|
||||
List<Language> languages = new ArrayList<Language>();
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(url);
|
||||
} catch (Exception e) {
|
||||
logger.severe("Movie.getTranslations error: " + e.getMessage());
|
||||
return languages;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return languages;
|
||||
}
|
||||
|
||||
NodeList nlLanguages = doc.getElementsByTagName("language");
|
||||
|
||||
if ((nlLanguages == null) || nlLanguages.getLength() == 0) {
|
||||
return languages;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nlLanguages.getLength(); i++) {
|
||||
Node node = nlLanguages.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
languages.add(parseSimpleLanguage(element));
|
||||
}
|
||||
}
|
||||
|
||||
return languages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a DOM document and returns the latest Movie.
|
||||
* This method is used for Movie.getLatest and Movie.getVersion where only
|
||||
* a few fields are initialized.
|
||||
* @param doc
|
||||
* @return
|
||||
*/
|
||||
public static MovieDB parseLatestMovie(String searchUrl) {
|
||||
MovieDB movie = null;
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
} catch (Exception error) {
|
||||
logger.severe("TheMovieDb Error: " + error.getMessage());
|
||||
return movies;
|
||||
logger.severe("GetLatest error: " + error.getMessage());
|
||||
return movie;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return movies;
|
||||
return movie;
|
||||
}
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return movies;
|
||||
return movie;
|
||||
}
|
||||
|
||||
MovieDB movie = null;
|
||||
Node node = nlMovies.item(0);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
movie = new MovieDB();
|
||||
|
||||
for (int i = 0; i < nlMovies.getLength(); i++) {
|
||||
Node movieNode = nlMovies.item(i);
|
||||
if (movieNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element movieElement = (Element) movieNode;
|
||||
movie = parseMovieInfo(movieElement);
|
||||
if (movie != null) {
|
||||
movies.add(movie);
|
||||
}
|
||||
}
|
||||
Element element = (Element) node;
|
||||
movie = MovieDbParser.parseSimpleMovie(element);
|
||||
}
|
||||
return movies;
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
public static Person parseLatestPerson(String url) {
|
||||
Person person = new Person();
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(url);
|
||||
} catch (Exception error) {
|
||||
logger.severe("Person.getLatest error: " + error.getMessage());
|
||||
return person;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return person;
|
||||
}
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("person");
|
||||
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return person;
|
||||
}
|
||||
|
||||
Node node = nlMovies.item(0);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
person = new Person();
|
||||
|
||||
Element element = (Element) node;
|
||||
person = MovieDbParser.parseSimplePerson(element);
|
||||
}
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,84 +211,36 @@ public class MovieDbParser {
|
||||
return movie;
|
||||
}
|
||||
|
||||
public static Person parsePersonInfo(String searchUrl) {
|
||||
Person person = null;
|
||||
public static List<MovieDB> parseMovieGetVersion(String url) {
|
||||
List<MovieDB> movies = new ArrayList<MovieDB>();
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
} catch (Exception error) {
|
||||
logger.severe("PersonSearch error: " + error.getMessage());
|
||||
return person;
|
||||
doc = DOMHelper.getEventDocFromUrl(url);
|
||||
} catch (Exception e) {
|
||||
logger.severe("Movie.getVersion error: " + e.getMessage());
|
||||
return movies;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return person;
|
||||
return movies;
|
||||
}
|
||||
|
||||
try {
|
||||
person = new Person();
|
||||
NodeList personNodeList = doc.getElementsByTagName("person");
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
|
||||
// Only get the first movie from the list
|
||||
Node personNode = personNodeList.item(0);
|
||||
|
||||
if (personNode == null) {
|
||||
logger.finest("Person not found");
|
||||
return person;
|
||||
}
|
||||
|
||||
if (personNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element personElement = (Element) personNode;
|
||||
|
||||
person.setName(DOMHelper.getValueFromElement(personElement, "name"));
|
||||
person.setId(DOMHelper.getValueFromElement(personElement, "id"));
|
||||
person.setBiography(DOMHelper.getValueFromElement(personElement, "biography"));
|
||||
person.setKnownMovies(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "known_movies")));
|
||||
person.setBirthday(DOMHelper.getValueFromElement(personElement, "birthday"));
|
||||
person.setBirthPlace(DOMHelper.getValueFromElement(personElement, "birthplace"));
|
||||
person.setUrl(DOMHelper.getValueFromElement(personElement, "url"));
|
||||
person.setVersion(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "version")));
|
||||
person.setLastModifiedAt(DOMHelper.getValueFromElement(personElement, "last_modified_at"));
|
||||
|
||||
NodeList artworkNodeList = doc.getElementsByTagName("image");
|
||||
for (int nodeLoop = 0; nodeLoop < artworkNodeList.getLength(); nodeLoop++) {
|
||||
Node artworkNode = artworkNodeList.item(nodeLoop);
|
||||
if (artworkNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element artworkElement = (Element) artworkNode;
|
||||
Artwork artwork = new Artwork();
|
||||
artwork.setType(artworkElement.getAttribute("type"));
|
||||
artwork.setUrl(artworkElement.getAttribute("url"));
|
||||
artwork.setSize(artworkElement.getAttribute("size"));
|
||||
artwork.setId(artworkElement.getAttribute("id"));
|
||||
person.addArtwork(artwork);
|
||||
}
|
||||
}
|
||||
|
||||
NodeList filmNodeList = doc.getElementsByTagName("movie");
|
||||
for (int nodeLoop = 0; nodeLoop < filmNodeList.getLength(); nodeLoop++) {
|
||||
Node filmNode = filmNodeList.item(nodeLoop);
|
||||
if (filmNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element filmElement = (Element) filmNode;
|
||||
Filmography film = new Filmography();
|
||||
|
||||
film.setCharacter(filmElement.getAttribute("character"));
|
||||
film.setDepartment(filmElement.getAttribute("department"));
|
||||
film.setId(filmElement.getAttribute("id"));
|
||||
film.setJob(filmElement.getAttribute("job"));
|
||||
film.setName(filmElement.getAttribute("name"));
|
||||
film.setUrl(filmElement.getAttribute("url"));
|
||||
|
||||
person.addFilm(film);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception error) {
|
||||
logger.severe("ERROR: " + error.getMessage());
|
||||
error.printStackTrace();
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return movies;
|
||||
}
|
||||
|
||||
return person;
|
||||
for (int i = 0; i < nlMovies.getLength(); i++) {
|
||||
Node node = nlMovies.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
movies.add(MovieDbParser.parseSimpleMovie(element));
|
||||
}
|
||||
}
|
||||
|
||||
return movies;
|
||||
}
|
||||
|
||||
private static MovieDB parseMovieInfo(Element movieElement) {
|
||||
@@ -410,6 +462,49 @@ public class MovieDbParser {
|
||||
return movie;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of MovieDB object parsed from the DOM Document
|
||||
* even if there is only one movie
|
||||
* @param doc DOM Document
|
||||
* @return
|
||||
*/
|
||||
public static List<MovieDB> parseMovies(String searchUrl) {
|
||||
List<MovieDB> movies = new ArrayList<MovieDB>();
|
||||
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
} catch (Exception error) {
|
||||
logger.severe("TheMovieDb Error: " + error.getMessage());
|
||||
return movies;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return movies;
|
||||
}
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return movies;
|
||||
}
|
||||
|
||||
MovieDB movie = null;
|
||||
|
||||
for (int i = 0; i < nlMovies.getLength(); i++) {
|
||||
Node movieNode = nlMovies.item(i);
|
||||
if (movieNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element movieElement = (Element) movieNode;
|
||||
movie = parseMovieInfo(movieElement);
|
||||
if (movie != null) {
|
||||
movies.add(movie);
|
||||
}
|
||||
}
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a DOM document and returns a list of Person
|
||||
* @param doc a DOM document
|
||||
@@ -446,148 +541,103 @@ public class MovieDbParser {
|
||||
return people;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of valid genres within TMDb.
|
||||
* @param doc a DOM document
|
||||
* @return
|
||||
*/
|
||||
public static List<Category> parseCategories(String searchUrl) {
|
||||
Document doc = null;
|
||||
List<Category> categories = new ArrayList<Category>();
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
} catch (Exception error) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
NodeList genres = doc.getElementsByTagName("genre");
|
||||
if ((genres == null) || genres.getLength() == 0) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
for (int i = 0; i < genres.getLength(); i++) {
|
||||
Node node = genres.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
Category category = new Category();
|
||||
category.setName(element.getAttribute("name"));
|
||||
category.setId(DOMHelper.getValueFromElement(element, "id"));
|
||||
category.setUrl(DOMHelper.getValueFromElement(element, "url"));
|
||||
categories.add(category);
|
||||
}
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a DOM document and returns the latest Movie.
|
||||
* This method is used for Movie.getLatest and Movie.getVersion where only
|
||||
* a few fields are initialized.
|
||||
* @param doc
|
||||
* @return
|
||||
*/
|
||||
public static MovieDB parseLatestMovie(String searchUrl) {
|
||||
MovieDB movie = null;
|
||||
public static Person parsePersonInfo(String searchUrl) {
|
||||
Person person = null;
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(searchUrl);
|
||||
} catch (Exception error) {
|
||||
logger.severe("GetLatest error: " + error.getMessage());
|
||||
return movie;
|
||||
logger.severe("PersonSearch error: " + error.getMessage());
|
||||
return person;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return movie;
|
||||
return person;
|
||||
}
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return movie;
|
||||
}
|
||||
|
||||
Node node = nlMovies.item(0);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
movie = new MovieDB();
|
||||
|
||||
Element element = (Element) node;
|
||||
movie = MovieDbParser.parseSimpleMovie(element);
|
||||
}
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
public static List<MovieDB> parseMovieGetVersion(String url) {
|
||||
List<MovieDB> movies = new ArrayList<MovieDB>();
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(url);
|
||||
} catch (Exception e) {
|
||||
logger.severe("Movie.getVersion error: " + e.getMessage());
|
||||
return movies;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return movies;
|
||||
}
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("movie");
|
||||
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return movies;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nlMovies.getLength(); i++) {
|
||||
Node node = nlMovies.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
movies.add(MovieDbParser.parseSimpleMovie(element));
|
||||
}
|
||||
}
|
||||
|
||||
return movies;
|
||||
}
|
||||
|
||||
public static Person parseLatestPerson(String url) {
|
||||
Person person = new Person();
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(url);
|
||||
} catch (Exception error) {
|
||||
logger.severe("Person.getLatest error: " + error.getMessage());
|
||||
return person;
|
||||
}
|
||||
|
||||
if (doc == null) {
|
||||
return person;
|
||||
}
|
||||
|
||||
NodeList nlMovies = doc.getElementsByTagName("person");
|
||||
|
||||
if ((nlMovies == null) || nlMovies.getLength() == 0) {
|
||||
return person;
|
||||
}
|
||||
|
||||
Node node = nlMovies.item(0);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
person = new Person();
|
||||
NodeList personNodeList = doc.getElementsByTagName("person");
|
||||
|
||||
Element element = (Element) node;
|
||||
person = MovieDbParser.parseSimplePerson(element);
|
||||
// Only get the first movie from the list
|
||||
Node personNode = personNodeList.item(0);
|
||||
|
||||
if (personNode == null) {
|
||||
logger.finest("Person not found");
|
||||
return person;
|
||||
}
|
||||
|
||||
if (personNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element personElement = (Element) personNode;
|
||||
|
||||
person.setName(DOMHelper.getValueFromElement(personElement, "name"));
|
||||
person.setId(DOMHelper.getValueFromElement(personElement, "id"));
|
||||
person.setBiography(DOMHelper.getValueFromElement(personElement, "biography"));
|
||||
person.setKnownMovies(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "known_movies")));
|
||||
person.setBirthday(DOMHelper.getValueFromElement(personElement, "birthday"));
|
||||
person.setBirthPlace(DOMHelper.getValueFromElement(personElement, "birthplace"));
|
||||
person.setUrl(DOMHelper.getValueFromElement(personElement, "url"));
|
||||
person.setVersion(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "version")));
|
||||
person.setLastModifiedAt(DOMHelper.getValueFromElement(personElement, "last_modified_at"));
|
||||
|
||||
NodeList artworkNodeList = doc.getElementsByTagName("image");
|
||||
for (int nodeLoop = 0; nodeLoop < artworkNodeList.getLength(); nodeLoop++) {
|
||||
Node artworkNode = artworkNodeList.item(nodeLoop);
|
||||
if (artworkNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element artworkElement = (Element) artworkNode;
|
||||
Artwork artwork = new Artwork();
|
||||
artwork.setType(artworkElement.getAttribute("type"));
|
||||
artwork.setUrl(artworkElement.getAttribute("url"));
|
||||
artwork.setSize(artworkElement.getAttribute("size"));
|
||||
artwork.setId(artworkElement.getAttribute("id"));
|
||||
person.addArtwork(artwork);
|
||||
}
|
||||
}
|
||||
|
||||
NodeList filmNodeList = doc.getElementsByTagName("movie");
|
||||
for (int nodeLoop = 0; nodeLoop < filmNodeList.getLength(); nodeLoop++) {
|
||||
Node filmNode = filmNodeList.item(nodeLoop);
|
||||
if (filmNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element filmElement = (Element) filmNode;
|
||||
Filmography film = new Filmography();
|
||||
|
||||
film.setCharacter(filmElement.getAttribute("character"));
|
||||
film.setDepartment(filmElement.getAttribute("department"));
|
||||
film.setId(filmElement.getAttribute("id"));
|
||||
film.setJob(filmElement.getAttribute("job"));
|
||||
film.setName(filmElement.getAttribute("name"));
|
||||
film.setUrl(filmElement.getAttribute("url"));
|
||||
|
||||
person.addFilm(film);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception error) {
|
||||
logger.severe("ERROR: " + error.getMessage());
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a "simple" Language in the form:
|
||||
* <language iso_639_1="en">
|
||||
* <english_name>English</english_name>
|
||||
* <native_name>English</native_name>
|
||||
* </language>
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
private static Language parseSimpleLanguage(Element element) {
|
||||
Language language = new Language();
|
||||
language.setIsoCode(element.getAttribute("iso_639_1"));
|
||||
language.setEnglishName(DOMHelper.getValueFromElement(element, "english_name"));
|
||||
language.setNativeName(DOMHelper.getValueFromElement(element, "native_name"));
|
||||
return language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a "simple" Movie in the form:
|
||||
* <movie>
|
||||
@@ -609,7 +659,7 @@ public class MovieDbParser {
|
||||
movie.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at"));
|
||||
return movie;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse a "simple" Person in the form:
|
||||
* <person>
|
||||
@@ -629,4 +679,5 @@ public class MovieDbParser {
|
||||
person.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at"));
|
||||
return person;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user