implemented Movie.getLatest

renamed method buildSearchUrl to buildUrl
master
Mohammed Le Doze 15 years ago
parent 1b7dbee8df
commit 909a494ede

@ -53,6 +53,7 @@ public class TheMovieDb {
private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup"; private static final String MOVIE_IMDB_LOOKUP = "Movie.imdbLookup";
private static final String MOVIE_GET_INFO = "Movie.getInfo"; 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_LATEST = "Movie.getLatest";
private static final String PERSON_GET_VERSION = "Person.getVersion"; private static final String PERSON_GET_VERSION = "Person.getVersion";
private static final String PERSON_GET_INFO = "Person.getInfo"; private static final String PERSON_GET_INFO = "Person.getInfo";
private static final String PERSON_SEARCH = "Person.search"; private static final String PERSON_SEARCH = "Person.search";
@ -122,7 +123,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(MOVIE_SEARCH, URLEncoder.encode(movieTitle, "UTF-8"), language); String searchUrl = buildUrl(MOVIE_SEARCH, URLEncoder.encode(movieTitle, "UTF-8"), language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
movies = DOMParser.parseMovies(doc); movies = DOMParser.parseMovies(doc);
} catch (Exception error) { } catch (Exception error) {
@ -192,7 +193,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
String searchUrl = buildSearchUrl(MOVIE_BROWSE, url, language); String searchUrl = buildUrl(MOVIE_BROWSE, url, language);
try { try {
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
} catch (Exception error) { } catch (Exception error) {
@ -220,7 +221,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(MOVIE_IMDB_LOOKUP, imdbID, language); String searchUrl = buildUrl(MOVIE_IMDB_LOOKUP, imdbID, language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
movie = DOMParser.parseMovie(doc); movie = DOMParser.parseMovie(doc);
@ -263,12 +264,12 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(MOVIE_GET_INFO, tmdbID, language); String searchUrl = buildUrl(MOVIE_GET_INFO, tmdbID, language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
if (doc == null && !language.equalsIgnoreCase(defaultLanguage)) { if (doc == null && !language.equalsIgnoreCase(defaultLanguage)) {
logger.fine("Trying to get the '" + defaultLanguage + "' version"); logger.fine("Trying to get the '" + defaultLanguage + "' version");
searchUrl = buildSearchUrl(MOVIE_GET_INFO, tmdbID, defaultLanguage); searchUrl = buildUrl(MOVIE_GET_INFO, tmdbID, defaultLanguage);
} }
if (doc == null) { if (doc == null) {
@ -283,6 +284,28 @@ public class TheMovieDb {
return movie; 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
* the database and want to know which id to stop at. <br/>
* The MovieDB object returned only has its title, TMDb id and IMDB id
* initialized.
* @param language the two digit language code. E.g. en=English
* @return
*/
public MovieDB moviedbGetLatest(String language) {
Document doc = null;
MovieDB movie = new MovieDB();
try {
String url = buildUrl(MOVIE_GET_LATEST, "", language);
doc = DOMHelper.getEventDocFromUrl(url);
movie = DOMParser.parseLatestMovie(doc);
} catch (Exception error) {
logger.severe("GetLatest error: " + error.getMessage());
}
return movie;
}
public MovieDB moviedbGetImages(String searchTerm, String language) { public MovieDB moviedbGetImages(String searchTerm, String language) {
MovieDB movie = null; MovieDB movie = null;
movie = moviedbGetImages(searchTerm, movie, language); movie = moviedbGetImages(searchTerm, movie, language);
@ -305,7 +328,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(MOVIE_GET_IMAGES, searchTerm, language); String searchUrl = buildUrl(MOVIE_GET_IMAGES, searchTerm, language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
movie = DOMParser.parseMovie(doc); movie = DOMParser.parseMovie(doc);
@ -334,7 +357,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(PERSON_SEARCH, personName, language); String searchUrl = buildUrl(PERSON_SEARCH, personName, language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
person = DOMParser.parsePersonInfo(doc); person = DOMParser.parsePersonInfo(doc);
} catch (Exception error) { } catch (Exception error) {
@ -361,7 +384,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(PERSON_GET_INFO, personID, language); String searchUrl = buildUrl(PERSON_GET_INFO, personID, language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
person = DOMParser.parsePersonInfo(doc); person = DOMParser.parsePersonInfo(doc);
} catch (Exception error) { } catch (Exception error) {
@ -385,10 +408,7 @@ public class TheMovieDb {
return new Person(); return new Person();
} }
List<Person> people = new ArrayList<Person>(); return this.personGetVersion(Arrays.asList(personID), language).get(0);
people = this.personGetVersion(Arrays.asList(personID), language);
return people.get(0);
} }
/** /**
@ -400,6 +420,11 @@ public class TheMovieDb {
public List<Person> personGetVersion(List<String> personIDs, String language) { public List<Person> personGetVersion(List<String> personIDs, String language) {
List<Person> people = new ArrayList<Person>(); List<Person> people = new ArrayList<Person>();
if (personIDs.isEmpty()) {
logger.warning("There are no Person ids!");
return people;
}
String ids = ""; String ids = "";
for (int i = 0; i < personIDs.size(); i++) { for (int i = 0; i < personIDs.size(); i++) {
if (i == 0) { if (i == 0) {
@ -412,7 +437,7 @@ public class TheMovieDb {
Document doc = null; Document doc = null;
try { try {
String searchUrl = buildSearchUrl(PERSON_GET_VERSION, ids, language); String searchUrl = buildUrl(PERSON_GET_VERSION, ids, language);
doc = DOMHelper.getEventDocFromUrl(searchUrl); doc = DOMHelper.getEventDocFromUrl(searchUrl);
people = DOMParser.parsePersonGetVersion(doc); people = DOMParser.parsePersonGetVersion(doc);
} catch (Exception error) { } catch (Exception error) {
@ -430,7 +455,7 @@ public class TheMovieDb {
public List<Category> getCategories(String language) { public List<Category> getCategories(String language) {
List<Category> categories = new ArrayList<Category>(); List<Category> categories = new ArrayList<Category>();
Document doc = null; Document doc = null;
String url = this.buildSearchUrl(GENRES_GET_LIST, "", language); String url = this.buildUrl(GENRES_GET_LIST, "", language);
try { try {
doc = DOMHelper.getEventDocFromUrl(url); doc = DOMHelper.getEventDocFromUrl(url);
@ -483,11 +508,11 @@ public class TheMovieDb {
if (moviedb.getOriginalName().equalsIgnoreCase(title)) { if (moviedb.getOriginalName().equalsIgnoreCase(title)) {
return true; return true;
} }
if (moviedb.getTitle().equalsIgnoreCase(title)) { if (moviedb.getTitle().equalsIgnoreCase(title)) {
return true; return true;
} }
// Try matching the alternative name too // Try matching the alternative name too
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) { if (moviedb.getAlternativeName().equalsIgnoreCase(title)) {
return true; return true;
@ -499,11 +524,11 @@ public class TheMovieDb {
if (moviedb.getOriginalName().equalsIgnoreCase(title)) { if (moviedb.getOriginalName().equalsIgnoreCase(title)) {
return true; return true;
} }
if (moviedb.getTitle().equalsIgnoreCase(title)) { if (moviedb.getTitle().equalsIgnoreCase(title)) {
return true; return true;
} }
// Try matching the alternative name too // Try matching the alternative name too
if (moviedb.getAlternativeName().equalsIgnoreCase(title)) { if (moviedb.getAlternativeName().equalsIgnoreCase(title)) {
return true; return true;
@ -513,24 +538,26 @@ public class TheMovieDb {
} }
/** /**
* Build the search URL from the search prefix and movie title. * Build the URL that is used to get the XML from TMDb.
* This will change between v2.0 and v2.1 of the API
* *
* @param prefix The search prefix before the movie title * @param prefix The search prefix before the movie title
* @param language The two digit language code. E.g. en=English * @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 * @param searchTerm The search key to use, e.g. movie title or IMDb ID
* @return The search URL * @return The search URL
*/ */
private String buildSearchUrl(String prefix, String searchTerm, String language) { private String buildUrl(String prefix, String searchTerm, String language) {
String searchUrl = apiSite + prefix + "/" + language + "/xml/" + apiKey; String url = apiSite + prefix + "/" + language + "/xml/" + apiKey;
if (searchTerm.equals("")) {
return url;
}
if (prefix.equals(MOVIE_BROWSE)) { if (prefix.equals(MOVIE_BROWSE)) {
searchUrl += "?"; url += "?";
} else if (!prefix.equals(GENRES_GET_LIST)) { } else {
searchUrl += "/"; url += "/";
} }
searchUrl += searchTerm; url += searchTerm;
logger.finest("Search URL: " + searchUrl); logger.finest("Search URL: " + url);
return searchUrl; return url;
} }
/** /**

@ -378,13 +378,13 @@ public class DOMParser {
public static List<Person> parsePersonGetVersion(Document doc) { public static List<Person> parsePersonGetVersion(Document doc) {
List<Person> people = new ArrayList<Person>(); List<Person> people = new ArrayList<Person>();
NodeList movies = doc.getElementsByTagName("movie"); NodeList movies = doc.getElementsByTagName("movie");
if( (movies == null) || movies.getLength() == 0) { if ((movies == null) || movies.getLength() == 0) {
return people; return people;
} }
for (int i= 0; i < movies.getLength(); i++) { for (int i = 0; i < movies.getLength(); i++) {
Node node = movies.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;
Person person = new Person(); Person person = new Person();
person.setName(DOMHelper.getValueFromElement(element, "name")); person.setName(DOMHelper.getValueFromElement(element, "name"));
@ -406,11 +406,11 @@ public class DOMParser {
public static List<Category> parseCategories(Document doc) { public static List<Category> parseCategories(Document doc) {
List<Category> categories = new ArrayList<Category>(); List<Category> categories = new ArrayList<Category>();
NodeList genres = doc.getElementsByTagName("genre"); NodeList genres = doc.getElementsByTagName("genre");
if( (genres == null) || genres.getLength() == 0) { if ((genres == null) || genres.getLength() == 0) {
return categories; return categories;
} }
for (int i= 0; i < genres.getLength(); i++) { for (int i = 0; i < genres.getLength(); i++) {
Node node = genres.item(i); Node node = genres.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node; Element element = (Element) node;
@ -425,4 +425,30 @@ public class DOMParser {
return categories; return categories;
} }
/**
* Parse a DOM document and returns the latest Movie.
* @param doc
* @return
*/
public static MovieDB parseLatestMovie(Document doc) {
MovieDB movie = new MovieDB();
NodeList movies = doc.getElementsByTagName("movie");
if ((movies == null) || movies.getLength() == 0) {
return movie;
}
Node node = movies.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
movie.setTitle(DOMHelper.getValueFromElement(element, "name"));
movie.setId(DOMHelper.getValueFromElement(element, "id"));
movie.setImdb(DOMHelper.getValueFromElement(element, "imdb_id"));
// to be done:
//movie.setVersion(DOMHelper.getValueFromElement(element, "version"));
//movie.setLastModifiedAt(DOMHelper.getValueFromElement(element, "last_modified_at"));
}
return movie;
}
} }

Loading…
Cancel
Save