1
0
mirror of https://github.com/mitb-archive/filebot synced 2025-01-11 05:48:01 -05:00

+ remove IMDb

This commit is contained in:
Reinhard Pointner 2014-08-05 08:23:39 +00:00
parent a6d1e7eddf
commit 3b509a9f05
8 changed files with 257 additions and 143 deletions

View File

@ -26,11 +26,11 @@ import net.filebot.web.AudioTrack;
import net.filebot.web.EpisodeListProvider;
import net.filebot.web.FanartTVClient;
import net.filebot.web.ID3Lookup;
import net.filebot.web.IMDbClient;
import net.filebot.web.LocalSearch;
import net.filebot.web.Movie;
import net.filebot.web.MovieIdentificationService;
import net.filebot.web.MusicIdentificationService;
import net.filebot.web.OMDbClient;
import net.filebot.web.OpenSubtitlesClient;
import net.filebot.web.SearchResult;
import net.filebot.web.SubtitleProvider;
@ -54,7 +54,7 @@ public final class WebServices {
public static final TheTVDBClientWithLocalSearch TheTVDB = new TheTVDBClientWithLocalSearch(getApplicationProperty("thetvdb.apikey"));
// movie dbs
public static final IMDbClient IMDb = new IMDbClient();
public static final OMDbClient OMDb = new OMDbClient();
public static final TMDbClient TheMovieDB = new TMDbClient(getApplicationProperty("themoviedb.apikey"));
// subtitle dbs
@ -70,7 +70,7 @@ public final class WebServices {
}
public static MovieIdentificationService[] getMovieIdentificationServices() {
return new MovieIdentificationService[] { TheMovieDB, IMDb };
return new MovieIdentificationService[] { TheMovieDB, OMDb };
}
public static SubtitleProvider[] getSubtitleProviders() {

View File

@ -573,11 +573,11 @@ public class MediaBindingBean {
try {
if (infoObject instanceof Episode) {
data = WebServices.IMDb.getImdbApiMovieInfo(new Movie(getEpisode().getSeriesName(), getEpisode().getSeriesStartDate().getYear(), -1, -1));
data = WebServices.OMDb.getMovieInfo(new Movie(getEpisode().getSeriesName(), getEpisode().getSeriesStartDate().getYear(), -1, -1));
}
if (infoObject instanceof Movie) {
Movie m = getMovie();
data = WebServices.IMDb.getImdbApiMovieInfo(m.getImdbId() > 0 ? m : new Movie(null, -1, WebServices.TheMovieDB.getMovieInfo(getMovie(), Locale.ENGLISH, false).getImdbId(), -1));
data = WebServices.OMDb.getMovieInfo(m.getImdbId() > 0 ? m : new Movie(null, -1, WebServices.TheMovieDB.getMovieInfo(getMovie(), Locale.ENGLISH, false).getImdbId(), -1));
}
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve metadata: " + infoObject, e);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 487 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 823 B

View File

@ -10,12 +10,8 @@ import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -25,17 +21,12 @@ import java.util.regex.Pattern;
import javax.swing.Icon;
import net.filebot.ResourceManager;
import net.filebot.web.TMDbClient.MovieInfo;
import net.filebot.web.TMDbClient.MovieInfo.MovieProperty;
import net.filebot.web.TMDbClient.Person;
import net.filebot.web.TMDbClient.Trailer;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
@Deprecated
public class IMDbClient implements MovieIdentificationService {
private String host = "www.imdb.com";
@ -166,78 +157,4 @@ public class IMDbClient implements MovieIdentificationService {
throw new UnsupportedOperationException();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public Map<String, String> getImdbApiData(Integer i, String t, String y, boolean tomatoes) throws IOException {
// e.g. http://www.imdbapi.com/?i=tt0379786&r=xml&tomatoes=true
String url = String.format("http://www.omdbapi.com/?i=%s&t=%s&y=%s&r=xml&tomatoes=%s", String.format(i == null ? "" : "tt%07d", i), t, y, tomatoes);
CachedResource<HashMap> data = new CachedResource<HashMap>(url, HashMap.class) {
@Override
public HashMap process(ByteBuffer data) throws Exception {
Document xml = getDocument(Charset.forName("UTF-8").decode(data).toString());
HashMap attr = new HashMap();
for (Node it : selectNodes("//@*", xml)) {
attr.put(it.getNodeName(), it.getTextContent());
}
return attr;
}
@Override
protected Cache getCache() {
return CacheManager.getInstance().getCache("web-datasource-lv2");
}
};
return data.get();
}
public MovieInfo getImdbApiMovieInfo(Movie movie) throws IOException {
Map<String, String> data = movie.getImdbId() > 0 ? getImdbApiData(movie.getImdbId(), "", "", false) : getImdbApiData(null, movie.getName(), String.valueOf(movie.getYear()), false);
// sanity check
if (!Boolean.parseBoolean(data.get("response"))) {
throw new IllegalArgumentException("Movie not found: " + data);
}
Map<MovieProperty, String> fields = new EnumMap<MovieProperty, String>(MovieProperty.class);
fields.put(MovieProperty.title, data.get("title"));
fields.put(MovieProperty.certification, data.get("rated"));
fields.put(MovieProperty.runtime, data.get("runtime"));
fields.put(MovieProperty.tagline, data.get("plot"));
fields.put(MovieProperty.vote_average, data.get("imdbRating"));
fields.put(MovieProperty.vote_count, data.get("imdbVotes").replaceAll("\\D", ""));
fields.put(MovieProperty.imdb_id, data.get("imdbID"));
fields.put(MovieProperty.poster_path, data.get("poster"));
// convert release date to yyyy-MM-dd
SimpleDate released = SimpleDate.parse(data.get("released"), "dd MMM yyyy");
if (released != null) {
fields.put(MovieProperty.release_date, released.format("yyyy-MM-dd"));
} else {
SimpleDate year = SimpleDate.parse(data.get("year"), "yyyy");
if (year != null) {
fields.put(MovieProperty.release_date, year.format("yyyy-MM-dd"));
}
}
List<String> genres = new ArrayList<String>();
for (String it : data.get("genre").split(",")) {
genres.add(it.trim());
}
List<Person> actors = new ArrayList<Person>();
for (String it : data.get("actors").split(",")) {
actors.add(new Person(it.trim(), null, null));
}
for (String director : data.get("director").split(",")) {
actors.add(new Person(director, null, "Director"));
}
for (String writer : data.get("writer").split(",")) {
actors.add(new Person(writer, null, "Writer"));
}
return new MovieInfo(fields, new ArrayList<String>(0), genres, new ArrayList<String>(0), actors, new ArrayList<Trailer>(0));
}
}

View File

@ -0,0 +1,237 @@
package net.filebot.web;
import static net.filebot.web.WebRequest.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.Icon;
import net.filebot.ResourceManager;
import net.filebot.web.TMDbClient.MovieInfo;
import net.filebot.web.TMDbClient.MovieInfo.MovieProperty;
import net.filebot.web.TMDbClient.Person;
import net.filebot.web.TMDbClient.Trailer;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import com.cedarsoftware.util.io.JsonObject;
import com.cedarsoftware.util.io.JsonReader;
public class OMDbClient implements MovieIdentificationService {
private static final FloodLimit REQUEST_LIMIT = new FloodLimit(20, 12, TimeUnit.SECONDS);
private final String protocol = "http";
private final String host = "www.omdbapi.com";
@Override
public String getName() {
return "OMDb";
}
@Override
public Icon getIcon() {
return ResourceManager.getIcon("search.omdb");
}
protected int getImdbId(String link) {
Matcher matcher = Pattern.compile("tt(\\d{7})").matcher(link);
if (matcher.find()) {
return Integer.parseInt(matcher.group(1));
}
// pattern not found
throw new IllegalArgumentException(String.format("Cannot find imdb id: %s", link));
}
private static Object[] array(Object node, String key) {
Object value = ((Map<?, ?>) node).get(key);
return value == null ? new Object[0] : ((JsonObject<?, ?>) value).getArray();
}
@Override
public List<Movie> searchMovie(String query, Locale locale) throws IOException {
// query by name with year filter if possible
Matcher nameYear = Pattern.compile("(.+)\\b(19\\d{2}|20\\d{2})$").matcher(query);
if (nameYear.matches()) {
return searchMovie(nameYear.group(1).trim(), Integer.parseInt(nameYear.group(2)));
} else {
return searchMovie(query, -1);
}
}
public List<Movie> searchMovie(String movieName, int movieYear) throws IOException {
Map<String, Object> param = new LinkedHashMap<String, Object>(2);
param.put("s", movieName);
if (movieYear > 0) {
param.put("y", movieYear);
}
Map<?, ?> response = request(param, REQUEST_LIMIT);
List<Movie> result = new ArrayList<Movie>();
for (Object it : array(response, "Search")) {
Map<String, String> info = getInfoMap(it);
if ("movie".equals(info.get("Type"))) {
result.add(getMovie(info));
}
}
return result;
}
@Override
public Movie getMovieDescriptor(int imdbid, Locale locale) throws Exception {
if (imdbid <= 0) {
throw new IllegalArgumentException("Illegal ID: " + imdbid);
}
// request full movie info for given id
return getMovie(getMovieInfo(imdbid, null, null, false));
}
public Map<String, String> getInfoMap(Object node) {
Map<String, String> info = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
if (node instanceof Map) {
for (Entry<?, ?> it : ((Map<?, ?>) node).entrySet()) {
if (it.getKey() != null && it.getValue() != null) {
info.put(it.getKey().toString(), it.getValue().toString());
}
}
}
return info;
}
public Movie getMovie(Map<?, ?> info) {
try {
String name = info.get("Title").toString();
int year = Integer.parseInt(info.get("Year").toString());
int imdbid = Integer.parseInt(info.get("imdbID").toString().replace("tt", ""));
return new Movie(name, year, imdbid, -1);
} catch (Exception e) {
throw new IllegalArgumentException("Illegal fields: " + info);
}
}
@Override
public Map<File, Movie> getMovieDescriptors(Collection<File> movieFiles, Locale locale) throws Exception {
throw new UnsupportedOperationException();
}
public Map<?, ?> request(Map<String, Object> parameters, final FloodLimit limit) throws IOException {
URL url = new URL(protocol, host, "/?" + encodeParameters(parameters, true));
CachedResource<String> json = new CachedResource<String>(url.toString(), String.class, CachedResource.ONE_WEEK) {
@Override
public String process(ByteBuffer data) throws Exception {
return Charset.forName("UTF-8").decode(data).toString();
}
@Override
protected ByteBuffer fetchData(URL url, long lastModified) throws IOException {
try {
if (limit != null) {
limit.acquirePermit();
}
return super.fetchData(url, lastModified);
} catch (FileNotFoundException e) {
return ByteBuffer.allocate(0);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
protected Cache getCache() {
return CacheManager.getInstance().getCache("web-datasource-lv2");
}
};
return JsonReader.jsonToMaps(json.get());
}
public Map<String, String> getMovieInfo(Integer i, String t, String y, boolean tomatoes) throws IOException {
// e.g. http://www.imdbapi.com/?i=tt0379786&r=xml&tomatoes=true
Map<String, Object> param = new LinkedHashMap<String, Object>(2);
if (i != null) {
param.put("i", String.format("tt%07d", i));
}
if (t != null) {
param.put("t", t);
}
if (y != null) {
param.put("y", y);
}
param.put("tomatoes", String.valueOf(tomatoes));
return getInfoMap(request(param, REQUEST_LIMIT));
}
public MovieInfo getMovieInfo(Movie movie) throws IOException {
Map<String, String> data = movie.getImdbId() > 0 ? getMovieInfo(movie.getImdbId(), null, null, false) : getMovieInfo(null, movie.getName(), String.valueOf(movie.getYear()), false);
// sanity check
if (!Boolean.parseBoolean(data.get("response"))) {
throw new IllegalArgumentException("Movie not found: " + data);
}
Map<MovieProperty, String> fields = new EnumMap<MovieProperty, String>(MovieProperty.class);
fields.put(MovieProperty.title, data.get("title"));
fields.put(MovieProperty.certification, data.get("rated"));
fields.put(MovieProperty.runtime, data.get("runtime"));
fields.put(MovieProperty.tagline, data.get("plot"));
fields.put(MovieProperty.vote_average, data.get("imdbRating"));
fields.put(MovieProperty.vote_count, data.get("imdbVotes").replaceAll("\\D", ""));
fields.put(MovieProperty.imdb_id, data.get("imdbID"));
fields.put(MovieProperty.poster_path, data.get("poster"));
// convert release date to yyyy-MM-dd
SimpleDate released = SimpleDate.parse(data.get("released"), "dd MMM yyyy");
if (released != null) {
fields.put(MovieProperty.release_date, released.format("yyyy-MM-dd"));
} else {
SimpleDate year = SimpleDate.parse(data.get("year"), "yyyy");
if (year != null) {
fields.put(MovieProperty.release_date, year.format("yyyy-MM-dd"));
}
}
List<String> genres = new ArrayList<String>();
for (String it : data.get("genre").split(",")) {
genres.add(it.trim());
}
List<Person> actors = new ArrayList<Person>();
for (String it : data.get("actors").split(",")) {
actors.add(new Person(it.trim(), null, null));
}
for (String director : data.get("director").split(",")) {
actors.add(new Person(director, null, "Director"));
}
for (String writer : data.get("writer").split(",")) {
actors.add(new Person(writer, null, "Writer"));
}
return new MovieInfo(fields, new ArrayList<String>(0), genres, new ArrayList<String>(0), actors, new ArrayList<Trailer>(0));
}
}

View File

@ -3,19 +3,18 @@ package net.filebot.web;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Locale;
import net.filebot.web.TMDbClient.MovieInfo;
import org.junit.Test;
public class IMDbClientTest {
public class OMDbClientTest {
private final IMDbClient imdb = new IMDbClient();
private final OMDbClient client = new OMDbClient();
@Test
public void searchMovie1() throws Exception {
List<Movie> results = imdb.searchMovie("Avatar", null);
List<Movie> results = client.searchMovie("Avatar", null);
Movie movie = results.get(0);
assertEquals("Avatar", movie.getName());
@ -25,7 +24,7 @@ public class IMDbClientTest {
@Test
public void searchMovie2() throws Exception {
List<Movie> results = imdb.searchMovie("The Illusionist", null);
List<Movie> results = client.searchMovie("The Illusionist", null);
Movie movie = results.get(0);
assertEquals("The Illusionist", movie.getName());
@ -35,7 +34,7 @@ public class IMDbClientTest {
@Test
public void searchMovie3() throws Exception {
List<Movie> results = imdb.searchMovie("Amélie", null);
List<Movie> results = client.searchMovie("Amélie", null);
Movie movie = results.get(0);
assertEquals("Amélie", movie.getName());
@ -45,7 +44,7 @@ public class IMDbClientTest {
@Test
public void searchMovie4() throws Exception {
List<Movie> results = imdb.searchMovie("Heat", null);
List<Movie> results = client.searchMovie("Heat", null);
Movie movie = results.get(0);
assertEquals("Heat", movie.getName());
@ -53,19 +52,9 @@ public class IMDbClientTest {
assertEquals(113277, movie.getImdbId(), 0);
}
@Test
public void searchMovie5() throws Exception {
List<Movie> results = imdb.searchMovie("Det sjunde inseglet", null);
Movie movie = results.get(0);
assertEquals("The Seventh Seal", movie.getName());
assertEquals(1957, movie.getYear());
assertEquals(50976, movie.getImdbId(), 0);
}
@Test
public void searchMovie6() throws Exception {
List<Movie> results = imdb.searchMovie("Drive 2011", null);
List<Movie> results = client.searchMovie("Drive 2011", null);
Movie movie = results.get(0);
assertEquals("Drive", movie.getName());
@ -73,20 +62,9 @@ public class IMDbClientTest {
assertEquals(780504, movie.getImdbId(), 0);
}
@Test
public void searchMovieRedirect() throws Exception {
List<Movie> results = imdb.searchMovie("(500) Days of Summer (2009)", null);
Movie movie = results.get(0);
assertEquals("(500) Days of Summer", movie.getName());
assertEquals(2009, movie.getYear());
assertEquals(1022603, movie.getImdbId(), 0);
}
@Test
public void getMovieDescriptor1() throws Exception {
Movie movie = imdb.getMovieDescriptor(499549, null);
Movie movie = client.getMovieDescriptor(499549, null);
assertEquals("Avatar", movie.getName());
assertEquals(2009, movie.getYear());
@ -95,7 +73,7 @@ public class IMDbClientTest {
@Test
public void getMovieDescriptor2() throws Exception {
Movie movie = imdb.getMovieDescriptor(211915, null);
Movie movie = client.getMovieDescriptor(211915, null);
assertEquals("Amélie", movie.getName());
assertEquals(2001, movie.getYear());
@ -104,7 +82,7 @@ public class IMDbClientTest {
@Test
public void getMovieDescriptor3() throws Exception {
Movie movie = imdb.getMovieDescriptor(75610, null);
Movie movie = client.getMovieDescriptor(75610, null);
assertEquals("21 Up", movie.getName());
assertEquals(1977, movie.getYear());
@ -113,7 +91,7 @@ public class IMDbClientTest {
@Test
public void getMovieDescriptor4() throws Exception {
Movie movie = imdb.getMovieDescriptor(369702, null);
Movie movie = client.getMovieDescriptor(369702, null);
assertEquals("The Sea Inside", movie.getName());
assertEquals(2004, movie.getYear());
@ -122,34 +100,16 @@ public class IMDbClientTest {
@Test
public void getMovieDescriptor5() throws Exception {
Movie movie = imdb.getMovieDescriptor(1020960, null);
Movie movie = client.getMovieDescriptor(1020960, null);
assertEquals("God, the Universe and Everything Else", movie.getName());
assertEquals(1988, movie.getYear());
assertEquals(1020960, movie.getImdbId(), 0);
}
@Test
public void getAkaMovieDescriptor() throws Exception {
Movie movie = imdb.getMovieDescriptor(106559, Locale.ENGLISH);
assertEquals("Ching Se", movie.getName());
assertEquals(1993, movie.getYear());
assertEquals(106559, movie.getImdbId(), 0);
}
@Test
public void getAkaMovieDescriptorExtra() throws Exception {
Movie movie = imdb.getMovieDescriptor(470761, Locale.ENGLISH);
assertEquals("First Born", movie.getName());
assertEquals(2007, movie.getYear());
assertEquals(470761, movie.getImdbId(), 0);
}
@Test
public void getImdbApiMovieInfoReleasedNA() throws Exception {
MovieInfo movie = imdb.getImdbApiMovieInfo(new Movie(null, -1, 1287357, -1));
MovieInfo movie = client.getMovieInfo(new Movie(null, -1, 1287357, -1));
assertEquals("Sommersonntag", movie.getName());
assertEquals(2008, movie.getReleased().getYear());
assertEquals("2008-06-07", movie.getReleased().toString());

View File

@ -5,7 +5,7 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AnidbClientTest.class, TVRageClientTest.class, TheTVDBClientTest.class, TMDbClientTest.class, IMDbClientTest.class, OpenSubtitlesXmlRpcTest.class, AcoustIDClientTest.class })
@SuiteClasses({ AnidbClientTest.class, TVRageClientTest.class, TheTVDBClientTest.class, TMDbClientTest.class, OMDbClientTest.class, OpenSubtitlesXmlRpcTest.class, AcoustIDClientTest.class })
public class WebTestSuite {
}