added getCategories in TheMovieDb to retrieve the list of genres
This commit is contained in:
@@ -58,6 +58,7 @@ public class TheMovieDb {
|
||||
private static final String PERSON_GET_VERSION = "Person.getVersion";
|
||||
private static final String PERSON_GET_INFO = "Person.getInfo";
|
||||
private static final String PERSON_SEARCH = "Person.search";
|
||||
private static final String GENRES_GET_LIST = "Genres.getList";
|
||||
|
||||
public TheMovieDb(String apiKey) {
|
||||
setLogger(Logger.getLogger("TheMovieDB"));
|
||||
@@ -400,6 +401,24 @@ public class TheMovieDb {
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of valid genres within TMDb.
|
||||
* @param language the two digit language code. E.g. en=English
|
||||
* @return
|
||||
*/
|
||||
public List<Category> getCategories(String language) {
|
||||
List<Category> categories = new ArrayList<Category>();
|
||||
Document doc = null;
|
||||
String url = this.buildSearchUrl(GENRES_GET_LIST, "", language);
|
||||
try {
|
||||
doc = DOMHelper.getEventDocFromUrl(url);
|
||||
categories = DOMParser.parseCategories(doc);
|
||||
} catch (Exception error) {
|
||||
logger.severe("Get categories error: " + error.getMessage());
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search a list of movies and return the one that matches the title & year
|
||||
|
||||
@@ -374,4 +374,32 @@ public class DOMParser {
|
||||
}
|
||||
return movie;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of valid genres within TMDb.
|
||||
* @param doc a DOM document
|
||||
* @return
|
||||
*/
|
||||
public static List<Category> parseCategories(Document doc) {
|
||||
List<Category> categories = new ArrayList<Category>();
|
||||
NodeList genres = doc.getElementsByTagName("genres");
|
||||
if( (genres == null) || genres.getLength() == 0) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
for (int i= 0; i < genres.getLength(); i++) {
|
||||
Node node = genres.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
Category category = new Category();
|
||||
category.setId(element.getAttribute("id"));
|
||||
category.setName(DOMHelper.getValueFromElement(element, "name"));
|
||||
category.setType(DOMHelper.getValueFromElement(element, "type"));
|
||||
category.setUrl(DOMHelper.getValueFromElement(element, "url"));
|
||||
categories.add(category);
|
||||
}
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user