Added getTranslations method

master
Omertron 15 years ago
parent 2ec7c56e35
commit ec442572ba

@ -25,6 +25,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.moviejukebox.themoviedb.model.Category; import com.moviejukebox.themoviedb.model.Category;
import com.moviejukebox.themoviedb.model.Language;
import com.moviejukebox.themoviedb.model.MovieDB; import com.moviejukebox.themoviedb.model.MovieDB;
import com.moviejukebox.themoviedb.model.Person; import com.moviejukebox.themoviedb.model.Person;
import com.moviejukebox.themoviedb.tools.MovieDbParser; import com.moviejukebox.themoviedb.tools.MovieDbParser;
@ -41,25 +42,140 @@ import com.moviejukebox.themoviedb.tools.WebBrowser;
*/ */
public class TheMovieDb { 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 String apiKey;
private static Logger logger = null; private static Logger logger = null;
private static LogFormatter tmdbFormatter = new LogFormatter(); 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 ConsoleHandler tmdbConsoleHandler = new ConsoleHandler();
private static final String apiSite = "http://api.themoviedb.org/2.1/"; private static final String apiSite = "http://api.themoviedb.org/2.1/";
private static final String defaultLanguage = "en-US"; private static final String defaultLanguage = "en-US";
private static final String GENRES_GET_LIST = "Genres.getList"; /*
* 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_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_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_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_GET_VERSION = "Movie.getVersion";
private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup"; private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup";
private static final String MOVIE_SEARCH = "Movie.search"; private static final String MOVIE_SEARCH = "Movie.search";
/*
* People
*/
private static final String PERSON_GET_INFO = "Person.getInfo"; private static final String PERSON_GET_INFO = "Person.getInfo";
private static final String PERSON_GET_LATEST = "Person.getLatest"; private static final String PERSON_GET_LATEST = "Person.getLatest";
private static final String PERSON_GET_VERSION = "Person.getVersion"; private static final String PERSON_GET_VERSION = "Person.getVersion";
private static final String PERSON_SEARCH = "Person.search"; 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) { public TheMovieDb(String apiKey) {
setLogger(Logger.getLogger("TheMovieDB")); setLogger(Logger.getLogger("TheMovieDB"));
if (!isValidString(apiKey)) { if (!isValidString(apiKey)) {
@ -77,44 +193,55 @@ public class TheMovieDb {
} }
/** /**
* Set proxy parameters. * Build comma separated ids for Movie.getLatest and Movie.getVersion.
* @param host proxy host URL * @param ids a List of ids
* @param port proxy port * @return
* @param username proxy username
* @param password proxy password
*/ */
public void setProxy(String host, String port, String username, String password) { private String buildIds(List<String> ids) {
WebBrowser.setProxyHost(host); String s = "";
WebBrowser.setProxyPort(port); for (int i = 0; i < ids.size(); i++) {
WebBrowser.setProxyUsername(username); if (i == 0) {
WebBrowser.setProxyPassword(password); s += ids.get(i);
continue;
}
s += "," + ids.get(i);
}
return s;
} }
/** /**
* Set web browser timeout. * Build the URL that is used to get the XML from TMDb.
* @param webTimeoutConnect *
* @param webTimeoutRead * @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) { private String buildUrl(String prefix, String searchTerm, String language) {
WebBrowser.setWebTimeoutConnect(webTimeoutConnect); String url = apiSite + prefix + "/" + language + "/xml/" + apiKey;
WebBrowser.setWebTimeoutRead(webTimeoutRead);
if (!isValidString(searchTerm)) {
return url;
} }
public static Logger getLogger() { String encodedSearchTerm;
return logger;
try {
encodedSearchTerm = URLEncoder.encode(searchTerm, "UTF-8");
} catch (UnsupportedEncodingException e) {
encodedSearchTerm = searchTerm;
} }
public void setLogger(Logger logger) { if (prefix.equals(MOVIE_BROWSE)) {
if (logger == null) { url += "?";
return; } else {
url += "/";
} }
TheMovieDb.logger = logger; url += encodedSearchTerm;
tmdbConsoleHandler.setFormatter(tmdbFormatter);
tmdbConsoleHandler.setLevel(Level.FINE); logger.finest("Search URL: " + url);
logger.addHandler(tmdbConsoleHandler); return url;
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
} }
/** /**
@ -126,12 +253,12 @@ public class TheMovieDb {
} }
/** /**
* Set the TMDb API key. * Retrieve a list of valid genres within TMDb.
* @param apiKey a valid TMDb API key. * @param language the two digit language code. E.g. en=English
* @return
*/ */
public void setApiKey(String apiKey) { public List<Category> getCategories(String language) {
this.apiKey = apiKey; return MovieDbParser.parseCategories(this.buildUrl(GENRES_GET_LIST, "", language));
tmdbFormatter.addApiKey(apiKey);
} }
/** /**
@ -142,36 +269,8 @@ public class TheMovieDb {
return defaultLanguage; return defaultLanguage;
} }
/** public List<Language> getTranslations(String movieId, String language) {
* Searches the database using the movie title passed return MovieDbParser.parseLanguages(this.buildUrl(MOVIE_GET_TRANSLATIONS, movieId, language));
*
* @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);
} }
/** /**
@ -227,35 +326,51 @@ public class TheMovieDb {
} }
/** /**
* Searches the database using the IMDb reference * Browse the database using the default parameters.
* http://api.themoviedb.org/2.1/methods/Movie.browse
* *
* @param imdbID IMDb reference, must include the "tt" at the start * @param orderBy either <code>rating</code>,
* @param language The two digit language code. E.g. en=English * <code>release</code> or <code>title</code>
* @return A movie bean with the data extracted * @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) { public List<MovieDB> moviedbBrowse(String orderBy, String order, String language) {
MovieDB movie = new MovieDB(); 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; return movie;
} }
String searchUrl = buildUrl(MOVIE_IMDB_LOOKUP, imdbID, language); String searchUrl = buildUrl(MOVIE_GET_IMAGES, movieId, language);
return MovieDbParser.parseMovie(searchUrl); return MovieDbParser.parseMovie(searchUrl);
} }
/** /**
* Passes a null MovieDB object to the full function * 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
* @param tmdbID TheMovieDB ID of the movie to get the information for * new images if that's all you're after.
* @param language The two digit language code. E.g. en=English * @param movieId the TMDb or IMDB ID (starting with tt) of the movie you
* @return A movie bean with all of the information * are searching for.
*/ * @param language the two digit language code. E.g. en=English
public MovieDB moviedbGetInfo(String tmdbID, String language) { * @return
MovieDB movie = null; */
movie = moviedbGetInfo(tmdbID, movie, language); public MovieDB moviedbGetImages(String movieId, String language) {
return movie; return moviedbGetImages(movieId, new MovieDB(), language);
} }
/** /**
@ -287,6 +402,19 @@ public class TheMovieDb {
return movie; 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 * 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 * 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)); 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 * 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 * 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 * The Movie.getVersion method is used to retrieve the last modified time
* posters for a particular movie. This is useful to scan for updates, or * along with the current version number of the called object(s). This is
* new images if that's all you're after. * useful if you've already called the object sometime in the past and
* @param movieId the TMDb or IMDB ID (starting with tt) of the movie you * simply want to do a quick check for updates. <br/>
* are searching for. * 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 * @param language the two digit language code. E.g. en=English
* @return * @return
*/ */
public MovieDB moviedbGetImages(String movieId, String language) { public MovieDB moviedbGetVersion(String movieId, String language) {
return moviedbGetImages(movieId, new MovieDB(), 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 * Searches the database using the IMDb reference
* posters for a particular movie. This is useful to scan for updates, or *
* new images if that's all you're after. * @param imdbID IMDb reference, must include the "tt" at the start
* @param movieId the TMDb or IMDB ID (starting with tt) of the movie you * @param language The two digit language code. E.g. en=English
* are searching for. * @return A movie bean with the data extracted
* @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) { public MovieDB moviedbImdbLookup(String imdbID, String language) {
// If the searchTerm is null, then exit MovieDB movie = new MovieDB();
if (!isValidString(movieId)) {
// If the imdbID is null, then exit
if (!isValidString(imdbID)) {
return movie; return movie;
} }
String searchUrl = buildUrl(MOVIE_GET_IMAGES, movieId, language); String searchUrl = buildUrl(MOVIE_IMDB_LOOKUP, imdbID, language);
return MovieDbParser.parseMovie(searchUrl); return MovieDbParser.parseMovie(searchUrl);
} }
/** /**
* The Person.search method is used to search for an actor, actress or production member. * Searches the database using the movie title passed
* http://api.themoviedb.org/2.1/methods/Person.search
* *
* @param personName * @param movieTitle The title to search for
* @param language * @param language The two digit language code. E.g. en=English
* @return * @return A movie bean with the data extracted
*/ */
public Person personSearch(String personName, String language) { public List<MovieDB> moviedbSearch(String movieTitle, String language) {
if (!isValidString(personName)) { // If the title is null, then exit
return new Person(); if (!isValidString(movieTitle)) {
return new ArrayList<MovieDB>();
} }
String searchUrl = buildUrl(PERSON_SEARCH, personName, language); String searchUrl = buildUrl(MOVIE_SEARCH, movieTitle, language);
return MovieDbParser.parsePersonInfo(searchUrl); return MovieDbParser.parseMovies(searchUrl);
} }
/** /**
@ -423,30 +537,6 @@ public class TheMovieDb {
return MovieDbParser.parseLatestPerson(buildUrl(PERSON_GET_LATEST, "", language)); 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 personID a Person TMDb id
* @param language the two digit language code. E.g. en=English
* @return
*/
public Person personGetVersion(String personID, String language) {
Person person = new Person();
if (!isValidString(personID)) {
return person;
}
List<Person> people = this.personGetVersion(Arrays.asList(personID), language);
if (people.isEmpty()) {
return person;
}
return people.get(0);
}
/** /**
* The Person.getVersion method is used to retrieve the last modified time * 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 * along with the current version number of the called object(s). This is
@ -467,147 +557,89 @@ public class TheMovieDb {
} }
/** /**
* Retrieve a list of valid genres within TMDb. * 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 personID a Person TMDb id
* @param language the two digit language code. E.g. en=English * @param language the two digit language code. E.g. en=English
* @return * @return
*/ */
public List<Category> getCategories(String language) { public Person personGetVersion(String personID, String language) {
return MovieDbParser.parseCategories(this.buildUrl(GENRES_GET_LIST, "", language)); Person person = new Person();
} if (!isValidString(personID)) {
return person;
/**
* 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) { List<Person> people = this.personGetVersion(Arrays.asList(personID), language);
if (compareMovies(moviedb, title, year)) { if (people.isEmpty()) {
return moviedb; return person;
}
} }
return null; return people.get(0);
} }
/** /**
* Compare the MovieDB object with a title & year * The Person.search method is used to search for an actor, actress or production member.
* @param moviedb The moviedb object to compare too * http://api.themoviedb.org/2.1/methods/Person.search
* @param title The title of the movie to compare *
* @param year The year of the movie to compare * @param personName
* @return True if there is a match, False otherwise. * @param language
* @return
*/ */
public static boolean compareMovies(MovieDB moviedb, String title, String year) { public Person personSearch(String personName, String language) {
if ((moviedb == null) || (!isValidString(title))) { if (!isValidString(personName)) {
return false; return new Person();
}
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 String searchUrl = buildUrl(PERSON_SEARCH, personName, language);
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) { return MovieDbParser.parsePersonInfo(searchUrl);
return true;
}
}
return false;
} }
/** /**
* Build the URL that is used to get the XML from TMDb. * Set the TMDb API key.
* * @param apiKey a valid TMDb API key.
* @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) { public void setApiKey(String apiKey) {
String url = apiSite + prefix + "/" + language + "/xml/" + apiKey; this.apiKey = apiKey;
tmdbFormatter.addApiKey(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)) { public void setLogger(Logger logger) {
url += "?"; if (logger == null) {
} else { return;
url += "/";
} }
url += encodedSearchTerm; TheMovieDb.logger = logger;
tmdbConsoleHandler.setFormatter(tmdbFormatter);
logger.finest("Search URL: " + url); tmdbConsoleHandler.setLevel(Level.FINE);
return url; logger.addHandler(tmdbConsoleHandler);
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
} }
/** /**
* Build comma separated ids for Movie.getLatest and Movie.getVersion. * Set proxy parameters.
* @param ids a List of ids * @param host proxy host URL
* @return * @param port proxy port
* @param username proxy username
* @param password proxy password
*/ */
private String buildIds(List<String> ids) { public void setProxy(String host, String port, String username, String password) {
String s = ""; WebBrowser.setProxyHost(host);
for (int i = 0; i < ids.size(); i++) { WebBrowser.setProxyPort(port);
if (i == 0) { WebBrowser.setProxyUsername(username);
s += ids.get(i); WebBrowser.setProxyPassword(password);
continue;
}
s += "," + ids.get(i);
}
return s;
} }
/** /**
* Check the string passed to see if it contains a value. * Set web browser timeout.
* @param testString The string to test * @param webTimeoutConnect
* @return False if the string is empty, null or UNKNOWN, True otherwise * @param webTimeoutRead
*/ */
private static boolean isValidString(String testString) { public void setTimeout(int webTimeoutConnect, int webTimeoutRead) {
if ((testString == null) WebBrowser.setWebTimeoutConnect(webTimeoutConnect);
|| (testString.trim().equals("")) WebBrowser.setWebTimeoutRead(webTimeoutRead);
|| (testString.equalsIgnoreCase(MovieDB.UNKNOWN))) {
return false;
}
return true;
} }
} }

@ -13,7 +13,7 @@
package com.moviejukebox.themoviedb.model; package com.moviejukebox.themoviedb.model;
/** /**
* Category from the MovieDB.org * Category from TheMovieDB.org
* *
* @author Stuart.Boston * @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.Category;
import com.moviejukebox.themoviedb.model.Country; import com.moviejukebox.themoviedb.model.Country;
import com.moviejukebox.themoviedb.model.Filmography; import com.moviejukebox.themoviedb.model.Filmography;
import com.moviejukebox.themoviedb.model.Language;
import com.moviejukebox.themoviedb.model.MovieDB; import com.moviejukebox.themoviedb.model.MovieDB;
import com.moviejukebox.themoviedb.model.Person; import com.moviejukebox.themoviedb.model.Person;
import com.moviejukebox.themoviedb.model.Studio; import com.moviejukebox.themoviedb.model.Studio;
@ -35,61 +36,91 @@ public class MovieDbParser {
static Logger logger = TheMovieDb.getLogger(); static Logger logger = TheMovieDb.getLogger();
/** /**
* Returns a list of MovieDB object parsed from the DOM Document * Retrieve a list of valid genres within TMDb.
* even if there is only one movie * @param doc a DOM document
* @param doc DOM Document
* @return * @return
*/ */
public static List<MovieDB> parseMovies(String searchUrl) { public static List<Category> parseCategories(String searchUrl) {
List<MovieDB> movies = new ArrayList<MovieDB>();
Document doc = null; Document doc = null;
List<Category> categories = new ArrayList<Category>();
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
} catch (Exception error) { } catch (Exception error) {
logger.severe("TheMovieDb Error: " + error.getMessage()); return categories;
return movies;
} }
if (doc == null) { if (doc == null) {
return movies; return categories;
} }
NodeList nlMovies = doc.getElementsByTagName("movie"); NodeList genres = doc.getElementsByTagName("genre");
if ((genres == null) || genres.getLength() == 0) {
return categories;
}
if ((nlMovies == null) || nlMovies.getLength() == 0) { for (int i = 0; i < genres.getLength(); i++) {
return movies; 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);
}
} }
MovieDB movie = null; return categories;
}
for (int i = 0; i < nlMovies.getLength(); i++) { public static List<Language> parseLanguages(String url) {
Node movieNode = nlMovies.item(i); List<Language> languages = new ArrayList<Language>();
if (movieNode.getNodeType() == Node.ELEMENT_NODE) { Document doc = null;
Element movieElement = (Element) movieNode;
movie = parseMovieInfo(movieElement); try {
if (movie != null) { doc = DOMHelper.getEventDocFromUrl(url);
movies.add(movie); } 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 movies; }
return languages;
} }
/** /**
* Returns the first MovieDB from the DOM Document. * Parse a DOM document and returns the latest Movie.
* @param doc a DOM Document * This method is used for Movie.getLatest and Movie.getVersion where only
* a few fields are initialized.
* @param doc
* @return * @return
*/ */
public static MovieDB parseMovie(String searchUrl) { public static MovieDB parseLatestMovie(String searchUrl) {
MovieDB movie = null; MovieDB movie = null;
Document doc = null; Document doc = null;
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
} catch (Exception error) { } catch (Exception error) {
logger.severe("TheMovieDb Error: " + error.getMessage()); logger.severe("GetLatest error: " + error.getMessage());
return movie; return movie;
} }
@ -98,27 +129,30 @@ public class MovieDbParser {
} }
NodeList nlMovies = doc.getElementsByTagName("movie"); NodeList nlMovies = doc.getElementsByTagName("movie");
if ((nlMovies == null) || nlMovies.getLength() == 0) { if ((nlMovies == null) || nlMovies.getLength() == 0) {
return movie; return movie;
} }
Node nMovie = nlMovies.item(0); Node node = nlMovies.item(0);
if (nMovie.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eMovie = (Element) nMovie; movie = new MovieDB();
movie = parseMovieInfo(eMovie);
Element element = (Element) node;
movie = MovieDbParser.parseSimpleMovie(element);
} }
return movie; return movie;
} }
public static Person parsePersonInfo(String searchUrl) { public static Person parseLatestPerson(String url) {
Person person = null; Person person = new Person();
Document doc = null; Document doc = null;
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(url);
} catch (Exception error) { } catch (Exception error) {
logger.severe("PersonSearch error: " + error.getMessage()); logger.severe("Person.getLatest error: " + error.getMessage());
return person; return person;
} }
@ -126,69 +160,87 @@ public class MovieDbParser {
return person; return person;
} }
try { 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(); person = new Person();
NodeList personNodeList = doc.getElementsByTagName("person");
// Only get the first movie from the list Element element = (Element) node;
Node personNode = personNodeList.item(0); person = MovieDbParser.parseSimplePerson(element);
}
if (personNode == null) {
logger.finest("Person not found");
return person; return person;
} }
if (personNode.getNodeType() == Node.ELEMENT_NODE) { /**
Element personElement = (Element) personNode; * Returns the first MovieDB from the DOM Document.
* @param doc a DOM Document
* @return
*/
public static MovieDB parseMovie(String searchUrl) {
MovieDB movie = null;
Document doc = null;
person.setName(DOMHelper.getValueFromElement(personElement, "name")); try {
person.setId(DOMHelper.getValueFromElement(personElement, "id")); doc = DOMHelper.getEventDocFromUrl(searchUrl);
person.setBiography(DOMHelper.getValueFromElement(personElement, "biography")); } catch (Exception error) {
person.setKnownMovies(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "known_movies"))); logger.severe("TheMovieDb Error: " + error.getMessage());
person.setBirthday(DOMHelper.getValueFromElement(personElement, "birthday")); return movie;
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"); if (doc == null) {
for (int nodeLoop = 0; nodeLoop < artworkNodeList.getLength(); nodeLoop++) { return movie;
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 nlMovies = doc.getElementsByTagName("movie");
if ((nlMovies == null) || nlMovies.getLength() == 0) {
return movie;
} }
NodeList filmNodeList = doc.getElementsByTagName("movie"); Node nMovie = nlMovies.item(0);
for (int nodeLoop = 0; nodeLoop < filmNodeList.getLength(); nodeLoop++) { if (nMovie.getNodeType() == Node.ELEMENT_NODE) {
Node filmNode = filmNodeList.item(nodeLoop); Element eMovie = (Element) nMovie;
if (filmNode.getNodeType() == Node.ELEMENT_NODE) { movie = parseMovieInfo(eMovie);
Element filmElement = (Element) filmNode; }
Filmography film = new Filmography();
film.setCharacter(filmElement.getAttribute("character")); return movie;
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); 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));
} }
} catch (Exception error) {
logger.severe("ERROR: " + error.getMessage());
error.printStackTrace();
} }
return person; return movies;
} }
private static MovieDB parseMovieInfo(Element movieElement) { private static MovieDB parseMovieInfo(Element movieElement) {
@ -411,181 +463,179 @@ public class MovieDbParser {
} }
/** /**
* Parse a DOM document and returns a list of Person * Returns a list of MovieDB object parsed from the DOM Document
* @param doc a DOM document * even if there is only one movie
* @param doc DOM Document
* @return * @return
*/ */
public static List<Person> parsePersonGetVersion(String searchUrl) { public static List<MovieDB> parseMovies(String searchUrl) {
List<Person> people = new ArrayList<Person>(); List<MovieDB> movies = new ArrayList<MovieDB>();
Document doc = null; Document doc = null;
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
} catch (Exception error) { } catch (Exception error) {
logger.severe("PersonGetVersion error: " + error.getMessage()); logger.severe("TheMovieDb Error: " + error.getMessage());
return people; return movies;
} }
if (doc == null) { if (doc == null) {
return people; return movies;
} }
NodeList movies = doc.getElementsByTagName("movie"); NodeList nlMovies = doc.getElementsByTagName("movie");
if ((movies == null) || movies.getLength() == 0) {
return people; if ((nlMovies == null) || nlMovies.getLength() == 0) {
return movies;
} }
for (int i = 0; i < movies.getLength(); i++) { MovieDB movie = null;
Node node = movies.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) { for (int i = 0; i < nlMovies.getLength(); i++) {
Element element = (Element) node; Node movieNode = nlMovies.item(i);
people.add(MovieDbParser.parseSimplePerson(element)); if (movieNode.getNodeType() == Node.ELEMENT_NODE) {
Element movieElement = (Element) movieNode;
movie = parseMovieInfo(movieElement);
if (movie != null) {
movies.add(movie);
} }
} }
}
return people; return movies;
} }
/** /**
* Retrieve a list of valid genres within TMDb. * Parse a DOM document and returns a list of Person
* @param doc a DOM document * @param doc a DOM document
* @return * @return
*/ */
public static List<Category> parseCategories(String searchUrl) { public static List<Person> parsePersonGetVersion(String searchUrl) {
List<Person> people = new ArrayList<Person>();
Document doc = null; Document doc = null;
List<Category> categories = new ArrayList<Category>();
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
} catch (Exception error) { } catch (Exception error) {
return categories; logger.severe("PersonGetVersion error: " + error.getMessage());
return people;
} }
if (doc == null) { if (doc == null) {
return categories; return people;
} }
NodeList genres = doc.getElementsByTagName("genre"); NodeList movies = doc.getElementsByTagName("movie");
if ((genres == null) || genres.getLength() == 0) { if ((movies == null) || movies.getLength() == 0) {
return categories; return people;
} }
for (int i = 0; i < genres.getLength(); i++) { for (int i = 0; i < movies.getLength(); i++) {
Node node = genres.item(i); Node node = movies.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node; Element element = (Element) node;
Category category = new Category(); people.add(MovieDbParser.parseSimplePerson(element));
category.setName(element.getAttribute("name"));
category.setId(DOMHelper.getValueFromElement(element, "id"));
category.setUrl(DOMHelper.getValueFromElement(element, "url"));
categories.add(category);
} }
} }
return categories; return people;
} }
/** public static Person parsePersonInfo(String searchUrl) {
* Parse a DOM document and returns the latest Movie. Person person = null;
* 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; Document doc = null;
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
} catch (Exception error) { } catch (Exception error) {
logger.severe("GetLatest error: " + error.getMessage()); logger.severe("PersonSearch error: " + error.getMessage());
return movie; return person;
} }
if (doc == null) { if (doc == null) {
return movie; return person;
} }
NodeList nlMovies = doc.getElementsByTagName("movie"); try {
person = new Person();
if ((nlMovies == null) || nlMovies.getLength() == 0) { NodeList personNodeList = doc.getElementsByTagName("person");
return movie;
}
Node node = nlMovies.item(0); // Only get the first movie from the list
if (node.getNodeType() == Node.ELEMENT_NODE) { Node personNode = personNodeList.item(0);
movie = new MovieDB();
Element element = (Element) node; if (personNode == null) {
movie = MovieDbParser.parseSimpleMovie(element); logger.finest("Person not found");
return person;
} }
return movie; if (personNode.getNodeType() == Node.ELEMENT_NODE) {
} Element personElement = (Element) personNode;
public static List<MovieDB> parseMovieGetVersion(String url) { person.setName(DOMHelper.getValueFromElement(personElement, "name"));
List<MovieDB> movies = new ArrayList<MovieDB>(); person.setId(DOMHelper.getValueFromElement(personElement, "id"));
Document doc = null; 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"));
try { NodeList artworkNodeList = doc.getElementsByTagName("image");
doc = DOMHelper.getEventDocFromUrl(url); for (int nodeLoop = 0; nodeLoop < artworkNodeList.getLength(); nodeLoop++) {
} catch (Exception e) { Node artworkNode = artworkNodeList.item(nodeLoop);
logger.severe("Movie.getVersion error: " + e.getMessage()); if (artworkNode.getNodeType() == Node.ELEMENT_NODE) {
return movies; 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);
} }
if (doc == null) {
return movies;
} }
NodeList nlMovies = doc.getElementsByTagName("movie"); 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();
if ((nlMovies == null) || nlMovies.getLength() == 0) { film.setCharacter(filmElement.getAttribute("character"));
return movies; 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"));
for (int i = 0; i < nlMovies.getLength(); i++) { person.addFilm(film);
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) { } catch (Exception error) {
logger.severe("Person.getLatest error: " + error.getMessage()); logger.severe("ERROR: " + error.getMessage());
return person; error.printStackTrace();
}
if (doc == null) {
return person;
} }
NodeList nlMovies = doc.getElementsByTagName("person");
if ((nlMovies == null) || nlMovies.getLength() == 0) {
return person; return person;
} }
Node node = nlMovies.item(0); /**
if (node.getNodeType() == Node.ELEMENT_NODE) { * Parse a "simple" Language in the form:
person = new Person(); * <language iso_639_1="en">
* <english_name>English</english_name>
Element element = (Element) node; * <native_name>English</native_name>
person = MovieDbParser.parseSimplePerson(element); * </language>
} * @param element
* @return
return person; */
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;
} }
/** /**
@ -629,4 +679,5 @@ public class MovieDbParser {
person.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at")); person.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at"));
return person; return person;
} }
} }

Loading…
Cancel
Save