Added Last Movie method

Changed organisation of API key to be more thread safe
master
Omertron 14 years ago
parent acf43320a8
commit 9c7b339761

@ -35,31 +35,30 @@ import org.codehaus.jackson.map.ObjectMapper;
public class TheMovieDb {
private static final Logger LOGGER = Logger.getLogger(TheMovieDb.class);
private static String apiKey;
private static TmdbConfiguration tmdbConfig;
private String apiKey;
private TmdbConfiguration tmdbConfig;
/*
* TheMovieDb API URLs
* API Methods These are not set to static so that multiple instances of the
* API can co-exist
*/
private static final String TMDB_API_BASE = "http://api.themoviedb.org/3/";
/*
* API Methods
*/
private static final ApiUrl TMDB_CONFIG_URL = new ApiUrl("configuration");
private static final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl("search/movie");
private static final ApiUrl TMDB_SEARCH_PEOPLE = new ApiUrl("search/person");
private static final ApiUrl TMDB_COLLECTION_INFO = new ApiUrl("collection/");
private static final ApiUrl TMDB_MOVIE_INFO = new ApiUrl("movie/");
private static final ApiUrl TMDB_MOVIE_ALT_TITLES = new ApiUrl("movie/", "/alternative_titles");
private static final ApiUrl TMDB_MOVIE_CASTS = new ApiUrl("movie/", "/casts");
private static final ApiUrl TMDB_MOVIE_IMAGES = new ApiUrl("movie/", "/images");
private static final ApiUrl TMDB_MOVIE_KEYWORDS = new ApiUrl("movie/", "/keywords");
private static final ApiUrl TMDB_MOVIE_RELEASE_INFO = new ApiUrl("movie/", "/releases");
private static final ApiUrl TMDB_MOVIE_TRAILERS = new ApiUrl("movie/", "/trailers");
private static final ApiUrl TMDB_MOVIE_TRANSLATIONS = new ApiUrl("movie/", "/translations");
private static final ApiUrl TMDB_PERSON_INFO = new ApiUrl("person/");
private static final ApiUrl TMDB_PERSON_CREDITS = new ApiUrl("person/", "/credits");
private static final ApiUrl TMDB_PERSON_IMAGES = new ApiUrl("person/", "/images");
private static final ApiUrl TMDB_LATEST_MOVIE = new ApiUrl("latest/movie");
private final String BASE_MOVIE = "movie/";
private final String BASE_PERSON = "person/";
private final ApiUrl TMDB_CONFIG_URL = new ApiUrl(this, "configuration");
private final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl(this, "search/movie");
private final ApiUrl TMDB_SEARCH_PEOPLE = new ApiUrl(this, "search/person");
private final ApiUrl TMDB_COLLECTION_INFO = new ApiUrl(this, "collection/");
private final ApiUrl TMDB_MOVIE_INFO = new ApiUrl(this, BASE_MOVIE);
private final ApiUrl TMDB_MOVIE_ALT_TITLES = new ApiUrl(this, BASE_MOVIE, "/alternative_titles");
private final ApiUrl TMDB_MOVIE_CASTS = new ApiUrl(this, BASE_MOVIE, "/casts");
private final ApiUrl TMDB_MOVIE_IMAGES = new ApiUrl(this, BASE_MOVIE, "/images");
private final ApiUrl TMDB_MOVIE_KEYWORDS = new ApiUrl(this, BASE_MOVIE, "/keywords");
private final ApiUrl TMDB_MOVIE_RELEASE_INFO = new ApiUrl(this, BASE_MOVIE, "/releases");
private final ApiUrl TMDB_MOVIE_TRAILERS = new ApiUrl(this, BASE_MOVIE, "/trailers");
private final ApiUrl TMDB_MOVIE_TRANSLATIONS = new ApiUrl(this, BASE_MOVIE, "/translations");
private final ApiUrl TMDB_PERSON_INFO = new ApiUrl(this, BASE_PERSON);
private final ApiUrl TMDB_PERSON_CREDITS = new ApiUrl(this, BASE_PERSON, "/credits");
private final ApiUrl TMDB_PERSON_IMAGES = new ApiUrl(this, BASE_PERSON, "/images");
private final ApiUrl TMDB_LATEST_MOVIE = new ApiUrl(this, "latest/movie");
/*
* Jackson JSON configuration
@ -73,7 +72,7 @@ public class TheMovieDb {
* @throws IOException
*/
public TheMovieDb(String apiKey) throws IOException {
TheMovieDb.apiKey = apiKey;
this.apiKey = apiKey;
URL configUrl = TMDB_CONFIG_URL.getQueryUrl("");
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
tmdbConfig = mapper.readValue(configUrl, TmdbConfiguration.class);
@ -81,14 +80,10 @@ public class TheMovieDb {
FilteringLayout.addApiKey(apiKey);
}
public static String getApiKey() {
public String getApiKey() {
return apiKey;
}
public static String getApiBase() {
return TMDB_API_BASE;
}
/**
* Search Movies This is a good starting point to start finding movies on
* TMDb. The idea is to be a quick and light method so you can iterate
@ -467,8 +462,23 @@ public class TheMovieDb {
}
}
/**
* This method is used to retrieve the newest movie that was added to TMDb.
* @return
*/
public MovieDb getLatestMovie() {
try {
URL url = TMDB_LATEST_MOVIE.getIdUrl("");
return mapper.readValue(url, MovieDb.class);
} catch (IOException ex) {
LOGGER.warn("Failed to get latest movie: " + ex.getMessage());
return new MovieDb();
}
}
/**
* 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

@ -19,6 +19,7 @@ import org.codehaus.jackson.annotate.JsonProperty;
/**
* Movie Bean
*
* @author stuart.boston
*/
public class MovieDb {
@ -257,6 +258,7 @@ public class MovieDb {
/**
* Handle unknown properties and print a message
*
* @param key
* @param value
*/
@ -274,104 +276,51 @@ public class MovieDb {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final MovieDb other = (MovieDb) obj;
if ((this.backdropPath == null) ? (other.backdropPath != null) : !this.backdropPath.equals(other.backdropPath)) {
return false;
}
if (this.id != other.id) {
return false;
}
if ((this.originalTitle == null) ? (other.originalTitle != null) : !this.originalTitle.equals(other.originalTitle)) {
return false;
}
if (Float.floatToIntBits(this.popularity) != Float.floatToIntBits(other.popularity)) {
return false;
}
if ((this.posterPath == null) ? (other.posterPath != null) : !this.posterPath.equals(other.posterPath)) {
return false;
}
if ((this.releaseDate == null) ? (other.releaseDate != null) : !this.releaseDate.equals(other.releaseDate)) {
return false;
}
if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) {
return false;
}
if (this.adult != other.adult) {
return false;
}
if (this.belongsToCollection != other.belongsToCollection && (this.belongsToCollection == null || !this.belongsToCollection.equals(other.belongsToCollection))) {
return false;
}
if (this.budget != other.budget) {
return false;
}
if (this.genres != other.genres && (this.genres == null || !this.genres.equals(other.genres))) {
return false;
}
if ((this.homepage == null) ? (other.homepage != null) : !this.homepage.equals(other.homepage)) {
return false;
}
if ((this.imdbID == null) ? (other.imdbID != null) : !this.imdbID.equals(other.imdbID)) {
return false;
}
if ((this.overview == null) ? (other.overview != null) : !this.overview.equals(other.overview)) {
return false;
}
if (this.productionCompanies != other.productionCompanies && (this.productionCompanies == null || !this.productionCompanies.equals(other.productionCompanies))) {
return false;
}
if (this.productionCountries != other.productionCountries && (this.productionCountries == null || !this.productionCountries.equals(other.productionCountries))) {
return false;
}
if (this.revenue != other.revenue) {
return false;
}
if (this.runtime != other.runtime) {
return false;
}
if (this.spokenLanguages != other.spokenLanguages && (this.spokenLanguages == null || !this.spokenLanguages.equals(other.spokenLanguages))) {
return false;
}
if ((this.tagline == null) ? (other.tagline != null) : !this.tagline.equals(other.tagline)) {
return false;
}
if (Float.floatToIntBits(this.voteAverage) != Float.floatToIntBits(other.voteAverage)) {
return false;
}
if (this.voteCount != other.voteCount) {
// Dirty way of checking that all the fields are the same
if (this.toString().equals(other.toString())) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + (this.backdropPath != null ? this.backdropPath.hashCode() : 0);
hash = 97 * hash + this.id;
hash = 97 * hash + (this.originalTitle != null ? this.originalTitle.hashCode() : 0);
hash = 97 * hash + Float.floatToIntBits(this.popularity);
hash = 97 * hash + (this.posterPath != null ? this.posterPath.hashCode() : 0);
hash = 97 * hash + (this.releaseDate != null ? this.releaseDate.hashCode() : 0);
hash = 97 * hash + (this.title != null ? this.title.hashCode() : 0);
hash = 97 * hash + (this.adult ? 1 : 0);
hash = 97 * hash + (this.belongsToCollection != null ? this.belongsToCollection.hashCode() : 0);
hash = 97 * hash + this.budget;
hash = 97 * hash + (this.genres != null ? this.genres.hashCode() : 0);
hash = 97 * hash + (this.homepage != null ? this.homepage.hashCode() : 0);
hash = 97 * hash + (this.imdbID != null ? this.imdbID.hashCode() : 0);
hash = 97 * hash + (this.overview != null ? this.overview.hashCode() : 0);
hash = 97 * hash + (this.productionCompanies != null ? this.productionCompanies.hashCode() : 0);
hash = 97 * hash + (this.productionCountries != null ? this.productionCountries.hashCode() : 0);
hash = 97 * hash + this.revenue;
hash = 97 * hash + this.runtime;
hash = 97 * hash + (this.spokenLanguages != null ? this.spokenLanguages.hashCode() : 0);
hash = 97 * hash + (this.tagline != null ? this.tagline.hashCode() : 0);
hash = 97 * hash + Float.floatToIntBits(this.voteAverage);
hash = 97 * hash + this.voteCount;
int multiplier = 97;
hash = multiplier * hash + (this.backdropPath != null ? this.backdropPath.hashCode() : 0);
hash = multiplier * hash + this.id;
hash = multiplier * hash + (this.originalTitle != null ? this.originalTitle.hashCode() : 0);
hash = multiplier * hash + Float.floatToIntBits(this.popularity);
hash = multiplier * hash + (this.posterPath != null ? this.posterPath.hashCode() : 0);
hash = multiplier * hash + (this.releaseDate != null ? this.releaseDate.hashCode() : 0);
hash = multiplier * hash + (this.title != null ? this.title.hashCode() : 0);
hash = multiplier * hash + (this.adult ? 1 : 0);
hash = multiplier * hash + (this.belongsToCollection != null ? this.belongsToCollection.hashCode() : 0);
hash = multiplier * hash + this.budget;
hash = multiplier * hash + (this.genres != null ? this.genres.hashCode() : 0);
hash = multiplier * hash + (this.homepage != null ? this.homepage.hashCode() : 0);
hash = multiplier * hash + (this.imdbID != null ? this.imdbID.hashCode() : 0);
hash = multiplier * hash + (this.overview != null ? this.overview.hashCode() : 0);
hash = multiplier * hash + (this.productionCompanies != null ? this.productionCompanies.hashCode() : 0);
hash = multiplier * hash + (this.productionCountries != null ? this.productionCountries.hashCode() : 0);
hash = multiplier * hash + this.revenue;
hash = multiplier * hash + this.runtime;
hash = multiplier * hash + (this.spokenLanguages != null ? this.spokenLanguages.hashCode() : 0);
hash = multiplier * hash + (this.tagline != null ? this.tagline.hashCode() : 0);
hash = multiplier * hash + Float.floatToIntBits(this.voteAverage);
hash = multiplier * hash + this.voteCount;
return hash;
}
//</editor-fold>

@ -31,6 +31,10 @@ public class ApiUrl {
* Logger
*/
private static final Logger LOGGER = Logger.getLogger(ApiUrl.class);
/*
* TheMovieDb API Base URL
*/
private final String TMDB_API_BASE = "http://api.themoviedb.org/3/";
/*
* Parameter configuration
*/
@ -48,13 +52,15 @@ public class ApiUrl {
*/
private String method;
private String submethod;
private TheMovieDb TMDb;
//<editor-fold defaultstate="collapsed" desc="Constructor Methods">
/**
* Constructor for the simple API URL method without a sub-method
* @param method
*/
public ApiUrl(String method) {
public ApiUrl(TheMovieDb TMDb, String method) {
this.TMDb = TMDb;
this.method = method;
this.submethod = DEFAULT_STRING;
}
@ -64,7 +70,8 @@ public class ApiUrl {
* @param method
* @param submethod
*/
public ApiUrl(String method, String submethod) {
public ApiUrl(TheMovieDb TMDb, String method, String submethod) {
this.TMDb = TMDb;
this.method = method;
this.submethod = submethod;
}
@ -81,7 +88,7 @@ public class ApiUrl {
* @return
*/
private URL getFullUrl(String query, String movieId, String language, String country, int page) {
StringBuilder urlString = new StringBuilder(TheMovieDb.getApiBase());
StringBuilder urlString = new StringBuilder(TMDB_API_BASE);
// Get the start of the URL
urlString.append(method);
@ -116,7 +123,7 @@ public class ApiUrl {
urlString.append(DELIMITER_SUBSEQUENT);
}
urlString.append(PARAMETER_API_KEY);
urlString.append(TheMovieDb.getApiKey());
urlString.append(TMDb.getApiKey());
// Append the language to the URL
if (StringUtils.isNotBlank(language)) {

Loading…
Cancel
Save