Source code cleanup

master
Omertron 14 years ago
parent 44b962a60d
commit d0ca6931ec

@ -39,6 +39,11 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<dependencyManagement>

@ -48,6 +48,55 @@ public class TheMovieDb {
private static final String API_SITE = "http://api.themoviedb.org/2.1/";
private static final String DEFAULT_LANGUAGE = "en-US";
/**
* Constructor with default logger.
* @param apiKey
*/
public TheMovieDb(String apiKey) {
setLogger(Logger.getLogger("TheMovieDB"));
if (!isValidString(apiKey)) {
logger.severe("TheMovieDb was initialized with a wrong API key!");
}
setApiKey(apiKey);
}
/*
* API Methods
* http://api.themoviedb.org/2.1
* Note: This is currently a read-only interface and as such, no write methods exist.
*/
/*
* 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";
/**
* Compare the MovieDB object with a title & year
* @param moviedb The moviedb object to compare too
@ -132,67 +181,10 @@ public class TheMovieDb {
return true;
}
/*
* API Methods
* http://api.themoviedb.org/2.1
* Note: This is currently a read-only interface and as such, no write methods exist.
*/
/*
* 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)) {
logger.severe("TheMovieDb was initialized with a wrong API key!");
}
setApiKey(apiKey);
}
public TheMovieDb(String apiKey, Logger logger) {
setLogger(logger);
if (!isValidString(apiKey)) {
logger.severe("TheMovieDb was initialized with a wrong API key!");
}
setApiKey(apiKey);
}
/**
* Build comma separated ids for Movie.getLatest and Movie.getVersion.
* @param ids a List of ids

@ -12,14 +12,17 @@
*/
package com.moviejukebox.themoviedb.model;
import com.moviejukebox.themoviedb.TheMovieDb;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import com.moviejukebox.themoviedb.tools.ModelTools;
import java.util.logging.Logger;
/**
* This is the Movie Search bean for the MovieDb.org search
@ -29,6 +32,7 @@ import com.moviejukebox.themoviedb.tools.ModelTools;
public class MovieDB extends ModelTools implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger logger = TheMovieDb.getLogger();
public static final String UNKNOWN = "UNKNOWN";
@ -298,11 +302,10 @@ public class MovieDB extends ModelTools implements Serializable {
public void setLastModifiedAt(String lastModifiedAt) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
setLastModifiedAt(df.parse(lastModifiedAt));
} catch (Exception ignore) {
return;
} catch (ParseException ex) {
logger.fine("MovieDB: Error parsing date: " + lastModifiedAt);
}
}

@ -12,14 +12,17 @@
*/
package com.moviejukebox.themoviedb.model;
import com.moviejukebox.themoviedb.TheMovieDb;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.moviejukebox.themoviedb.tools.ModelTools;
import java.util.logging.Logger;
/**
* This is the new bean for the Person
@ -29,6 +32,7 @@ import com.moviejukebox.themoviedb.tools.ModelTools;
*/
public class Person extends ModelTools implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger logger = TheMovieDb.getLogger();
private static final String UNKNOWN = MovieDB.UNKNOWN;
@ -50,118 +54,225 @@ public class Person extends ModelTools implements Serializable {
private List<String> aka = new ArrayList<String>();
private List<Artwork> images = new ArrayList<Artwork>();
/**
* Add a single AKA
* @param alsoKnownAs
*/
public void addAka(String alsoKnownAs) {
this.aka.add(alsoKnownAs);
}
/**
* Add a film for the person
* @param film
*/
public void addFilm(Filmography film) {
this.filmography.add(film);
}
/**
* Add an artwork image to the person
* @param image
*/
public void addImage(Artwork image) {
if (image != null) {
this.images.add(image);
}
}
/**
* Get all the AKA values
* @return
*/
public List<String> getAka() {
return aka;
}
/**
* Get the biography information
* @return
*/
public String getBiography() {
return biography;
}
/**
* Get the birthday of the person
* @return
*/
public Date getBirthday() {
return birthday;
}
/**
* Get the birthplace
* @return
*/
public String getBirthPlace() {
return birthPlace;
}
/**
* get the cast ID
* @return
*/
public int getCastId() {
return castId;
}
/**
* get the character
* @return
*/
public String getCharacter() {
return character;
}
/**
* get the department
* @return
*/
public String getDepartment() {
return department;
}
/**
* get the list of films
* @return
*/
public List<Filmography> getFilmography() {
return filmography;
}
/**
* get the ID of the person
* @return
*/
public String getId() {
return id;
}
/**
* get a list of images for the person
* @return
*/
public List<Artwork> getImages() {
return images;
}
/**
* get the job
* @return
*/
public String getJob() {
return job;
}
/**
* get the known movies
* @return
*/
public int getKnownMovies() {
return knownMovies;
}
/**
* get the last modified date for the person
* @return
*/
public Date getLastModifiedAt() {
return lastModifiedAt;
}
/**
* get the name
* @return
*/
public String getName() {
return name;
}
/**
* get the order
* @return
*/
public int getOrder() {
return order;
}
/**
* get the URL for the person
* @return
*/
public String getUrl() {
return url;
}
/**
* get the version
* @return
*/
public int getVersion() {
return version;
}
/**
* Set the AKA list for the person
* @param aka
*/
public void setAka(List<String> aka) {
this.aka = aka;
}
/**
* Set the biography
* @param biography
*/
public void setBiography(String biography) {
this.biography = biography;
}
/**
* Set the person's birthday
* @param birthday
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* Set the person's birthday
* @param sBirthday
*/
public void setBirthday(String sBirthday) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
setBirthday(df.parse(sBirthday));
} catch (Exception ignore) {
return;
} catch (ParseException ex) {
logger.fine("TheMovieDB - Person: Error parsing birthday: " + sBirthday);
}
}
/**
* Set the birth place
* @param birthPlace
*/
public void setBirthPlace(String birthPlace) {
this.birthPlace = birthPlace;
}
/**
* Set the cast ID for the person
* @param castId
*/
public void setCastId(int castId) {
this.castId = castId;
}
/**
* Set the cast ID for the person
* @param castId
*/
public void setCastId(String castId) {
try {
this.castId = Integer.parseInt(castId);
@ -170,38 +281,74 @@ public class Person extends ModelTools implements Serializable {
}
}
/**
* Set the character
* @param character
*/
public void setCharacter(String character) {
this.character = character;
}
/**
* set the Department
* @param department
*/
public void setDepartment(String department) {
this.department = department;
}
/**
* Add a list of films
* @param filmography
*/
public void setFilmography(List<Filmography> filmography) {
this.filmography = filmography;
}
/**
* Set the ID of the person
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* Set a list of images for the person
* @param images
*/
public void setImages(List<Artwork> images) {
this.images = images;
}
/**
* Set the job for the person
* @param job
*/
public void setJob(String job) {
this.job = job;
}
/**
* Set the known movie for the person
* @param knownMovies
*/
public void setKnownMovies(int knownMovies) {
this.knownMovies = knownMovies;
}
/**
* Set the last modified date
* @param lastModifiedAt
*/
public void setLastModifiedAt(Date lastModifiedAt) {
this.lastModifiedAt = lastModifiedAt;
}
/**
* Set the last modified date
* @param lastModifiedAt
*/
public void setLastModifiedAt(String lastModifiedAt) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ -213,14 +360,26 @@ public class Person extends ModelTools implements Serializable {
}
}
/**
* Set the person's anme
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
*Set the order
* @param order
*/
public void setOrder(int order) {
this.order = order;
}
/**
* Set the order
* @param order
*/
public void setOrder(String order) {
try {
this.order = Integer.parseInt(order);
@ -229,14 +388,26 @@ public class Person extends ModelTools implements Serializable {
}
}
/**
* Set the URL
* @param url
*/
public void setUrl(String url) {
this.url = url;
}
/**
* Set the version
* @param version
*/
public void setVersion(int version) {
this.version = version;
}
/**
* Generate a String representation of the person
* @return
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

@ -1,36 +0,0 @@
/*
* Copyright (c) 2004-2011 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;
public class Base64 {
private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static String base64Encode(String string) {
String unEncoded = string; // Copy the string so we can modify it
StringBuffer encoded = new StringBuffer();
// determine how many padding bytes to add to the output
int paddingCount = (3 - (unEncoded.length() % 3)) % 3;
// add any necessary padding to the input
unEncoded += "\0\0".substring(0, paddingCount);
// process 3 bytes at a time, churning out 4 output bytes
// worry about CRLF insertions later
for (int i = 0; i < unEncoded.length(); i += 3) {
int j = (unEncoded.charAt(i) << 16) + (unEncoded.charAt(i + 1) << 8) + unEncoded.charAt(i + 2);
encoded.append(base64code.charAt((j >> 18) & 0x3f) + base64code.charAt((j >> 12) & 0x3f) + base64code.charAt((j >> 6) & 0x3f)
+ base64code.charAt(j & 0x3f));
}
// replace encoded padding nulls with "="
// return encoded;
return "Basic " + encoded.toString();
}
}

@ -35,7 +35,7 @@ import com.moviejukebox.themoviedb.TheMovieDb;
*
*/
public class DOMHelper {
private static Logger logger = TheMovieDb.getLogger();
private static final Logger logger = TheMovieDb.getLogger();
/**
* Gets the string value of the tag element name passed

@ -15,15 +15,18 @@ package com.moviejukebox.themoviedb.tools;
import java.security.PrivilegedAction;
import java.util.logging.LogRecord;
public class LogFormatter extends java.util.logging.Formatter
{
public class LogFormatter extends java.util.logging.Formatter {
private static String apiKey = null;
private static String EOL = (String)java.security.AccessController.doPrivileged(new PrivilegedAction<Object>() {
private static final String EOL = (String) java.security.AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
return System.getProperty("line.separator");
}
});
@Override
public synchronized String format(LogRecord logRecord) {
String logMessage = logRecord.getMessage();

@ -34,9 +34,13 @@ import com.moviejukebox.themoviedb.model.MovieDB;
import com.moviejukebox.themoviedb.model.Person;
import com.moviejukebox.themoviedb.model.Studio;
/**
* The parser helper class for TheMovieDb API
* @author stuart.boston
*/
public class MovieDbParser {
private static Logger logger = TheMovieDb.getLogger();
private static final Logger logger = TheMovieDb.getLogger();
private static final String NAME = "name";
private static final String GENRE = "genre";
@ -86,6 +90,11 @@ public class MovieDbParser {
return categories;
}
/**
* Get the list of available languages
* @param url
* @return
*/
public static List<Language> parseLanguages(String url) {
List<Language> languages = new ArrayList<Language>();
Document doc = null;
@ -155,6 +164,11 @@ public class MovieDbParser {
return movie;
}
/**
* Parse a DOM document and return the person information
* @param url
* @return
*/
public static Person parseLatestPerson(String url) {
Person person = new Person();
Document doc = null;
@ -219,6 +233,11 @@ public class MovieDbParser {
return movie;
}
/**
* Parse the DOM document for movie information and return a list of movies
* @param url
* @return
*/
public static List<MovieDB> parseMovieGetVersion(String url) {
List<MovieDB> movies = new ArrayList<MovieDB>();
Document doc = null;
@ -251,6 +270,11 @@ public class MovieDbParser {
return movies;
}
/**
* Returns a MovieDB object from the Element
* @param movieElement
* @return
*/
private static MovieDB parseMovieInfo(Element movieElement) {
// Inspired by
// http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html
@ -552,6 +576,9 @@ public class MovieDbParser {
return people;
}
/**
* Parse the URL and return a list of the people found in the DOM document
*/
public static ArrayList<Person> parsePersonInfo(String searchUrl) {
ArrayList<Person> people = new ArrayList<Person>();
Person person = null;

@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
/**
* Web browser with simple cookies support
@ -33,7 +34,7 @@ import java.util.regex.Pattern;
public final class WebBrowser {
private static Map<String, String> browserProperties = new HashMap<String, String>();
private static Map<String, Map<String, String>> cookies;
private static Map<String, Map<String, String>> cookies = new HashMap<String, Map<String, String>>();
private static String proxyHost = null;
private static String proxyPort = null;
private static String proxyUsername = null;
@ -42,15 +43,32 @@ public final class WebBrowser {
private static int webTimeoutConnect = 25000; // 25 second timeout
private static int webTimeoutRead = 90000; // 90 second timeout
static {
/**
* Constructor for WebBrowser.
* Does instantiates the browser properties.
*/
public WebBrowser() {
if (browserProperties.isEmpty()) {
browserProperties.put("User-Agent", "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)");
cookies = new HashMap<String, Map<String, String>>();
}
}
/**
* Request the web page at the specified URL
* @param url
* @return
* @throws IOException
*/
public static String request(String url) throws IOException {
return request(new URL(url));
}
/**
* Open a connection using proxy parameters if they exist.
* @param url
* @return
* @throws IOException
*/
public static URLConnection openProxiedConnection(URL url) throws IOException {
if (proxyHost != null) {
System.getProperties().put("proxySet", "true");
@ -67,11 +85,14 @@ public final class WebBrowser {
return cnx;
}
/**
* Request the web page at the specified URL
* @param url
* @return
* @throws IOException
*/
public static String request(URL url) throws IOException {
StringWriter content = null;
try {
content = new StringWriter();
StringBuilder content = new StringBuilder();
BufferedReader in = null;
URLConnection cnx = null;
@ -84,7 +105,7 @@ public final class WebBrowser {
in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
String line;
while ((line = in.readLine()) != null) {
content.write(line);
content.append(line);
}
} finally {
if (in != null) {
@ -97,13 +118,12 @@ public final class WebBrowser {
}
return content.toString();
} finally {
if (content != null) {
content.close();
}
}
}
/**
* Set the header information for the connection
* @param cnx
*/
private static void sendHeader(URLConnection cnx) {
// send browser properties
for (Map.Entry<String, String> browserProperty : browserProperties.entrySet()) {
@ -116,6 +136,11 @@ public final class WebBrowser {
}
}
/**
* Create the cookies for the header
* @param cnx
* @return
*/
private static String createCookieHeader(URLConnection cnx) {
String host = cnx.getURL().getHost();
StringBuilder cookiesHeader = new StringBuilder();
@ -136,6 +161,10 @@ public final class WebBrowser {
return cookiesHeader.toString();
}
/**
* Read the header information into the cookies
* @param cnx
*/
private static void readHeader(URLConnection cnx) {
// read new cookies and update our cookies
for (Map.Entry<String, List<String>> header : cnx.getHeaderFields().entrySet()) {
@ -172,6 +201,11 @@ public final class WebBrowser {
}
}
/**
* Determine the charset for the connection
* @param cnx
* @return
*/
private static Charset getCharset(URLConnection cnx) {
Charset charset = null;
// content type will be string like "text/html; charset=UTF-8" or "text/html"
@ -195,55 +229,104 @@ public final class WebBrowser {
return charset;
}
/**
* Return the proxy host name
* @return
*/
public static String getProxyHost() {
return proxyHost;
}
/**
* Set the proxy host name
* @param tvdbProxyHost
*/
public static void setProxyHost(String tvdbProxyHost) {
WebBrowser.proxyHost = tvdbProxyHost;
}
/**
* Get the proxy port
* @return
*/
public static String getProxyPort() {
return proxyPort;
}
public static void setProxyPort(String tvdbProxyPort) {
WebBrowser.proxyPort = tvdbProxyPort;
/**
* Set the proxy port
* @param proxyPort
*/
public static void setProxyPort(String proxyPort) {
WebBrowser.proxyPort = proxyPort;
}
public static String getTvdbProxyUsername() {
/**
* Get the proxy username
* @return
*/
public static String getProxyUsername() {
return proxyUsername;
}
public static void setProxyUsername(String tvdbProxyUsername) {
WebBrowser.proxyUsername = tvdbProxyUsername;
/**
* Set the proxy username
* @param proxyUsername
*/
public static void setProxyUsername(String proxyUsername) {
WebBrowser.proxyUsername = proxyUsername;
}
/**
* Get the proxy password
* @return
*/
public static String getProxyPassword() {
return proxyPassword;
}
public static void setProxyPassword(String tvdbProxyPassword) {
WebBrowser.proxyPassword = tvdbProxyPassword;
/**
* Set the proxy password.
* Note this will automatically encode the password
* @param proxyPassword
*/
public static void setProxyPassword(String proxyPassword) {
WebBrowser.proxyPassword = proxyPassword;
if (proxyUsername != null) {
proxyEncodedPassword = proxyUsername + ":" + tvdbProxyPassword;
proxyEncodedPassword = Base64.base64Encode(proxyEncodedPassword);
if (proxyUsername != null && !proxyPassword.isEmpty()) {
proxyEncodedPassword = proxyUsername + ":" + proxyPassword;
proxyEncodedPassword = "Basic " + new String(Base64.encodeBase64((proxyUsername + ":" + proxyPassword).getBytes()));
}
}
/**
* Get the current web connect timeout value
* @return
*/
public static int getWebTimeoutConnect() {
return webTimeoutConnect;
}
/**
* Get the current web read timeout value
* @return
*/
public static int getWebTimeoutRead() {
return webTimeoutRead;
}
/**
* Set the web connect timeout value
* @param webTimeoutConnect
*/
public static void setWebTimeoutConnect(int webTimeoutConnect) {
WebBrowser.webTimeoutConnect = webTimeoutConnect;
}
/**
* Set the web read timeout value
* @param webTimeoutRead
*/
public static void setWebTimeoutRead(int webTimeoutRead) {
WebBrowser.webTimeoutRead = webTimeoutRead;
}

Loading…
Cancel
Save