From ec442572ba77903e04552d24ede359de29e6ade6 Mon Sep 17 00:00:00 2001 From: Omertron Date: Fri, 26 Nov 2010 15:36:57 +0000 Subject: [PATCH] Added getTranslations method --- .../moviejukebox/themoviedb/TheMovieDb.java | 594 +++++++++--------- .../themoviedb/model/Category.java | 2 +- .../themoviedb/model/Language.java | 65 ++ .../themoviedb/tools/MovieDbParser.java | 445 +++++++------ 4 files changed, 627 insertions(+), 479 deletions(-) create mode 100644 themoviedbapi/src/com/moviejukebox/themoviedb/model/Language.java diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java b/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java index 5dd7be1cf..f27eb3caa 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java @@ -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 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 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; - } + if (!isValidString(searchTerm)) { + return url; + } - public void setLogger(Logger logger) { - if (logger == null) { - return; + String encodedSearchTerm; + + try { + encodedSearchTerm = URLEncoder.encode(searchTerm, "UTF-8"); + } catch (UnsupportedEncodingException e) { + encodedSearchTerm = searchTerm; } - TheMovieDb.logger = logger; - tmdbConsoleHandler.setFormatter(tmdbFormatter); - tmdbConsoleHandler.setLevel(Level.FINE); - logger.addHandler(tmdbConsoleHandler); - logger.setUseParentHandlers(false); - logger.setLevel(Level.ALL); + 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 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 moviedbSearch(String movieTitle, String language) { - // If the title is null, then exit - if (!isValidString(movieTitle)) { - return new ArrayList(); - } - - 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 rating, - * release or title - * @param order how results are ordered. Either asc or - * desc - * @param language the two digit language code. E.g. en=English - * @return a list of MovieDB objects - */ - public List moviedbBrowse(String orderBy, String order, String language) { - return this.moviedbBrowse(orderBy, order, new HashMap(), language); + public List 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 rating, + * release or title + * @param order how results are ordered. Either asc or + * desc + * @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 moviedbBrowse(String orderBy, String order, String language) { + return this.moviedbBrowse(orderBy, order, new HashMap(), 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.
- * 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 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.
+ * 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 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 moviedbSearch(String movieTitle, String language) { + // If the title is null, then exit + if (!isValidString(movieTitle)) { + return new ArrayList(); } - String searchUrl = buildUrl(PERSON_SEARCH, personName, language); - return MovieDbParser.parsePersonInfo(searchUrl); + String searchUrl = buildUrl(MOVIE_SEARCH, movieTitle, language); + return MovieDbParser.parseMovies(searchUrl); } /** @@ -423,30 +537,6 @@ 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 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 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 * 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 * @return */ - public List getCategories(String language) { - return MovieDbParser.parseCategories(this.buildUrl(GENRES_GET_LIST, "", language)); - } - - /** - * 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 movieList, String title, String year) { - if ((movieList == null) || (movieList.isEmpty()) || (!isValidString(title))) { - return null; + public Person personGetVersion(String personID, String language) { + Person person = new Person(); + if (!isValidString(personID)) { + return person; } - for (MovieDB moviedb : movieList) { - if (compareMovies(moviedb, title, year)) { - return moviedb; - } + List people = this.personGetVersion(Arrays.asList(personID), language); + if (people.isEmpty()) { + return person; } - return null; + return people.get(0); } /** - * 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. + * 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 static boolean compareMovies(MovieDB moviedb, String title, String year) { - if ((moviedb == null) || (!isValidString(title))) { - return false; + public Person personSearch(String personName, String language) { + if (!isValidString(personName)) { + 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 - if (moviedb.getAlternativeName().equalsIgnoreCase(title)) { - return true; - } - } - return false; + String searchUrl = buildUrl(PERSON_SEARCH, personName, language); + return MovieDbParser.parsePersonInfo(searchUrl); } /** - * 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 + * Set the TMDb API key. + * @param apiKey a valid TMDb API key. */ - 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; - } + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + tmdbFormatter.addApiKey(apiKey); + } - if (prefix.equals(MOVIE_BROWSE)) { - url += "?"; - } else { - url += "/"; + public void setLogger(Logger logger) { + if (logger == null) { + return; } - url += encodedSearchTerm; - - logger.finest("Search URL: " + url); - return url; + TheMovieDb.logger = logger; + tmdbConsoleHandler.setFormatter(tmdbFormatter); + tmdbConsoleHandler.setLevel(Level.FINE); + logger.addHandler(tmdbConsoleHandler); + logger.setUseParentHandlers(false); + logger.setLevel(Level.ALL); } /** - * Build comma separated ids for Movie.getLatest and Movie.getVersion. - * @param ids a List of ids - * @return + * Set proxy parameters. + * @param host proxy host URL + * @param port proxy port + * @param username proxy username + * @param password proxy password */ - private String buildIds(List 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; + public void setProxy(String host, String port, String username, String password) { + WebBrowser.setProxyHost(host); + WebBrowser.setProxyPort(port); + WebBrowser.setProxyUsername(username); + WebBrowser.setProxyPassword(password); } /** - * 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 + * Set web browser timeout. + * @param webTimeoutConnect + * @param webTimeoutRead */ - 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); } } diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Category.java b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Category.java index aaf589d1b..7cf867642 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Category.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Category.java @@ -13,7 +13,7 @@ package com.moviejukebox.themoviedb.model; /** - * Category from the MovieDB.org + * Category from TheMovieDB.org * * @author Stuart.Boston * diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Language.java b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Language.java new file mode 100644 index 000000000..2994b9cde --- /dev/null +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Language.java @@ -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; + } + + +} diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/tools/MovieDbParser.java b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/MovieDbParser.java index 14eacfa24..4249428b2 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/tools/MovieDbParser.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/MovieDbParser.java @@ -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,61 +36,91 @@ 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 parseMovies(String searchUrl) { - List movies = new ArrayList(); - + public static List parseCategories(String searchUrl) { Document doc = null; + List categories = new ArrayList(); try { doc = DOMHelper.getEventDocFromUrl(searchUrl); } catch (Exception error) { - logger.severe("TheMovieDb Error: " + error.getMessage()); - return movies; + return categories; } 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) { - return movies; + 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); + } } - MovieDB movie = null; + return categories; + } - 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); - } + public static List parseLanguages(String url) { + List languages = new ArrayList(); + 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 movies; + + return languages; } /** - * Returns the first MovieDB from the DOM Document. - * @param doc a DOM Document + * 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 parseMovie(String searchUrl) { + 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()); + logger.severe("GetLatest error: " + error.getMessage()); return movie; } @@ -98,27 +129,30 @@ public class MovieDbParser { } NodeList nlMovies = doc.getElementsByTagName("movie"); + if ((nlMovies == null) || nlMovies.getLength() == 0) { return movie; } - Node nMovie = nlMovies.item(0); - if (nMovie.getNodeType() == Node.ELEMENT_NODE) { - Element eMovie = (Element) nMovie; - movie = parseMovieInfo(eMovie); + 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 Person parsePersonInfo(String searchUrl) { - Person person = null; + public static Person parseLatestPerson(String url) { + Person person = new Person(); Document doc = null; try { - doc = DOMHelper.getEventDocFromUrl(searchUrl); + doc = DOMHelper.getEventDocFromUrl(url); } catch (Exception error) { - logger.severe("PersonSearch error: " + error.getMessage()); + logger.severe("Person.getLatest error: " + error.getMessage()); return person; } @@ -126,69 +160,87 @@ public class MovieDbParser { 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(); - NodeList personNodeList = doc.getElementsByTagName("person"); - // Only get the first movie from the list - Node personNode = personNodeList.item(0); + Element element = (Element) node; + 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")); - 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")); + try { + doc = DOMHelper.getEventDocFromUrl(searchUrl); + } catch (Exception error) { + logger.severe("TheMovieDb Error: " + error.getMessage()); + return movie; + } - 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); - } - } + if (doc == null) { + return 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(); + NodeList nlMovies = doc.getElementsByTagName("movie"); + if ((nlMovies == null) || nlMovies.getLength() == 0) { + return movie; + } - 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")); + Node nMovie = nlMovies.item(0); + if (nMovie.getNodeType() == Node.ELEMENT_NODE) { + Element eMovie = (Element) nMovie; + movie = parseMovieInfo(eMovie); + } - person.addFilm(film); - } - } + return movie; + } + + public static List parseMovieGetVersion(String url) { + List movies = new ArrayList(); + 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) { @@ -411,183 +463,181 @@ public class MovieDbParser { } /** - * Parse a DOM document and returns a list of Person - * @param doc a DOM document + * 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 parsePersonGetVersion(String searchUrl) { - List people = new ArrayList(); + public static List parseMovies(String searchUrl) { + List movies = new ArrayList(); + Document doc = null; try { doc = DOMHelper.getEventDocFromUrl(searchUrl); } catch (Exception error) { - logger.severe("PersonGetVersion error: " + error.getMessage()); - return people; + logger.severe("TheMovieDb Error: " + error.getMessage()); + return movies; } if (doc == null) { - return people; + return movies; } - NodeList movies = doc.getElementsByTagName("movie"); - if ((movies == null) || movies.getLength() == 0) { - return people; + NodeList nlMovies = doc.getElementsByTagName("movie"); + + if ((nlMovies == null) || nlMovies.getLength() == 0) { + return movies; } - for (int i = 0; i < movies.getLength(); i++) { - Node node = movies.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - Element element = (Element) node; - people.add(MovieDbParser.parseSimplePerson(element)); + 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 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 * @return */ - public static List parseCategories(String searchUrl) { + public static List parsePersonGetVersion(String searchUrl) { + List people = new ArrayList(); Document doc = null; - List categories = new ArrayList(); try { doc = DOMHelper.getEventDocFromUrl(searchUrl); } catch (Exception error) { - return categories; + logger.severe("PersonGetVersion error: " + error.getMessage()); + return people; } if (doc == null) { - return categories; + return people; } - NodeList genres = doc.getElementsByTagName("genre"); - if ((genres == null) || genres.getLength() == 0) { - return categories; + NodeList movies = doc.getElementsByTagName("movie"); + if ((movies == null) || movies.getLength() == 0) { + return people; } - for (int i = 0; i < genres.getLength(); i++) { - Node node = genres.item(i); + for (int i = 0; i < movies.getLength(); i++) { + Node node = movies.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); + people.add(MovieDbParser.parseSimplePerson(element)); } } - return categories; + return people; } - /** - * 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; - } - - 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 person; } - return movie; - } - - public static List parseMovieGetVersion(String url) { - List movies = new ArrayList(); - 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"); + person = new Person(); + NodeList personNodeList = doc.getElementsByTagName("person"); - if ((nlMovies == null) || nlMovies.getLength() == 0) { - return movies; - } + // Only get the first movie from the list + Node personNode = personNodeList.item(0); - 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)); + if (personNode == null) { + logger.finest("Person not found"); + return person; } - } - - 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 (personNode.getNodeType() == Node.ELEMENT_NODE) { + Element personElement = (Element) personNode; - if (doc == null) { - return person; - } + 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 nlMovies = doc.getElementsByTagName("person"); + 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); + } + } - if ((nlMovies == null) || nlMovies.getLength() == 0) { - return person; - } + 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(); - Node node = nlMovies.item(0); - if (node.getNodeType() == Node.ELEMENT_NODE) { - person = new Person(); + 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")); - Element element = (Element) node; - person = MovieDbParser.parseSimplePerson(element); + person.addFilm(film); + } + } + } + } catch (Exception error) { + logger.severe("ERROR: " + error.getMessage()); + error.printStackTrace(); } return person; } + /** + * Parse a "simple" Language in the form: + * + * English + * English + * + * @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: * @@ -609,7 +659,7 @@ public class MovieDbParser { movie.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at")); return movie; } - + /** * Parse a "simple" Person in the form: * @@ -629,4 +679,5 @@ public class MovieDbParser { person.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at")); return person; } + }