* revert back to imdbapi code since it's been moved and renamed to omdbapi

This commit is contained in:
Reinhard Pointner 2012-10-26 09:57:28 +00:00
parent 649a9f1e4d
commit e3dde62618
3 changed files with 81 additions and 23 deletions

View File

@ -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<String, String> 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<JSONObject> data = new CachedResource<JSONObject>(url, JSONObject.class, 7 * 24 * 60 * 60 * 1000) {
@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, 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<String, String> data = movie.getImdbId() > 0 ? getImdbApiData(movie.getImdbId(), null, null) : getImdbApiData(null, movie.getName(), String.valueOf(movie.getYear()));
Map<String, String> 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<MovieProperty, String> fields = new EnumMap<MovieProperty, String>(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<String>(0), new ArrayList<String>(0), new ArrayList<Person>(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<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, genres, new ArrayList<String>(0), actors);
}
}

View File

@ -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<Person> getPeope() {
public List<Person> 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<String> getActors() {
List<String> actors = new ArrayList<String>();
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();

View File

@ -362,9 +362,14 @@
</tr>
<tr>
<td>info</td>
<td>TheMovieDB / TheTVDB info</td>
<td><a href="http://www.themoviedb.org/">TheMovieDB</a> / <a href="http://thetvdb.com/">TheTVDB</a> info</td>
<td>&lt;any movie / series info&gt;</td>
</tr>
<tr>
<td>imdb</td>
<td><a href="http://www.omdbapi.com/">OMDb</a> info</td>
<td>&lt;any movie info&gt;</td>
</tr>
<tr>
<td>file</td>
<td>file object</td>