* use thetvdb for tvseries imdb lookups

This commit is contained in:
Reinhard Pointner 2011-12-14 01:19:36 +00:00
parent 710010b4d7
commit 66af29de7f
3 changed files with 76 additions and 56 deletions

View File

@ -22,6 +22,8 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -29,7 +31,6 @@ import net.sourceforge.filebot.MediaTypes;
import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.WebServices;
import net.sourceforge.filebot.similarity.SeriesNameMatcher; import net.sourceforge.filebot.similarity.SeriesNameMatcher;
import net.sourceforge.filebot.web.CachedResource; import net.sourceforge.filebot.web.CachedResource;
import net.sourceforge.filebot.web.Movie;
import net.sourceforge.filebot.web.SearchResult; import net.sourceforge.filebot.web.SearchResult;
import net.sourceforge.filebot.web.TheTVDBClient.TheTVDBSearchResult; import net.sourceforge.filebot.web.TheTVDBClient.TheTVDBSearchResult;
@ -42,8 +43,12 @@ public class ReleaseInfo {
// don't allow duplicates // don't allow duplicates
Map<String, String> names = new LinkedHashMap<String, String>(); Map<String, String> names = new LinkedHashMap<String, String>();
for (SearchResult it : releaseInfo.lookupNameByInfoFile(files, Locale.ENGLISH)) { try {
names.put(it.getName().toLowerCase(), it.getName()); for (SearchResult it : releaseInfo.lookupSeriesNameByInfoFile(files, Locale.ENGLISH)) {
names.put(it.getName().toLowerCase(), it.getName());
}
} catch (Exception e) {
Logger.getLogger(ReleaseInfo.class.getClass().getName()).log(Level.WARNING, "Failed to lookup info by id: " + e.getMessage());
} }
// match common word sequence and clean detected word sequence from unwanted elements // match common word sequence and clean detected word sequence from unwanted elements
@ -69,7 +74,7 @@ public class ReleaseInfo {
} }
public Set<SearchResult> lookupNameByInfoFile(Collection<File> files, Locale language) throws Exception { public Set<SearchResult> lookupSeriesNameByInfoFile(Collection<File> files, Locale language) throws Exception {
Set<SearchResult> names = new LinkedHashSet<SearchResult>(); Set<SearchResult> names = new LinkedHashSet<SearchResult>();
// search for id in sibling nfo files // search for id in sibling nfo files
@ -78,14 +83,14 @@ public class ReleaseInfo {
String text = new String(readFile(nfo), "UTF-8"); String text = new String(readFile(nfo), "UTF-8");
for (int imdbid : grepImdbId(text)) { for (int imdbid : grepImdbId(text)) {
Movie movie = WebServices.OpenSubtitles.getMovieDescriptor(imdbid, language); // movies and tv shows TheTVDBSearchResult series = WebServices.TheTVDB.lookupByIMDbID(imdbid, language);
if (movie != null) { if (series != null) {
names.add(movie); names.add(series);
} }
} }
for (int tvdbid : grepTheTvdbId(text)) { for (int tvdbid : grepTheTvdbId(text)) {
TheTVDBSearchResult series = WebServices.TheTVDB.lookup(tvdbid, language); // just tv shows TheTVDBSearchResult series = WebServices.TheTVDB.lookupByID(tvdbid, language);
if (series != null) { if (series != null) {
names.add(series); names.add(series);
} }

View File

@ -15,9 +15,9 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Scanner; import java.util.Scanner;
import java.util.Map.Entry;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -39,12 +39,12 @@ public class OpenSubtitlesXmlRpc {
private String token; private String token;
public OpenSubtitlesXmlRpc(String useragent) { public OpenSubtitlesXmlRpc(String useragent) {
this.useragent = useragent; this.useragent = useragent;
} }
/** /**
* Login as anonymous user * Login as anonymous user
*/ */
@ -52,7 +52,7 @@ public class OpenSubtitlesXmlRpc {
login("", "", "en"); login("", "", "en");
} }
/** /**
* This will login user. This method should be called always when starting talking with * This will login user. This method should be called always when starting talking with
* server. * server.
@ -69,7 +69,7 @@ public class OpenSubtitlesXmlRpc {
token = response.get("token").toString(); token = response.get("token").toString();
} }
/** /**
* This will logout user (ends session id). Call this function is before closing the client program. * This will logout user (ends session id). Call this function is before closing the client program.
*/ */
@ -84,23 +84,23 @@ public class OpenSubtitlesXmlRpc {
} }
} }
public boolean isLoggedOn() { public boolean isLoggedOn() {
return token != null; return token != null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, String> getServerInfo() throws XmlRpcFault { public Map<String, String> getServerInfo() throws XmlRpcFault {
return (Map<String, String>) invoke("ServerInfo", token); return (Map<String, String>) invoke("ServerInfo", token);
} }
public List<OpenSubtitlesSubtitleDescriptor> searchSubtitles(int imdbid, String... sublanguageids) throws XmlRpcFault { public List<OpenSubtitlesSubtitleDescriptor> searchSubtitles(int imdbid, String... sublanguageids) throws XmlRpcFault {
return searchSubtitles(singleton(Query.forImdbId(imdbid, sublanguageids))); return searchSubtitles(singleton(Query.forImdbId(imdbid, sublanguageids)));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<OpenSubtitlesSubtitleDescriptor> searchSubtitles(Collection<Query> queryList) throws XmlRpcFault { public List<OpenSubtitlesSubtitleDescriptor> searchSubtitles(Collection<Query> queryList) throws XmlRpcFault {
List<OpenSubtitlesSubtitleDescriptor> subtitles = new ArrayList<OpenSubtitlesSubtitleDescriptor>(); List<OpenSubtitlesSubtitleDescriptor> subtitles = new ArrayList<OpenSubtitlesSubtitleDescriptor>();
@ -119,7 +119,7 @@ public class OpenSubtitlesXmlRpc {
return subtitles; return subtitles;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<Movie> searchMoviesOnIMDB(String query) throws XmlRpcFault { public List<Movie> searchMoviesOnIMDB(String query) throws XmlRpcFault {
Map<?, ?> response = invoke("SearchMoviesOnIMDB", token, query); Map<?, ?> response = invoke("SearchMoviesOnIMDB", token, query);
@ -153,7 +153,7 @@ public class OpenSubtitlesXmlRpc {
return movies; return movies;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TryUploadResponse tryUploadSubtitles(SubFile... subtitles) throws XmlRpcFault { public TryUploadResponse tryUploadSubtitles(SubFile... subtitles) throws XmlRpcFault {
Map<String, SubFile> struct = new HashMap<String, SubFile>(); Map<String, SubFile> struct = new HashMap<String, SubFile>();
@ -171,7 +171,7 @@ public class OpenSubtitlesXmlRpc {
return new TryUploadResponse(uploadRequired, Property.asEnumMap(subtitleData)); return new TryUploadResponse(uploadRequired, Property.asEnumMap(subtitleData));
} }
public URI uploadSubtitles(BaseInfo baseInfo, SubFile... subtitles) throws XmlRpcFault { public URI uploadSubtitles(BaseInfo baseInfo, SubFile... subtitles) throws XmlRpcFault {
Map<String, Object> struct = new HashMap<String, Object>(); Map<String, Object> struct = new HashMap<String, Object>();
@ -189,7 +189,7 @@ public class OpenSubtitlesXmlRpc {
return URI.create(response.get("data").toString()); return URI.create(response.get("data").toString());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<String> detectLanguage(byte[] data) throws XmlRpcFault { public List<String> detectLanguage(byte[] data) throws XmlRpcFault {
// compress and base64 encode // compress and base64 encode
@ -205,7 +205,7 @@ public class OpenSubtitlesXmlRpc {
return languages; return languages;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, Integer> checkSubHash(Collection<String> hashes) throws XmlRpcFault { public Map<String, Integer> checkSubHash(Collection<String> hashes) throws XmlRpcFault {
Map<?, ?> response = invoke("CheckSubHash", token, hashes); Map<?, ?> response = invoke("CheckSubHash", token, hashes);
@ -221,7 +221,7 @@ public class OpenSubtitlesXmlRpc {
return subHashMap; return subHashMap;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, Movie> checkMovieHash(Collection<String> hashes) throws XmlRpcFault { public Map<String, Movie> checkMovieHash(Collection<String> hashes) throws XmlRpcFault {
Map<?, ?> response = invoke("CheckMovieHash", token, hashes); Map<?, ?> response = invoke("CheckMovieHash", token, hashes);
@ -246,12 +246,12 @@ public class OpenSubtitlesXmlRpc {
return movieHashMap; return movieHashMap;
} }
public Map<String, String> getSubLanguages() throws XmlRpcFault { public Map<String, String> getSubLanguages() throws XmlRpcFault {
return getSubLanguages("en"); return getSubLanguages("en");
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Movie getIMDBMovieDetails(int imdbid) throws XmlRpcFault { public Movie getIMDBMovieDetails(int imdbid) throws XmlRpcFault {
Map<?, ?> response = invoke("GetIMDBMovieDetails", token, imdbid); Map<?, ?> response = invoke("GetIMDBMovieDetails", token, imdbid);
@ -265,12 +265,13 @@ public class OpenSubtitlesXmlRpc {
return new Movie(name, year, imdbid); return new Movie(name, year, imdbid);
} catch (RuntimeException e) { } catch (RuntimeException e) {
// ignore, invalid response // ignore, invalid response
Logger.getLogger(getClass().getName()).log(Level.WARNING, String.format("Failed to lookup movie by imdbid %s: %s", imdbid, e.getMessage()));
} }
return null; return null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, String> getSubLanguages(String languageCode) throws XmlRpcFault { public Map<String, String> getSubLanguages(String languageCode) throws XmlRpcFault {
Map<String, List<Map<String, String>>> response = (Map<String, List<Map<String, String>>>) invoke("GetSubLanguages", languageCode); Map<String, List<Map<String, String>>> response = (Map<String, List<Map<String, String>>>) invoke("GetSubLanguages", languageCode);
@ -284,12 +285,12 @@ public class OpenSubtitlesXmlRpc {
return subLanguageMap; return subLanguageMap;
} }
public void noOperation() throws XmlRpcFault { public void noOperation() throws XmlRpcFault {
invoke("NoOperation", token); invoke("NoOperation", token);
} }
protected Map<?, ?> invoke(String method, Object... arguments) throws XmlRpcFault { protected Map<?, ?> invoke(String method, Object... arguments) throws XmlRpcFault {
try { try {
XmlRpcClient rpc = new XmlRpcClient(getXmlRpcUrl(), false); XmlRpcClient rpc = new XmlRpcClient(getXmlRpcUrl(), false);
@ -308,7 +309,7 @@ public class OpenSubtitlesXmlRpc {
} }
} }
protected URL getXmlRpcUrl() { protected URL getXmlRpcUrl() {
try { try {
return new URL("http://api.opensubtitles.org/xml-rpc"); return new URL("http://api.opensubtitles.org/xml-rpc");
@ -318,7 +319,7 @@ public class OpenSubtitlesXmlRpc {
} }
} }
protected static String encodeData(byte[] data) { protected static String encodeData(byte[] data) {
try { try {
DeflaterInputStream compressedDataStream = new DeflaterInputStream(new ByteArrayInputStream(data)); DeflaterInputStream compressedDataStream = new DeflaterInputStream(new ByteArrayInputStream(data));
@ -335,7 +336,7 @@ public class OpenSubtitlesXmlRpc {
} }
} }
/** /**
* Check whether status is OK or not * Check whether status is OK or not
* *
@ -357,7 +358,7 @@ public class OpenSubtitlesXmlRpc {
} }
} }
public static final class Query extends HashMap<String, Object> { public static final class Query extends HashMap<String, Object> {
private Query(String imdbid, String... sublanguageids) { private Query(String imdbid, String... sublanguageids) {
@ -365,119 +366,119 @@ public class OpenSubtitlesXmlRpc {
put("sublanguageid", join(sublanguageids, ",")); put("sublanguageid", join(sublanguageids, ","));
} }
private Query(String moviehash, String moviebytesize, String... sublanguageids) { private Query(String moviehash, String moviebytesize, String... sublanguageids) {
put("moviehash", moviehash); put("moviehash", moviehash);
put("moviebytesize", moviebytesize); put("moviebytesize", moviebytesize);
put("sublanguageid", join(sublanguageids, ",")); put("sublanguageid", join(sublanguageids, ","));
} }
public static Query forHash(String moviehash, long moviebytesize, String... sublanguageids) { public static Query forHash(String moviehash, long moviebytesize, String... sublanguageids) {
return new Query(moviehash, Long.toString(moviebytesize), sublanguageids); return new Query(moviehash, Long.toString(moviebytesize), sublanguageids);
} }
public static Query forImdbId(int imdbid, String... sublanguageids) { public static Query forImdbId(int imdbid, String... sublanguageids) {
return new Query(Integer.toString(imdbid), sublanguageids); return new Query(Integer.toString(imdbid), sublanguageids);
} }
} }
public static final class BaseInfo extends HashMap<String, Object> { public static final class BaseInfo extends HashMap<String, Object> {
public void setIDMovieImdb(int imdb) { public void setIDMovieImdb(int imdb) {
put("idmovieimdb", Integer.toString(imdb)); put("idmovieimdb", Integer.toString(imdb));
} }
public void setSubLanguageID(String sublanguageid) { public void setSubLanguageID(String sublanguageid) {
put("sublanguageid", sublanguageid); put("sublanguageid", sublanguageid);
} }
public void setMovieReleaseName(String moviereleasename) { public void setMovieReleaseName(String moviereleasename) {
put("moviereleasename", moviereleasename); put("moviereleasename", moviereleasename);
} }
public void setMovieAka(String movieaka) { public void setMovieAka(String movieaka) {
put("movieaka", movieaka); put("movieaka", movieaka);
} }
public void setSubAuthorComment(String subauthorcomment) { public void setSubAuthorComment(String subauthorcomment) {
put("subauthorcomment", subauthorcomment); put("subauthorcomment", subauthorcomment);
} }
} }
public static final class SubFile extends HashMap<String, Object> { public static final class SubFile extends HashMap<String, Object> {
public void setSubHash(String subhash) { public void setSubHash(String subhash) {
put("subhash", subhash); put("subhash", subhash);
} }
public void setSubFileName(String subfilename) { public void setSubFileName(String subfilename) {
put("subfilename", subfilename); put("subfilename", subfilename);
} }
public void setMovieHash(String moviehash) { public void setMovieHash(String moviehash) {
put("moviehash", moviehash); put("moviehash", moviehash);
} }
public void setMovieByteSize(long moviebytesize) { public void setMovieByteSize(long moviebytesize) {
put("moviebytesize", Long.toString(moviebytesize)); put("moviebytesize", Long.toString(moviebytesize));
} }
public void setMovieTimeMS(int movietimems) { public void setMovieTimeMS(int movietimems) {
put("movietimems", movietimems); put("movietimems", movietimems);
} }
public void setMovieFrames(int movieframes) { public void setMovieFrames(int movieframes) {
put("movieframes", movieframes); put("movieframes", movieframes);
} }
public void setMovieFPS(double moviefps) { public void setMovieFPS(double moviefps) {
put("moviefps", moviefps); put("moviefps", moviefps);
} }
public void setMovieFileName(String moviefilename) { public void setMovieFileName(String moviefilename) {
put("moviefilename", moviefilename); put("moviefilename", moviefilename);
} }
public void setSubContent(byte[] data) { public void setSubContent(byte[] data) {
put("subcontent", encodeData(data)); put("subcontent", encodeData(data));
} }
} }
public static final class TryUploadResponse { public static final class TryUploadResponse {
private final boolean uploadRequired; private final boolean uploadRequired;
private final Map<Property, String> subtitleData; private final Map<Property, String> subtitleData;
private TryUploadResponse(boolean uploadRequired, Map<Property, String> subtitleData) { private TryUploadResponse(boolean uploadRequired, Map<Property, String> subtitleData) {
this.uploadRequired = uploadRequired; this.uploadRequired = uploadRequired;
this.subtitleData = subtitleData; this.subtitleData = subtitleData;
} }
public boolean isUploadRequired() { public boolean isUploadRequired() {
return uploadRequired; return uploadRequired;
} }
public Map<Property, String> getSubtitleData() { public Map<Property, String> getSubtitleData() {
return subtitleData; return subtitleData;
} }

View File

@ -184,21 +184,35 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
public TheTVDBSearchResult lookup(int id, Locale language) throws Exception { public TheTVDBSearchResult lookupByID(int id, Locale language) throws Exception {
try { try {
URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml"); URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml");
Document baseRecord = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(baseRecordLocation.openStream()); Document baseRecord = getDocument(baseRecordLocation);
String name = selectString("//SeriesName", baseRecord); String name = selectString("//SeriesName", baseRecord);
return new TheTVDBSearchResult(name, id); return new TheTVDBSearchResult(name, id);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// illegal series id // illegal series id
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to retrieve base series record", e); Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to retrieve base series record: " + e.getMessage());
return null; return null;
} }
} }
public TheTVDBSearchResult lookupByIMDbID(int imdbid, Locale language) throws Exception {
URL query = getResource(MirrorType.XML, "/api/GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + language.getLanguage());
Document dom = getDocument(query);
String id = selectString("//seriesid", dom);
String name = selectString("//SeriesName", dom);
if (id == null || id.isEmpty() || name == null || name.isEmpty())
return null;
return new TheTVDBSearchResult(name, Integer.parseInt(id));
}
@Override @Override
public URI getEpisodeListLink(SearchResult searchResult) { public URI getEpisodeListLink(SearchResult searchResult) {
int seriesId = ((TheTVDBSearchResult) searchResult).getSeriesId(); int seriesId = ((TheTVDBSearchResult) searchResult).getSeriesId();