From f902b04deeb092c4e71c0756176abce9597a915d Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 6 Jan 2014 20:41:59 +0000 Subject: [PATCH] * make OpenSubtitlesClient fault tolerant via automatic retries * try for exact matches in internal movie index first before running a query --- .../filebot/cli/ScriptShell.lib.groovy | 4 +- .../filebot/web/OpenSubtitlesClient.java | 37 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index b2e2f5a0..4c5985ea 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -234,8 +234,8 @@ def detectSeriesName(files, locale = Locale.ENGLISH) { return names == null || names.isEmpty() ? null : names.toList()[0] } -def detectMovie(File movieFile, strict = true, queryLookupService = TheMovieDB, hashLookupService = OpenSubtitles, locale = Locale.ENGLISH) { - def movies = MediaDetection.detectMovie(movieFile, hashLookupService, queryLookupService, locale, strict) +def detectMovie(File file, strict = true, queryLookupService = TheMovieDB, hashLookupService = OpenSubtitles, locale = Locale.ENGLISH) { + def movies = MediaDetection.matchMovieName([file.name, file.parentFile.name], true, 0) ?: MediaDetection.detectMovie(file, hashLookupService, queryLookupService, locale, strict) return movies == null || movies.isEmpty() ? null : movies.toList()[0] } diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java index 98a771b0..617b8fdf 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java @@ -38,8 +38,10 @@ import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.BaseInfo; import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.Query; import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.SubFile; import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.TryUploadResponse; +import net.sourceforge.tuned.ExceptionUtilities; import net.sourceforge.tuned.Timer; import redstone.xmlrpc.XmlRpcException; +import redstone.xmlrpc.XmlRpcFault; /** * SubtitleClient for OpenSubtitles. @@ -52,7 +54,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS private String password = ""; public OpenSubtitlesClient(String useragent) { - this.xmlrpc = new OpenSubtitlesXmlRpc(useragent); + this.xmlrpc = new OpenSubtitlesXmlRpcWithRetry(useragent, 2, 3000); } public synchronized void setUser(String username, String password) { @@ -576,4 +578,37 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS } + protected static class OpenSubtitlesXmlRpcWithRetry extends OpenSubtitlesXmlRpc { + + private int retryCountLimit; + private long retryWaitTime; + + public OpenSubtitlesXmlRpcWithRetry(String useragent, int retryCountLimit, long retryWaitTime) { + super(useragent); + this.retryCountLimit = retryCountLimit; + this.retryWaitTime = retryWaitTime; + } + + @Override + protected Map invoke(String method, Object... arguments) throws XmlRpcFault { + for (int i = 0; retryCountLimit < 0 || i <= retryCountLimit; i++) { + try { + if (i > 0) { + Thread.sleep(retryWaitTime); + } + return super.invoke(method, arguments); + } catch (XmlRpcFault | XmlRpcException e) { + IOException ioException = ExceptionUtilities.findCause(e, IOException.class); + if (ioException == null || i >= 0 && i >= retryCountLimit) { + throw e; + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + return null; // can't happen + } + + } + }