From 7034be468165527604436d0ab116b1f7cd442c30 Mon Sep 17 00:00:00 2001 From: Omertron <> Date: Thu, 27 Dec 2012 21:50:28 +0000 Subject: [PATCH] Added keyword search --- .../omertron/themoviedbapi/TheMovieDbApi.java | 30 +++++- .../wrapper/WrapperKeywords.java | 100 ++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/omertron/themoviedbapi/wrapper/WrapperKeywords.java diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 2aec5a543..f405916c9 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -55,6 +55,7 @@ import com.omertron.themoviedbapi.wrapper.WrapperCompanyMovies; import com.omertron.themoviedbapi.wrapper.WrapperConfig; import com.omertron.themoviedbapi.wrapper.WrapperGenres; import com.omertron.themoviedbapi.wrapper.WrapperImages; +import com.omertron.themoviedbapi.wrapper.WrapperKeywords; import com.omertron.themoviedbapi.wrapper.WrapperMovie; import com.omertron.themoviedbapi.wrapper.WrapperMovieCasts; import com.omertron.themoviedbapi.wrapper.WrapperMovieKeywords; @@ -1469,7 +1470,34 @@ public class TheMovieDbApi { } } - public void searchKeyword() { + /** + * Search for keywords by name + * + * @param query + * @param page + * @throws MovieDbException + */ + public List searchKeyword(String query, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(this, BASE_SEARCH, "keyword"); + + if (StringUtils.isNotBlank(query)) { + apiUrl.addArgument(PARAM_QUERY, query); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); + } + + URL url = apiUrl.buildUrl(); + + String webpage = WebBrowser.request(url); + try { + WrapperKeywords wrapper = mapper.readValue(webpage, WrapperKeywords.class); + return wrapper.getResults(); + } catch (IOException ex) { + logger.warn("Failed to find keyword: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } } // // diff --git a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperKeywords.java b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperKeywords.java new file mode 100644 index 000000000..06714f0b9 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperKeywords.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2004-2012 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi.wrapper; + +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.omertron.themoviedbapi.model.Collection; +import com.omertron.themoviedbapi.model.Keyword; +import java.util.List; +import org.apache.log4j.Logger; + +/** + * + * @author stuart.boston + */ +public class WrapperKeywords { + /* + * Logger + */ + + private static final Logger logger = Logger.getLogger(WrapperKeywords.class); + /* + * Properties + */ + @JsonProperty("page") + private int page; + @JsonProperty("results") + private List results; + @JsonProperty("total_pages") + private int totalPages; + @JsonProperty("total_results") + private int totalResults; + + // + public int getPage() { + return page; + } + + public List getResults() { + return results; + } + + public int getTotalPages() { + return totalPages; + } + + public int getTotalResults() { + return totalResults; + } + // + + // + public void setPage(int page) { + this.page = page; + } + + public void setResults(List results) { + this.results = results; + } + + public void setTotalPages(int totalPages) { + this.totalPages = totalPages; + } + + public void setTotalResults(int totalResults) { + this.totalResults = totalResults; + } + // + + /** + * Handle unknown properties and print a message + * + * @param key + * @param value + */ + @JsonAnySetter + public void handleUnknown(String key, Object value) { + StringBuilder sb = new StringBuilder(); + sb.append("Unknown property: '").append(key); + sb.append("' value: '").append(value).append("'"); + logger.trace(sb.toString()); + } +}