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 { public class TheMovieDb {
private static final Logger LOGGER = Logger.getLogger(TheMovieDb.class); private static final Logger LOGGER = Logger.getLogger(TheMovieDb.class);
private static String apiKey; private String apiKey;
private static TmdbConfiguration tmdbConfig; 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/"; private final String BASE_MOVIE = "movie/";
/* private final String BASE_PERSON = "person/";
* API Methods private final ApiUrl TMDB_CONFIG_URL = new ApiUrl(this, "configuration");
*/ private final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl(this, "search/movie");
private static final ApiUrl TMDB_CONFIG_URL = new ApiUrl("configuration"); private final ApiUrl TMDB_SEARCH_PEOPLE = new ApiUrl(this, "search/person");
private static final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl("search/movie"); private final ApiUrl TMDB_COLLECTION_INFO = new ApiUrl(this, "collection/");
private static final ApiUrl TMDB_SEARCH_PEOPLE = new ApiUrl("search/person"); private final ApiUrl TMDB_MOVIE_INFO = new ApiUrl(this, BASE_MOVIE);
private static final ApiUrl TMDB_COLLECTION_INFO = new ApiUrl("collection/"); private final ApiUrl TMDB_MOVIE_ALT_TITLES = new ApiUrl(this, BASE_MOVIE, "/alternative_titles");
private static final ApiUrl TMDB_MOVIE_INFO = new ApiUrl("movie/"); private final ApiUrl TMDB_MOVIE_CASTS = new ApiUrl(this, BASE_MOVIE, "/casts");
private static final ApiUrl TMDB_MOVIE_ALT_TITLES = new ApiUrl("movie/", "/alternative_titles"); private final ApiUrl TMDB_MOVIE_IMAGES = new ApiUrl(this, BASE_MOVIE, "/images");
private static final ApiUrl TMDB_MOVIE_CASTS = new ApiUrl("movie/", "/casts"); private final ApiUrl TMDB_MOVIE_KEYWORDS = new ApiUrl(this, BASE_MOVIE, "/keywords");
private static final ApiUrl TMDB_MOVIE_IMAGES = new ApiUrl("movie/", "/images"); private final ApiUrl TMDB_MOVIE_RELEASE_INFO = new ApiUrl(this, BASE_MOVIE, "/releases");
private static final ApiUrl TMDB_MOVIE_KEYWORDS = new ApiUrl("movie/", "/keywords"); private final ApiUrl TMDB_MOVIE_TRAILERS = new ApiUrl(this, BASE_MOVIE, "/trailers");
private static final ApiUrl TMDB_MOVIE_RELEASE_INFO = new ApiUrl("movie/", "/releases"); private final ApiUrl TMDB_MOVIE_TRANSLATIONS = new ApiUrl(this, BASE_MOVIE, "/translations");
private static final ApiUrl TMDB_MOVIE_TRAILERS = new ApiUrl("movie/", "/trailers"); private final ApiUrl TMDB_PERSON_INFO = new ApiUrl(this, BASE_PERSON);
private static final ApiUrl TMDB_MOVIE_TRANSLATIONS = new ApiUrl("movie/", "/translations"); private final ApiUrl TMDB_PERSON_CREDITS = new ApiUrl(this, BASE_PERSON, "/credits");
private static final ApiUrl TMDB_PERSON_INFO = new ApiUrl("person/"); private final ApiUrl TMDB_PERSON_IMAGES = new ApiUrl(this, BASE_PERSON, "/images");
private static final ApiUrl TMDB_PERSON_CREDITS = new ApiUrl("person/", "/credits"); private final ApiUrl TMDB_LATEST_MOVIE = new ApiUrl(this, "latest/movie");
private static final ApiUrl TMDB_PERSON_IMAGES = new ApiUrl("person/", "/images");
private static final ApiUrl TMDB_LATEST_MOVIE = new ApiUrl("latest/movie");
/* /*
* Jackson JSON configuration * Jackson JSON configuration
@ -73,7 +72,7 @@ public class TheMovieDb {
* @throws IOException * @throws IOException
*/ */
public TheMovieDb(String apiKey) throws IOException { public TheMovieDb(String apiKey) throws IOException {
TheMovieDb.apiKey = apiKey; this.apiKey = apiKey;
URL configUrl = TMDB_CONFIG_URL.getQueryUrl(""); URL configUrl = TMDB_CONFIG_URL.getQueryUrl("");
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
tmdbConfig = mapper.readValue(configUrl, TmdbConfiguration.class); tmdbConfig = mapper.readValue(configUrl, TmdbConfiguration.class);
@ -81,14 +80,10 @@ public class TheMovieDb {
FilteringLayout.addApiKey(apiKey); FilteringLayout.addApiKey(apiKey);
} }
public static String getApiKey() { public String getApiKey() {
return apiKey; return apiKey;
} }
public static String getApiBase() {
return TMDB_API_BASE;
}
/** /**
* Search Movies This is a good starting point to start finding movies on * 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 * TMDb. The idea is to be a quick and light method so you can iterate
@ -467,12 +462,27 @@ 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 * 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 moviedb The moviedb object to compare too
* @param year The year of the movie to compare * @param title The title of the movie to compare
* @return True if there is a match, False otherwise. * @param year The year of the movie to compare
* @return True if there is a match, False otherwise.
*/ */
public static boolean compareMovies(MovieDb moviedb, String title, String year) { public static boolean compareMovies(MovieDb moviedb, String title, String year) {
if ((moviedb == null) || (StringUtils.isBlank(title))) { if ((moviedb == null) || (StringUtils.isBlank(title))) {

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

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

Loading…
Cancel
Save