From f6dbc24caf1f940b5f5e7abc57396338ab9338df Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 8 Feb 2012 12:24:36 +0000 Subject: [PATCH] + introduce {meta} tvseries/movie metadata binding --- .../filebot/format/MediaBindingBean.java | 14 ++ .../sourceforge/filebot/web/TMDbClient.java | 126 +++++++++++++++++- .../filebot/web/TheTVDBClient.java | 11 ++ .../filebot/web/TMDbClientTest.java | 4 +- 4 files changed, 151 insertions(+), 4 deletions(-) diff --git a/source/net/sourceforge/filebot/format/MediaBindingBean.java b/source/net/sourceforge/filebot/format/MediaBindingBean.java index 6f5beb3f..6e3f4756 100644 --- a/source/net/sourceforge/filebot/format/MediaBindingBean.java +++ b/source/net/sourceforge/filebot/format/MediaBindingBean.java @@ -33,6 +33,7 @@ public class MediaBindingBean { private final Object infoObject; private final File mediaFile; private MediaInfo mediaInfo; + private Object metaInfo; public MediaBindingBean(Object infoObject, File mediaFile) { @@ -303,6 +304,19 @@ public class MediaBindingBean { } + @Define("meta") + public synchronized Object getMetaInfo() throws Exception { + if (metaInfo == null) { + if (infoObject instanceof Episode) + metaInfo = WebServices.TheTVDB.getSeriesInfoByName(((Episode) infoObject).getSeriesName(), Locale.ENGLISH); + if (infoObject instanceof Movie) + metaInfo = WebServices.TMDb.getMovieInfo((Movie) infoObject, Locale.ENGLISH); + } + + return metaInfo; + } + + @Define("media") public Object getGeneralMediaInfo() { return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.General, 0)); diff --git a/source/net/sourceforge/filebot/web/TMDbClient.java b/source/net/sourceforge/filebot/web/TMDbClient.java index 37c02da8..0df430d1 100644 --- a/source/net/sourceforge/filebot/web/TMDbClient.java +++ b/source/net/sourceforge/filebot/web/TMDbClient.java @@ -23,12 +23,14 @@ import java.util.logging.Logger; import javax.swing.Icon; +import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; import net.sourceforge.filebot.ResourceManager; import net.sourceforge.filebot.web.TMDbClient.Artwork.ArtworkProperty; import net.sourceforge.filebot.web.TMDbClient.MovieInfo.MovieProperty; +import net.sourceforge.filebot.web.TMDbClient.Person.PersonProperty; public class TMDbClient implements MovieIdentificationService { @@ -138,12 +140,37 @@ public class TMDbClient implements MovieIdentificationService { public MovieInfo getMovieInfo(Movie movie, Locale locale) throws Exception { - return getMovieInfoByIMDbID(movie.getImdbId(), locale); + if (movie.getImdbId() >= 0) { + return getMovieInfoByIMDbID(movie.getImdbId(), Locale.ENGLISH); + } else { + return getMovieInfoByName(movie.getName(), movie.getYear(), Locale.ENGLISH); + } + } + + + public MovieInfo getMovieInfoByName(String name, int year, Locale locale) throws Exception { + for (Movie it : searchMovie(name, locale)) { + if (name.equalsIgnoreCase(it.getName()) && year == it.getYear()) { + return getMovieInfo(it, locale); + } + } + + return null; } public MovieInfo getMovieInfoByIMDbID(int imdbid, Locale locale) throws Exception { + if (imdbid < 0) + throw new IllegalArgumentException("Illegal IMDb ID: " + imdbid); + URL resource = getResource("Movie.imdbLookup", String.format("tt%07d", imdbid), locale); + Document dom = getDocument(resource); + + // get complete movie info via tmdbid lookup + resource = getResource("Movie.getInfo", selectString("//movie/id", dom), locale); + dom = getDocument(resource); + + // select info from xml Node node = selectNode("//movie", getDocument(resource)); Map movieProperties = new EnumMap(MovieProperty.class); @@ -165,7 +192,16 @@ public class TMDbClient implements MovieIdentificationService { artwork.add(new Artwork(artworkProperties)); } - return new MovieInfo(movieProperties, genres, artwork); + List cast = new ArrayList(); + for (Node image : selectNodes("//person", node)) { + Map personProperties = new EnumMap(PersonProperty.class); + for (PersonProperty property : PersonProperty.values()) { + personProperties.put(property, getAttribute(property.name(), image)); + } + cast.add(new Person(personProperties)); + } + + return new MovieInfo(movieProperties, genres, cast, artwork); } @@ -175,6 +211,7 @@ public class TMDbClient implements MovieIdentificationService { translated, adult, language, + original_name, name, type, id, @@ -183,6 +220,7 @@ public class TMDbClient implements MovieIdentificationService { overview, votes, rating, + tagline, certification, released, runtime @@ -191,6 +229,7 @@ public class TMDbClient implements MovieIdentificationService { protected Map fields; protected String[] genres; + protected Person[] cast; protected Artwork[] images; @@ -199,9 +238,10 @@ public class TMDbClient implements MovieIdentificationService { } - protected MovieInfo(Map fields, List genres, List images) { + protected MovieInfo(Map fields, List genres, List cast, List images) { this.fields = new EnumMap(fields); this.genres = genres.toArray(new String[0]); + this.cast = cast.toArray(new Person[0]); this.images = images.toArray(new Artwork[0]); } @@ -235,6 +275,11 @@ public class TMDbClient implements MovieIdentificationService { } + public String getOriginalName() { + return get(MovieProperty.original_name); + } + + public String getName() { return get(MovieProperty.name); } @@ -296,6 +341,11 @@ public class TMDbClient implements MovieIdentificationService { } + public String getTagline() { + return get(MovieProperty.tagline); + } + + public String getCertification() { // e.g. PG-13 return get(MovieProperty.certification); @@ -322,6 +372,11 @@ public class TMDbClient implements MovieIdentificationService { } + public List getCast() { + return unmodifiableList(asList(cast)); + } + + public List getImages() { return unmodifiableList(asList(images)); } @@ -411,4 +466,69 @@ public class TMDbClient implements MovieIdentificationService { } } + + public static class Person implements Serializable { + + public static enum PersonProperty { + name, + character, + job, + thumb, + department + } + + + protected Map fields; + + + protected Person() { + // used by serializer + } + + + protected Person(Map fields) { + this.fields = new EnumMap(fields); + } + + + public String get(Object key) { + return fields.get(PersonProperty.valueOf(key.toString())); + } + + + public String get(PersonProperty key) { + return fields.get(key); + } + + + public String getName() { + return get(PersonProperty.name); + } + + + public String getJob() { + return get(PersonProperty.job); + } + + + public String getDepartment() { + return get(PersonProperty.department); + } + + + public URL getThumb() { + try { + return new URL(get(PersonProperty.thumb)); + } catch (Exception e) { + return null; + } + } + + + @Override + public String toString() { + return fields.toString(); + } + } + } diff --git a/source/net/sourceforge/filebot/web/TheTVDBClient.java b/source/net/sourceforge/filebot/web/TheTVDBClient.java index 834417e4..782b3c14 100644 --- a/source/net/sourceforge/filebot/web/TheTVDBClient.java +++ b/source/net/sourceforge/filebot/web/TheTVDBClient.java @@ -403,6 +403,17 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { } + public SeriesInfo getSeriesInfoByName(String name, Locale locale) throws Exception { + for (SearchResult it : search(name, locale)) { + if (name.equalsIgnoreCase(it.getName())) { + return getSeriesInfo((TheTVDBSearchResult) it, locale); + } + } + + return null; + } + + public SeriesInfo getSeriesInfo(TheTVDBSearchResult searchResult, Locale locale) throws Exception { // check cache first SeriesInfo cachedItem = getCache().getData("seriesInfo", searchResult.seriesId, locale, SeriesInfo.class); diff --git a/test/net/sourceforge/filebot/web/TMDbClientTest.java b/test/net/sourceforge/filebot/web/TMDbClientTest.java index 99fc14c6..56f9936e 100644 --- a/test/net/sourceforge/filebot/web/TMDbClientTest.java +++ b/test/net/sourceforge/filebot/web/TMDbClientTest.java @@ -23,7 +23,7 @@ public class TMDbClientTest { List result = tmdb.searchMovie("Serenity", Locale.CHINESE); Movie movie = result.get(0); - assertEquals("冲出宁静号", movie.getName()); + assertEquals("冲出???", movie.getName()); assertEquals(2005, movie.getYear()); assertEquals(379786, movie.getImdbId()); } @@ -57,6 +57,8 @@ public class TMDbClientTest { assertEquals("Transformers", movie.getName()); assertEquals("2007-07-03", movie.getReleased().toString()); assertEquals("Adventure", movie.getGenres().get(0)); + assertEquals("Deborah Lynn Scott", movie.getCast().get(0).getName()); + assertEquals("Costume Design", movie.getCast().get(0).getJob()); assertEquals("thumb", movie.getImages().get(0).getSize()); assertEquals("http://cf2.imgobject.com/t/p/w92/bgSHbGEA1OM6qDs3Qba4VlSZsNG.jpg", movie.getImages().get(0).getUrl().toString()); }