From f955b8fd1c38b39a7694c225d0f3f98f6ec72b00 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 9 Nov 2014 18:09:34 +0000 Subject: [PATCH] * make sure OpenSubtitles language detection is cached --- .../net/filebot/web/OpenSubtitlesClient.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/source/net/filebot/web/OpenSubtitlesClient.java b/source/net/filebot/web/OpenSubtitlesClient.java index 76bda316..3cdb5b1a 100644 --- a/source/net/filebot/web/OpenSubtitlesClient.java +++ b/source/net/filebot/web/OpenSubtitlesClient.java @@ -270,7 +270,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS public CheckResult checkSubtitle(File videoFile, File subtitleFile) throws Exception { // subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename SubFile sub = new SubFile(); - sub.setSubHash(md5(subtitleFile)); + sub.setSubHash(md5(readFile(subtitleFile))); sub.setSubFileName(subtitleFile.getName()); sub.setMovieHash(computeHash(videoFile)); sub.setMovieByteSize(videoFile.length()); @@ -318,7 +318,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS // subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename SubFile sub = new SubFile(); - sub.setSubHash(md5(subtitleFile)); + sub.setSubHash(md5(readFile(subtitleFile))); sub.setSubFileName(subtitleFile.getName()); sub.setMovieHash(computeHash(videoFile)); sub.setMovieByteSize(videoFile.length()); @@ -350,10 +350,10 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS /** * Calculate MD5 hash. */ - private String md5(File file) throws IOException { + private String md5(byte[] data) throws IOException { try { MessageDigest hash = MessageDigest.getInstance("MD5"); - hash.update(readFile(file)); + hash.update(data); return String.format("%032x", new BigInteger(1, hash.digest())); // as hex string } catch (NoSuchAlgorithmException e) { 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)); } - public Locale detectLanguage(byte[] data) throws Exception { + public synchronized Locale detectLanguage(byte[] data) throws Exception { if (data.length < 256) { 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 login(); // detect language List languages = xmlrpc.detectLanguage(data); - + // 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 {