diff --git a/source/net/sourceforge/filebot/web/IMDbClient.java b/source/net/sourceforge/filebot/web/IMDbClient.java index 75410726..6c87dcbf 100644 --- a/source/net/sourceforge/filebot/web/IMDbClient.java +++ b/source/net/sourceforge/filebot/web/IMDbClient.java @@ -17,6 +17,7 @@ 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; @@ -35,8 +36,6 @@ import net.sourceforge.filebot.web.TMDbClient.MovieInfo; import net.sourceforge.filebot.web.TMDbClient.MovieInfo.MovieProperty; import net.sourceforge.filebot.web.TMDbClient.Person; -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; @@ -189,14 +188,20 @@ public class IMDbClient implements MovieIdentificationService { } - @SuppressWarnings("unchecked") - public Map getImdbApiData(Integer i, String t, String y) throws IOException { - String url = i != null ? String.format("http://www.deanclatworthy.com/imdb/?id=tt%07d", i) : String.format("http://www.deanclatworthy.com/imdb/?q=%s&year=%s", encode(t), encode(y)); - CachedResource data = new CachedResource(url, JSONObject.class, 7 * 24 * 60 * 60 * 1000) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + public Map 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 data = new CachedResource(url, HashMap.class, 7 * 24 * 60 * 60 * 1000) { @Override - public JSONObject process(ByteBuffer data) throws Exception { - return (JSONObject) JSONValue.parse(Charset.forName("UTF-8").decode(data).toString()); + 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; } @@ -211,18 +216,47 @@ public class IMDbClient implements MovieIdentificationService { public MovieInfo getImdbApiMovieInfo(Movie movie) throws IOException { - Map data = movie.getImdbId() > 0 ? getImdbApiData(movie.getImdbId(), null, null) : getImdbApiData(null, movie.getName(), String.valueOf(movie.getYear())); + Map data = movie.getImdbId() > 0 ? getImdbApiData(movie.getImdbId(), "", "", false) : getImdbApiData(null, movie.getName(), String.valueOf(movie.getYear()), false); // sanity check - if (data.get("error") != null) { - throw new IllegalArgumentException(data.get("error")); + if (!Boolean.parseBoolean(data.get("response"))) { + throw new IllegalArgumentException("Movie not found: " + data); } Map fields = new EnumMap(MovieProperty.class); - fields.put(MovieProperty.vote_average, data.get("rating")); - fields.put(MovieProperty.vote_count, data.get("votes")); - fields.put(MovieProperty.imdb_id, data.get("imdbid")); + 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")); - return new MovieInfo(fields, new ArrayList(0), new ArrayList(0), new ArrayList(0)); + // convert release date to yyyy-MM-dd + Date released = Date.parse(data.get("Released"), "dd MMM yyyy"); + if (released != null) { + fields.put(MovieProperty.release_date, released.format("yyyy-MM-dd")); + } + + List genres = new ArrayList(); + for (String it : data.get("genre").split(",")) { + genres.add(it.trim()); + } + + List actors = new ArrayList(); + 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, genres, new ArrayList(0), actors); } } diff --git a/source/net/sourceforge/filebot/web/TMDbClient.java b/source/net/sourceforge/filebot/web/TMDbClient.java index 51b92139..6f129896 100644 --- a/source/net/sourceforge/filebot/web/TMDbClient.java +++ b/source/net/sourceforge/filebot/web/TMDbClient.java @@ -435,12 +435,8 @@ public class TMDbClient implements MovieIdentificationService { } - public Integer getRuntime() { - try { - return new Integer(get(MovieProperty.runtime)); - } catch (Exception e) { - return null; - } + public String getRuntime() { + return get(MovieProperty.runtime); } @@ -449,7 +445,7 @@ public class TMDbClient implements MovieIdentificationService { } - public List getPeope() { + public List getPeople() { return unmodifiableList(asList(people)); } @@ -474,6 +470,15 @@ public class TMDbClient implements MovieIdentificationService { } + public String getWriter() { + for (Person person : people) { + if (person.isWriter()) + return person.getName(); + } + return null; + } + + public List getActors() { List actors = new ArrayList(); for (Person actor : getCast()) { @@ -483,6 +488,15 @@ public class TMDbClient implements MovieIdentificationService { } + public URL getPoster() { + try { + return new URL(get(MovieProperty.poster_path)); + } catch (Exception e) { + return null; + } + } + + @Override public String toString() { return fields.toString(); @@ -552,6 +566,11 @@ public class TMDbClient implements MovieIdentificationService { } + public boolean isWriter() { + return "Writer".equals(getJob()); + } + + @Override public String toString() { return fields.toString(); diff --git a/website/naming.html b/website/naming.html index 33940261..b5128993 100644 --- a/website/naming.html +++ b/website/naming.html @@ -362,9 +362,14 @@ info - TheMovieDB / TheTVDB info + TheMovieDB / TheTVDB info <any movie / series info> + + imdb + OMDb info + <any movie info> + file file object