diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java b/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java index fcdcbe96d..afe38f2dd 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/TheMovieDb.java @@ -13,30 +13,17 @@ package com.moviejukebox.themoviedb; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; import java.net.URLEncoder; import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; -import com.moviejukebox.themoviedb.model.Artwork; -import com.moviejukebox.themoviedb.model.Category; -import com.moviejukebox.themoviedb.model.Country; import com.moviejukebox.themoviedb.model.MovieDB; import com.moviejukebox.themoviedb.model.Person; +import com.moviejukebox.themoviedb.tools.DOMHelper; +import com.moviejukebox.themoviedb.tools.DOMParser; import com.moviejukebox.themoviedb.tools.LogFormatter; /** @@ -44,7 +31,7 @@ import com.moviejukebox.themoviedb.tools.LogFormatter; * of the API as detailed here http://api.themoviedb.org/2.1/docs/ * * @author Stuart.Boston - * @version 1.1 + * @version 1.3 */ public class TheMovieDb { @@ -52,21 +39,41 @@ public class TheMovieDb { private static String apiSite = "http://api.themoviedb.org/2.1/"; private static String defaultLanguage = "en"; private static Logger logger; + private static LogFormatter tmdbFormatter = new LogFormatter(); + private static ConsoleHandler tmdbConsoleHandler = new ConsoleHandler(); + + public TheMovieDb(String apiKey) { + setLogger(Logger.getLogger("TheMovieDB")); + setApiKey(apiKey); + } + + public TheMovieDb(String apiKey, Logger logger) { + setLogger(logger); + setApiKey(apiKey); + } - public TheMovieDb(String apiKey) { - logger = Logger.getLogger("TheMovieDB"); - LogFormatter mjbFormatter = new LogFormatter(); - ConsoleHandler ch = new ConsoleHandler(); - ch.setFormatter(mjbFormatter); - ch.setLevel(Level.FINE); - logger.addHandler(ch); + public static Logger getLogger() { + return logger; + } + + public static void setLogger(Logger logger) { + TheMovieDb.logger = logger; + tmdbConsoleHandler.setFormatter(tmdbFormatter); + tmdbConsoleHandler.setLevel(Level.FINE); + logger.addHandler(tmdbConsoleHandler); logger.setUseParentHandlers(true); logger.setLevel(Level.ALL); - + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { this.apiKey = apiKey; - mjbFormatter.addApiKey(apiKey); + tmdbFormatter.addApiKey(apiKey); } - + /** * Build the search URL from the search prefix and movie title. * This will change between v2.0 and v2.1 of the API @@ -101,8 +108,8 @@ public class TheMovieDb { try { String searchUrl = buildSearchUrl("Movie.search", URLEncoder.encode(movieTitle, "UTF-8"), language); - doc = getEventDocFromUrl(searchUrl); - movie = parseMovieInfo(doc); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + movie = DOMParser.parseMovieInfo(doc); } catch (Exception error) { logger.severe("ERROR: " + error.getMessage()); @@ -130,8 +137,8 @@ public class TheMovieDb { try { String searchUrl = buildSearchUrl("Movie.imdbLookup", imdbID, language); - doc = getEventDocFromUrl(searchUrl); - movie = parseMovieInfo(doc); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + movie = DOMParser.parseMovieInfo(doc); } catch (Exception error) { logger.severe("ERROR: " + error.getMessage()); @@ -175,8 +182,8 @@ public class TheMovieDb { try { String searchUrl = buildSearchUrl("Movie.getImages", tmdbID, language); - doc = getEventDocFromUrl(searchUrl); - movie = parseMovieInfo(doc); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + movie = DOMParser.parseMovieInfo(doc); } catch (Exception error) { logger.severe("ERROR: " + error.getMessage()); @@ -209,8 +216,8 @@ public class TheMovieDb { try { String searchUrl = buildSearchUrl("Movie.getImages", searchTerm, language); - doc = getEventDocFromUrl(searchUrl); - movie = parseMovieInfo(doc); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + movie = DOMParser.parseMovieInfo(doc); } catch (Exception error) { logger.severe("ERROR: " + error.getMessage()); @@ -234,212 +241,94 @@ public class TheMovieDb { return language; } - public MovieDB parseMovieInfo(Document doc) { - // Borrowed from http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html - MovieDB movie = null; - NodeList movieNodeList, subNodeList; - Node movieNode, subNode; - Element movieElement, subElement; + + /** + * 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 Person personSearch(String personName, String language) { + Person person = new Person(); + Document doc = null; + + language = validateLanguage(language); + + if (personName == null || personName.equals("")) { + return person; + } try { - movie = new MovieDB(); - movieNodeList = doc.getElementsByTagName("movie"); - - // Only get the first movie from the list - movieNode = movieNodeList.item(0); - - if (movieNode == null) { - logger.finest("Movie not found"); - return movie; - } - - if (movieNode.getNodeType() == Node.ELEMENT_NODE) { - movieElement = (Element) movieNode; - - movie.setTitle(getValueFromElement(movieElement, "name")); - movie.setPopularity(getValueFromElement(movieElement, "popularity")); - movie.setType(getValueFromElement(movieElement, "type")); - movie.setId(getValueFromElement(movieElement, "id")); - movie.setImdb(getValueFromElement(movieElement, "imdb_id")); - movie.setUrl(getValueFromElement(movieElement, "url")); - movie.setOverview(getValueFromElement(movieElement, "overview")); - movie.setRating(getValueFromElement(movieElement, "rating")); - movie.setReleaseDate(getValueFromElement(movieElement, "released")); - movie.setRuntime(getValueFromElement(movieElement, "runtime")); - movie.setBudget(getValueFromElement(movieElement, "budget")); - movie.setRevenue(getValueFromElement(movieElement, "revenue")); - movie.setHomepage(getValueFromElement(movieElement, "homepage")); - movie.setTrailer(getValueFromElement(movieElement, "trailer")); - - // Process the "categories" - subNodeList = doc.getElementsByTagName("categories"); - - for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { - subNode = subNodeList.item(nodeLoop); - if (subNode.getNodeType() == Node.ELEMENT_NODE) { - subElement = (Element) subNode; - Category category = new Category(); - - category.setType(getValueFromElement(subElement, "type")); - category.setUrl(getValueFromElement(subElement, "url")); - category.setName(getValueFromElement(subElement, "name")); - - movie.addCategory(category); - } - } - - // Process the "countries" - subNodeList = doc.getElementsByTagName("countries"); - - for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { - subNode = subNodeList.item(nodeLoop); - if (subNode.getNodeType() == Node.ELEMENT_NODE) { - subElement = (Element) subNode; - Country country = new Country(); - - country.setCode(getValueFromElement(subElement, "code")); - country.setUrl(getValueFromElement(subElement, "url")); - country.setName(getValueFromElement(subElement, "name")); - - movie.addProductionCountry(country); - } - } - - // Process the "cast" - subNodeList = doc.getElementsByTagName("cast"); - - for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { - subNode = subNodeList.item(nodeLoop); - if (subNode.getNodeType() == Node.ELEMENT_NODE) { - subElement = (Element) subNode; - Person person = new Person(); - - person.setUrl(getValueFromElement(subElement, "url")); - person.setName(getValueFromElement(subElement, "name")); - person.setJob(getValueFromElement(subElement, "job")); - person.setCharacter(getValueFromElement(subElement, "character")); - person.setId(getValueFromElement(subElement, "id")); - - movie.addPerson(person); - } - } - - /* - * This processes the image elements. There are two formats to deal with: - * Movie.imdbLookup, Movie.getInfo & Movie.search: - * - * - * - * - * - * Movie.getImages: - * - * - * - * - * - * - * - * - * - * - * - * - * - */ - subNodeList = doc.getElementsByTagName("images"); - - for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { - subNode = subNodeList.item(nodeLoop); - - if (subNode.getNodeType() == Node.ELEMENT_NODE) { - - NodeList artworkNodeList = subNode.getChildNodes(); - for (int artworkLoop = 0; artworkLoop < artworkNodeList.getLength(); artworkLoop++) { - Node artworkNode = artworkNodeList.item(artworkLoop); - if (artworkNode.getNodeType() == Node.ELEMENT_NODE) { - subElement = (Element) artworkNode; - - if (subElement.getNodeName().equalsIgnoreCase("image")) { - // This is the format used in Movie.imdbLookup, Movie.getInfo & Movie.search - Artwork artwork = new Artwork(); - artwork.setType(subElement.getAttribute("type")); - artwork.setSize(subElement.getAttribute("size")); - artwork.setUrl(subElement.getAttribute("url")); - artwork.setId(subElement.getAttribute("id")); - movie.addArtwork(artwork); - } else if (subElement.getNodeName().equalsIgnoreCase("backdrop") || - subElement.getNodeName().equalsIgnoreCase("poster")) { - // This is the format used in Movie.getImages - String artworkId = subElement.getAttribute("id"); - String artworkType = subElement.getNodeName(); - - // We need to decode and loop round the child nodes to get the data - NodeList imageNodeList = subElement.getChildNodes(); - for (int imageLoop = 0; imageLoop < imageNodeList.getLength(); imageLoop++) { - Node imageNode = imageNodeList.item(imageLoop); - if (imageNode.getNodeType() == Node.ELEMENT_NODE) { - Element imageElement = (Element) imageNode; - Artwork artwork = new Artwork(); - artwork.setId(artworkId); - artwork.setType(artworkType); - artwork.setUrl(imageElement.getAttribute("url")); - artwork.setSize(imageElement.getAttribute("size")); - movie.addArtwork(artwork); - } - } - } else { - // This is a classic, it should never happen error - logger.severe("UNKNOWN Image type"); - } - } - } - } - } - } + String searchUrl = buildSearchUrl("Person.search", personName, language); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + person = DOMParser.parsePersonInfo(doc); } catch (Exception error) { logger.severe("ERROR: " + error.getMessage()); - error.printStackTrace(); } - return movie; + + return person; } - + /** - * Gets the string value of the tag element name passed - * @param element - * @param tagName + * The Person.getInfo method is used to retrieve the full filmography, known movies, + * images and things like birthplace for a specific person in the TMDb database. + * + * @param personID + * @param language * @return */ - private String getValueFromElement(Element element, String tagName) { - String returnValue = ""; + public Person personGetInfo(String personID, String language) { + Person person = new Person(); + Document doc = null; + + language = validateLanguage(language); + + if (personID == null || personID.equals("")) { + return person; + } try { - NodeList elementNodeList = element.getElementsByTagName(tagName); - Element tagElement = (Element) elementNodeList.item(0); - NodeList tagNodeList = tagElement.getChildNodes(); - returnValue = ((Node) tagNodeList.item(0)).getNodeValue(); - } catch (Exception ignore) { - return returnValue; + String searchUrl = buildSearchUrl("Person.getInfo", personID, language); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + person = DOMParser.parsePersonInfo(doc); + } catch (Exception error) { + logger.severe("ERROR: " + error.getMessage()); } - return returnValue; + return person; } - + /** - * Get a DOM document from the supplied URL - * @param url + * 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 + * @param language * @return - * @throws MalformedURLException - * @throws IOException - * @throws ParserConfigurationException - * @throws SAXException */ - public static Document getEventDocFromUrl(String url) throws MalformedURLException, IOException, ParserConfigurationException, SAXException { - InputStream in = (new URL(url)).openStream(); - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(in); - doc.getDocumentElement().normalize(); - return doc; + public Person personGetVersion(String personID, String language) { + Person person = new Person(); + Document doc = null; + + language = validateLanguage(language); + + if (personID == null || personID.equals("")) { + return person; + } + + try { + String searchUrl = buildSearchUrl("Person.getVersion", personID, language); + doc = DOMHelper.getEventDocFromUrl(searchUrl); + person = DOMParser.parsePersonGetVersion(doc); + } catch (Exception error) { + logger.severe("ERROR: " + error.getMessage()); + } + + return person; } + + } diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Artwork.java b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Artwork.java index e79e77d95..95d32f2ed 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Artwork.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Artwork.java @@ -22,14 +22,16 @@ package com.moviejukebox.themoviedb.model; public class Artwork implements Comparable { public static String ARTWORK_TYPE_POSTER = "poster"; public static String ARTWORK_TYPE_BACKDROP = "backdrop"; - public static String[] ARTWORK_TYPES = {ARTWORK_TYPE_POSTER, ARTWORK_TYPE_BACKDROP}; + public static String ARTWORK_TYPE_PERSON = "profile"; + public static String[] ARTWORK_TYPES = {ARTWORK_TYPE_POSTER, ARTWORK_TYPE_BACKDROP, ARTWORK_TYPE_PERSON}; public static String ARTWORK_SIZE_ORIGINAL = "original"; public static String ARTWORK_SIZE_THUMB = "thumb"; public static String ARTWORK_SIZE_MID = "mid"; public static String ARTWORK_SIZE_COVER = "cover"; public static String ARTWORK_SIZE_POSTER = "poster"; - public static String[] ARTWORK_SIZES = {ARTWORK_SIZE_ORIGINAL, ARTWORK_SIZE_THUMB, ARTWORK_SIZE_MID, ARTWORK_SIZE_COVER, ARTWORK_SIZE_POSTER}; + public static String ARTWORK_SIZE_PROFILE = "profile"; + public static String[] ARTWORK_SIZES = {ARTWORK_SIZE_ORIGINAL, ARTWORK_SIZE_THUMB, ARTWORK_SIZE_MID, ARTWORK_SIZE_COVER, ARTWORK_SIZE_POSTER, ARTWORK_SIZE_PROFILE}; public String type; public String size; diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Filmography.java b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Filmography.java new file mode 100644 index 000000000..6237d1b83 --- /dev/null +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Filmography.java @@ -0,0 +1,71 @@ +/* + * 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; + +public class Filmography { + private String url; + private String name; + private String department; + private String character; + private String job; + private String id; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public String getCharacter() { + return character; + } + + public void setCharacter(String character) { + this.character = character; + } + + public String getJob() { + return job; + } + + public void setJob(String job) { + this.job = job; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +} diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/model/MovieDB.java b/themoviedbapi/src/com/moviejukebox/themoviedb/model/MovieDB.java index a22ab7642..63ef59e2b 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/model/MovieDB.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/model/MovieDB.java @@ -14,16 +14,17 @@ package com.moviejukebox.themoviedb.model; import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import com.moviejukebox.themoviedb.tools.ModelTools; + /** * This is the Movie Search bean for the MovieDb.org search * * @author Stuart.Boston */ -public class MovieDB { +public class MovieDB extends ModelTools { public static String UNKNOWN = "UNKNOWN"; private String score = UNKNOWN; @@ -41,7 +42,6 @@ public class MovieDB { private String revenue = UNKNOWN; private String homepage = UNKNOWN; private String trailer = UNKNOWN; - private List artwork = new ArrayList(); private List countries = new ArrayList(); private List people = new ArrayList(); private List categories = new ArrayList(); @@ -166,59 +166,6 @@ public class MovieDB { this.trailer = trailer; } - /** - * Add a piece of artwork to the artwork array - * @param artworkType must be one of Artwork.ARTWORK_TYPES - * @param artworkSize must be one of Artwork.ARTWORK_SIZES - * @param artworkUrl - * @param posterId - */ - public void addArtwork(String artworkType, String artworkSize, String artworkUrl, String artworkId) { - if (validateElement(Artwork.ARTWORK_TYPES, artworkType) && validateElement(Artwork.ARTWORK_SIZES, artworkSize)) { - Artwork newArtwork = new Artwork(); - - newArtwork.setType(artworkType); - newArtwork.setSize(artworkSize); - newArtwork.setUrl(artworkUrl); - newArtwork.setId(artworkId); - - artwork.add(newArtwork); - Collections.sort(artwork); - } - return; - } - - /** - * Add a piece of artwork to the artwork array - * @param newArtwork an Artwork object to add to the array - */ - public void addArtwork(Artwork newArtwork) { - if (validateElement(Artwork.ARTWORK_TYPES, newArtwork.getType()) && validateElement(Artwork.ARTWORK_SIZES, newArtwork.getSize())) { - artwork.add(newArtwork); - Collections.sort(artwork); - } - return; - } - - /** - * Check to see if element is contained in elementArray - * @param elementArray - * @param element - * @return - */ - private boolean validateElement(String[] elementArray, String element) { - boolean valid = false; - - for (String arrayEntry : elementArray) { - if (arrayEntry.equalsIgnoreCase(element)) { - valid = true; - break; - } - } - - return valid; - } - public List getProductionCountries() { return countries; } @@ -248,103 +195,4 @@ public class MovieDB { categories.add(category); } } - - /** - * Return all the artwork for a movie - * @return - */ - public List getArtwork() { - return artwork; - } - - /** - * Get all the artwork of a specific type - * @param artworkType - * @return - */ - public List getArtwork(String artworkType) { - // Validate the Type and Size arguments - if (!validateElement(Artwork.ARTWORK_TYPES, artworkType)) { - return null; - } - - List artworkList = new ArrayList(); - - for (Artwork singleArtwork : artwork) { - if (singleArtwork.getType().equalsIgnoreCase(artworkType)) { - artworkList.add(singleArtwork); - } - } - - return artworkList; - } - - /** - * Get all artwork of a specific Type and Size - * @param artworkType - * @param artworkSize - * @return - */ - public List getArtwork(String artworkType, String artworkSize) { - List artworkList = new ArrayList(); - // Validate the Type and Size arguments - if (!validateElement(Artwork.ARTWORK_TYPES, artworkType) && !validateElement(Artwork.ARTWORK_SIZES, artworkSize)) { - return null; - } - - for (Artwork singleArtwork : artwork) { - if (singleArtwork.getType().equalsIgnoreCase(artworkType) && singleArtwork.getSize().equalsIgnoreCase(artworkSize)) { - artworkList.add(singleArtwork); - } - } - - return artworkList; - } - - /** - * Return a specific artwork entry for a Type & Size - * @param artworkType - * @param artworkSize - * @param artworkNumber - * @return - */ - public Artwork getArtwork(String artworkType, String artworkSize, int artworkNumber) { - // Validate the Type and Size arguments - if (!validateElement(Artwork.ARTWORK_TYPES, artworkType) && !validateElement(Artwork.ARTWORK_SIZES, artworkSize)) { - return null; - } - - // Validate the number - if (artworkNumber <= 0) { - artworkNumber = 0; - } else { - // Artwork elements start at 0 (Zero) - artworkNumber -= 1; - } - - List artworkList = getArtwork(artworkType, artworkSize); - - int artworkCount = artworkList.size(); - if (artworkCount < 1) { - return null; - } - - // If the number requested is greater than the array size, loop around until it's within scope - while (artworkNumber > artworkCount) { - artworkNumber = artworkNumber - artworkCount; - } - - return artworkList.get(artworkNumber); - } - - /** - * Get the first artwork that matches the Type and Size - * @param artworkType - * @param artworkSize - * @return - */ - public Artwork getFirstArtwork(String artworkType, String artworkSize) { - return getArtwork(artworkType, artworkSize, 1); - } - } diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Person.java b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Person.java index 8f19afed4..4adb0a2ed 100644 --- a/themoviedbapi/src/com/moviejukebox/themoviedb/model/Person.java +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/model/Person.java @@ -13,25 +13,58 @@ package com.moviejukebox.themoviedb.model; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.moviejukebox.themoviedb.tools.ModelTools; + /** * This is the new bean for the Person * * @author Stuart.Boston * */ -public class Person { - public String url; - public String name; - public String job; - public String character; - public String id; +public class Person extends ModelTools { + private String biography; + private String character; + private String id; + private String job; + private String name; + private String url; + private int version; + private Date lastModifiedAt; + private List filmography = new ArrayList(); + private List aka = new ArrayList(); + private int knownMovies; + private Date birthday; + private String birthPlace; + - public String getName() { - return name; + public String getBiography() { + return biography; } - public void setName(String name) { - this.name = name; + public void setBiography(String biography) { + this.biography = biography; + } + + public String getCharacter() { + return character; + } + + public void setCharacter(String character) { + this.character = character; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; } public String getJob() { @@ -42,6 +75,14 @@ public class Person { this.job = job; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public String getUrl() { return url; } @@ -49,20 +90,90 @@ public class Person { public void setUrl(String url) { this.url = url; } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } - public String getId() { - return id; + public List getFilmography() { + return filmography; + } + + public void setFilmography(List filmography) { + this.filmography = filmography; } - public void setId(String id) { - this.id = id; + public void addFilm(Filmography film) { + this.filmography.add(film); } - public String getCharacter() { - return character; + public List getAka() { + return aka; + } + + public void setAka(List aka) { + this.aka = aka; } - public void setCharacter(String character) { - this.character = character; + public void addAka(String alsoKnownAs) { + this.aka.add(alsoKnownAs); + } + + public Date getLastModifiedAt() { + return lastModifiedAt; + } + + public void setLastModifiedAt(Date lastModifiedAt) { + this.lastModifiedAt = lastModifiedAt; + } + + public void setLastModifiedAt(String lastModifiedAt) { + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + try { + Date lma = df.parse(lastModifiedAt); + setLastModifiedAt(lma); + } catch (Exception ignore) { + return; + } + } + + public int getKnownMovies() { + return knownMovies; + } + + public void setKnownMovies(int knownMovies) { + this.knownMovies = knownMovies; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public void setBirthday(String sBirthday) { + DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + + try { + Date birthday = df.parse(sBirthday); + setBirthday(birthday); + } catch (Exception ignore) { + return; + } + } + + public String getBirthPlace() { + return birthPlace; + } + + public void setBirthPlace(String birthPlace) { + this.birthPlace = birthPlace; } } diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/tools/DOMHelper.java b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/DOMHelper.java new file mode 100644 index 000000000..ae4e5efa0 --- /dev/null +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/DOMHelper.java @@ -0,0 +1,75 @@ +/* + * 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.tools; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * Generic set of routines to process the DOM model data + * @author Stuart + * + */ +public class DOMHelper { + /** + * Gets the string value of the tag element name passed + * @param element + * @param tagName + * @return + */ + public static String getValueFromElement(Element element, String tagName) { + String returnValue = ""; + + try { + NodeList elementNodeList = element.getElementsByTagName(tagName); + Element tagElement = (Element) elementNodeList.item(0); + NodeList tagNodeList = tagElement.getChildNodes(); + returnValue = ((Node) tagNodeList.item(0)).getNodeValue(); + } catch (Exception ignore) { + return returnValue; + } + + return returnValue; + } + + /** + * Get a DOM document from the supplied URL + * @param url + * @return + * @throws MalformedURLException + * @throws IOException + * @throws ParserConfigurationException + * @throws SAXException + */ + public static Document getEventDocFromUrl(String url) throws MalformedURLException, IOException, ParserConfigurationException, SAXException { + InputStream in = (new URL(url)).openStream(); + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(in); + doc.getDocumentElement().normalize(); + return doc; + } +} diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/tools/DOMParser.java b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/DOMParser.java new file mode 100644 index 000000000..699609e7a --- /dev/null +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/DOMParser.java @@ -0,0 +1,276 @@ +/* + * 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.tools; + +import java.util.logging.Logger; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import com.moviejukebox.themoviedb.TheMovieDb; +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.MovieDB; +import com.moviejukebox.themoviedb.model.Person; + +public class DOMParser { + static Logger logger = TheMovieDb.getLogger(); + + public static MovieDB parseMovieInfo(Document doc) { + // Borrowed from http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html + MovieDB movie = null; + NodeList movieNodeList, subNodeList; + Node movieNode, subNode; + Element movieElement, subElement; + + try { + movie = new MovieDB(); + movieNodeList = doc.getElementsByTagName("movie"); + + // Only get the first movie from the list + movieNode = movieNodeList.item(0); + + if (movieNode == null) { + logger.finest("Movie not found"); + return movie; + } + + if (movieNode.getNodeType() == Node.ELEMENT_NODE) { + movieElement = (Element) movieNode; + + movie.setTitle(DOMHelper.getValueFromElement(movieElement, "name")); + movie.setPopularity(DOMHelper.getValueFromElement(movieElement, "popularity")); + movie.setType(DOMHelper.getValueFromElement(movieElement, "type")); + movie.setId(DOMHelper.getValueFromElement(movieElement, "id")); + movie.setImdb(DOMHelper.getValueFromElement(movieElement, "imdb_id")); + movie.setUrl(DOMHelper.getValueFromElement(movieElement, "url")); + movie.setOverview(DOMHelper.getValueFromElement(movieElement, "overview")); + movie.setRating(DOMHelper.getValueFromElement(movieElement, "rating")); + movie.setReleaseDate(DOMHelper.getValueFromElement(movieElement, "released")); + movie.setRuntime(DOMHelper.getValueFromElement(movieElement, "runtime")); + movie.setBudget(DOMHelper.getValueFromElement(movieElement, "budget")); + movie.setRevenue(DOMHelper.getValueFromElement(movieElement, "revenue")); + movie.setHomepage(DOMHelper.getValueFromElement(movieElement, "homepage")); + movie.setTrailer(DOMHelper.getValueFromElement(movieElement, "trailer")); + + // Process the "categories" + subNodeList = doc.getElementsByTagName("categories"); + + for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { + subNode = subNodeList.item(nodeLoop); + if (subNode.getNodeType() == Node.ELEMENT_NODE) { + subElement = (Element) subNode; + Category category = new Category(); + + category.setType(DOMHelper.getValueFromElement(subElement, "type")); + category.setUrl(DOMHelper.getValueFromElement(subElement, "url")); + category.setName(DOMHelper.getValueFromElement(subElement, "name")); + + movie.addCategory(category); + } + } + + // Process the "countries" + subNodeList = doc.getElementsByTagName("countries"); + + for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { + subNode = subNodeList.item(nodeLoop); + if (subNode.getNodeType() == Node.ELEMENT_NODE) { + subElement = (Element) subNode; + Country country = new Country(); + + country.setCode(DOMHelper.getValueFromElement(subElement, "code")); + country.setUrl(DOMHelper.getValueFromElement(subElement, "url")); + country.setName(DOMHelper.getValueFromElement(subElement, "name")); + + movie.addProductionCountry(country); + } + } + + // Process the "cast" + subNodeList = doc.getElementsByTagName("cast"); + + for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { + subNode = subNodeList.item(nodeLoop); + if (subNode.getNodeType() == Node.ELEMENT_NODE) { + subElement = (Element) subNode; + Person person = new Person(); + + person.setUrl(DOMHelper.getValueFromElement(subElement, "url")); + person.setName(DOMHelper.getValueFromElement(subElement, "name")); + person.setJob(DOMHelper.getValueFromElement(subElement, "job")); + person.setCharacter(DOMHelper.getValueFromElement(subElement, "character")); + person.setId(DOMHelper.getValueFromElement(subElement, "id")); + + movie.addPerson(person); + } + } + + /* + * This processes the image elements. There are two formats to deal with: + * Movie.imdbLookup, Movie.getInfo & Movie.search: + * + * + * + * + * + * Movie.getImages: + * + * + * + * + * + * + * + * + * + * + * + * + * + */ + subNodeList = doc.getElementsByTagName("images"); + + for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) { + subNode = subNodeList.item(nodeLoop); + + if (subNode.getNodeType() == Node.ELEMENT_NODE) { + + NodeList artworkNodeList = subNode.getChildNodes(); + for (int artworkLoop = 0; artworkLoop < artworkNodeList.getLength(); artworkLoop++) { + Node artworkNode = artworkNodeList.item(artworkLoop); + if (artworkNode.getNodeType() == Node.ELEMENT_NODE) { + subElement = (Element) artworkNode; + + if (subElement.getNodeName().equalsIgnoreCase("image")) { + // This is the format used in Movie.imdbLookup, Movie.getInfo & Movie.search + Artwork artwork = new Artwork(); + artwork.setType(subElement.getAttribute("type")); + artwork.setSize(subElement.getAttribute("size")); + artwork.setUrl(subElement.getAttribute("url")); + artwork.setId(subElement.getAttribute("id")); + movie.addArtwork(artwork); + } else if (subElement.getNodeName().equalsIgnoreCase("backdrop") || + subElement.getNodeName().equalsIgnoreCase("poster")) { + // This is the format used in Movie.getImages + String artworkId = subElement.getAttribute("id"); + String artworkType = subElement.getNodeName(); + + // We need to decode and loop round the child nodes to get the data + NodeList imageNodeList = subElement.getChildNodes(); + for (int imageLoop = 0; imageLoop < imageNodeList.getLength(); imageLoop++) { + Node imageNode = imageNodeList.item(imageLoop); + if (imageNode.getNodeType() == Node.ELEMENT_NODE) { + Element imageElement = (Element) imageNode; + Artwork artwork = new Artwork(); + artwork.setId(artworkId); + artwork.setType(artworkType); + artwork.setUrl(imageElement.getAttribute("url")); + artwork.setSize(imageElement.getAttribute("size")); + movie.addArtwork(artwork); + } + } + } else { + // This is a classic, it should never happen error + logger.severe("UNKNOWN Image type"); + } + } + } + } + } + } + } catch (Exception error) { + logger.severe("ERROR: " + error.getMessage()); + error.printStackTrace(); + } + return movie; + } + + public static Person parsePersonInfo(Document doc) { + Person person = null; + + try { + person = new Person(); + NodeList personNodeList = doc.getElementsByTagName("person"); + + // Only get the first movie from the list + Node personNode = personNodeList.item(0); + + if (personNode == null) { + logger.finest("Person not found"); + return person; + } + + if (personNode.getNodeType() == Node.ELEMENT_NODE) { + Element personElement = (Element) personNode; + + person.setName(DOMHelper.getValueFromElement(personElement, "name")); + person.setId(DOMHelper.getValueFromElement(personElement, "id")); + person.setBiography(DOMHelper.getValueFromElement(personElement, "biography")); + person.setKnownMovies(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "known_movies"))); + person.setBirthday(DOMHelper.getValueFromElement(personElement, "birthday")); + person.setBirthPlace(DOMHelper.getValueFromElement(personElement, "birthplace")); + person.setUrl(DOMHelper.getValueFromElement(personElement, "url")); + person.setVersion(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "version"))); + person.setLastModifiedAt(DOMHelper.getValueFromElement(personElement, "last_modified_at")); + + NodeList artworkNodeList = doc.getElementsByTagName("image"); + for (int nodeLoop = 0; nodeLoop < artworkNodeList.getLength(); nodeLoop++) { + Node artworkNode = artworkNodeList.item(nodeLoop); + if (artworkNode.getNodeType() == Node.ELEMENT_NODE) { + Element artworkElement = (Element) artworkNode; + Artwork artwork = new Artwork(); + artwork.setType(artworkElement.getAttribute("type")); + artwork.setUrl(artworkElement.getAttribute("url")); + artwork.setSize(artworkElement.getAttribute("size")); + artwork.setId(artworkElement.getAttribute("id")); + person.addArtwork(artwork); + } + } + + NodeList filmNodeList = doc.getElementsByTagName("movie"); + for (int nodeLoop = 0; nodeLoop < filmNodeList.getLength(); nodeLoop++) { + Node filmNode = filmNodeList.item(nodeLoop); + if (filmNode.getNodeType() == Node.ELEMENT_NODE) { + Element filmElement = (Element) filmNode; + Filmography film = new Filmography(); + + film.setCharacter(filmElement.getAttribute("character")); + film.setDepartment(filmElement.getAttribute("department")); + film.setId(filmElement.getAttribute("id")); + film.setJob(filmElement.getAttribute("job")); + film.setName(filmElement.getAttribute("name")); + film.setUrl(filmElement.getAttribute("url")); + + person.addFilm(film); + } + } + } + } catch (Exception error) { + logger.severe("ERROR: " + error.getMessage()); + error.printStackTrace(); + } + + return person; + } + + public static Person parsePersonGetVersion(Document doc) { + // TODO Auto-generated method stub + return null; + } +} diff --git a/themoviedbapi/src/com/moviejukebox/themoviedb/tools/ModelTools.java b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/ModelTools.java new file mode 100644 index 000000000..00833b2d4 --- /dev/null +++ b/themoviedbapi/src/com/moviejukebox/themoviedb/tools/ModelTools.java @@ -0,0 +1,176 @@ +/* + * 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.tools; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.moviejukebox.themoviedb.model.Artwork; + +public class ModelTools { + private List artwork = new ArrayList(); + + /** + * Add a piece of artwork to the artwork array + * @param artworkType must be one of Artwork.ARTWORK_TYPES + * @param artworkSize must be one of Artwork.ARTWORK_SIZES + * @param artworkUrl + * @param posterId + */ + public void addArtwork(String artworkType, String artworkSize, String artworkUrl, String artworkId) { + if (validateElement(Artwork.ARTWORK_TYPES, artworkType) && validateElement(Artwork.ARTWORK_SIZES, artworkSize)) { + Artwork newArtwork = new Artwork(); + + newArtwork.setType(artworkType); + newArtwork.setSize(artworkSize); + newArtwork.setUrl(artworkUrl); + newArtwork.setId(artworkId); + + artwork.add(newArtwork); + Collections.sort(artwork); + } + return; + } + + /** + * Add a piece of artwork to the artwork array + * @param newArtwork an Artwork object to add to the array + */ + public void addArtwork(Artwork newArtwork) { + if (validateElement(Artwork.ARTWORK_TYPES, newArtwork.getType()) && validateElement(Artwork.ARTWORK_SIZES, newArtwork.getSize())) { + artwork.add(newArtwork); + Collections.sort(artwork); + } + return; + } + + /** + * Get the first artwork that matches the Type and Size + * @param artworkType + * @param artworkSize + * @return + */ + public Artwork getFirstArtwork(String artworkType, String artworkSize) { + return getArtwork(artworkType, artworkSize, 1); + } + + /** + * Check to see if element is contained in elementArray + * @param elementArray + * @param element + * @return + */ + private boolean validateElement(String[] elementArray, String element) { + boolean valid = false; + + for (String arrayEntry : elementArray) { + if (arrayEntry.equalsIgnoreCase(element)) { + valid = true; + break; + } + } + + return valid; + } + + /** + * Return all the artwork for a movie + * @return + */ + public List getArtwork() { + return artwork; + } + + /** + * Get all the artwork of a specific type + * @param artworkType + * @return + */ + public List getArtwork(String artworkType) { + // Validate the Type and Size arguments + if (!validateElement(Artwork.ARTWORK_TYPES, artworkType)) { + return null; + } + + List artworkList = new ArrayList(); + + for (Artwork singleArtwork : artwork) { + if (singleArtwork.getType().equalsIgnoreCase(artworkType)) { + artworkList.add(singleArtwork); + } + } + + return artworkList; + } + + /** + * Get all artwork of a specific Type and Size + * @param artworkType + * @param artworkSize + * @return + */ + public List getArtwork(String artworkType, String artworkSize) { + List artworkList = new ArrayList(); + // Validate the Type and Size arguments + if (!validateElement(Artwork.ARTWORK_TYPES, artworkType) && !validateElement(Artwork.ARTWORK_SIZES, artworkSize)) { + return null; + } + + for (Artwork singleArtwork : artwork) { + if (singleArtwork.getType().equalsIgnoreCase(artworkType) && singleArtwork.getSize().equalsIgnoreCase(artworkSize)) { + artworkList.add(singleArtwork); + } + } + + return artworkList; + } + + /** + * Return a specific artwork entry for a Type & Size + * @param artworkType + * @param artworkSize + * @param artworkNumber + * @return + */ + public Artwork getArtwork(String artworkType, String artworkSize, int artworkNumber) { + // Validate the Type and Size arguments + if (!validateElement(Artwork.ARTWORK_TYPES, artworkType) && !validateElement(Artwork.ARTWORK_SIZES, artworkSize)) { + return null; + } + + // Validate the number + if (artworkNumber <= 0) { + artworkNumber = 0; + } else { + // Artwork elements start at 0 (Zero) + artworkNumber -= 1; + } + + List artworkList = getArtwork(artworkType, artworkSize); + + int artworkCount = artworkList.size(); + if (artworkCount < 1) { + return null; + } + + // If the number requested is greater than the array size, loop around until it's within scope + while (artworkNumber > artworkCount) { + artworkNumber = artworkNumber - artworkCount; + } + + return artworkList.get(artworkNumber); + } + +}