1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-16 14:25:02 -05:00

* make sure OpenSubtitles language detection is cached

This commit is contained in:
Reinhard Pointner 2014-11-09 18:09:34 +00:00
parent 3afc7a7ff5
commit f955b8fd1c

View File

@ -270,7 +270,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
public CheckResult checkSubtitle(File videoFile, File subtitleFile) throws Exception { public CheckResult checkSubtitle(File videoFile, File subtitleFile) throws Exception {
// subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename // subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename
SubFile sub = new SubFile(); SubFile sub = new SubFile();
sub.setSubHash(md5(subtitleFile)); sub.setSubHash(md5(readFile(subtitleFile)));
sub.setSubFileName(subtitleFile.getName()); sub.setSubFileName(subtitleFile.getName());
sub.setMovieHash(computeHash(videoFile)); sub.setMovieHash(computeHash(videoFile));
sub.setMovieByteSize(videoFile.length()); sub.setMovieByteSize(videoFile.length());
@ -318,7 +318,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
// subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename // subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename
SubFile sub = new SubFile(); SubFile sub = new SubFile();
sub.setSubHash(md5(subtitleFile)); sub.setSubHash(md5(readFile(subtitleFile)));
sub.setSubFileName(subtitleFile.getName()); sub.setSubFileName(subtitleFile.getName());
sub.setMovieHash(computeHash(videoFile)); sub.setMovieHash(computeHash(videoFile));
sub.setMovieByteSize(videoFile.length()); sub.setMovieByteSize(videoFile.length());
@ -350,10 +350,10 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
/** /**
* Calculate MD5 hash. * Calculate MD5 hash.
*/ */
private String md5(File file) throws IOException { private String md5(byte[] data) throws IOException {
try { try {
MessageDigest hash = MessageDigest.getInstance("MD5"); MessageDigest hash = MessageDigest.getInstance("MD5");
hash.update(readFile(file)); hash.update(data);
return String.format("%032x", new BigInteger(1, hash.digest())); // as hex string return String.format("%032x", new BigInteger(1, hash.digest())); // as hex string
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // won't happen throw new RuntimeException(e); // won't happen
@ -482,19 +482,26 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
return URI.create(String.format("http://www.opensubtitles.org/en/search/imdbid-%d/sublanguageid-%s", movie.getImdbId(), sublanguageid)); return URI.create(String.format("http://www.opensubtitles.org/en/search/imdbid-%d/sublanguageid-%s", movie.getImdbId(), sublanguageid));
} }
public Locale detectLanguage(byte[] data) throws Exception { public synchronized Locale detectLanguage(byte[] data) throws Exception {
if (data.length < 256) { if (data.length < 256) {
throw new IllegalArgumentException("data is not enough"); throw new IllegalArgumentException("data is not enough");
} }
String language = getCache().getData("detectLanguage", md5(data), Locale.ROOT, String.class);
if (language != null) {
return language.isEmpty() ? null : new Locale(language);
}
// require login // require login
login(); login();
// detect language // detect language
List<String> languages = xmlrpc.detectLanguage(data); List<String> languages = xmlrpc.detectLanguage(data);
// return first language // return first language
return languages.size() > 0 ? new Locale(languages.get(0)) : null; language = languages.size() > 0 ? languages.get(0) : "";
getCache().putData("detectLanguage", md5(data), Locale.ROOT, language);
return new Locale(language);
} }
public synchronized void login() throws Exception { public synchronized void login() throws Exception {