From 0aeea04062d8b70d6ef5084e36a904294c40539e Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 24 Dec 2011 11:08:31 +0000 Subject: [PATCH] * use web-persistent ehcache for remembering language filters --- .../filebot/web/OpenSubtitlesClient.java | 6 +- .../filebot/web/SubsceneSubtitleClient.java | 74 +++++++++++-------- .../web/SubsceneSubtitleClientTest.java | 20 ++--- 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java index 6c3d84e8..5d6f59da 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java @@ -284,9 +284,9 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS @SuppressWarnings("unchecked") protected synchronized Map getSubLanguageMap() throws Exception { Cache cache = CacheManager.getInstance().getCache("web-persistent-datasource"); - String key = getClass().getName() + ".getSubLanguageMap"; + String cacheKey = getClass().getName() + ".subLanguageMap"; - Element element = cache.get(key); + Element element = cache.get(cacheKey); Map subLanguageMap; if (element == null) { @@ -302,7 +302,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS subLanguageMap.put("brazilian", "pob"); // cache data - cache.put(new Element(key, subLanguageMap)); + cache.put(new Element(cacheKey, subLanguageMap)); } else { // use cached entry subLanguageMap = (Map) element.getValue(); diff --git a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java index 245bb192..7761e336 100644 --- a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java +++ b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java @@ -24,35 +24,35 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; import net.sourceforge.filebot.ResourceManager; -import net.sourceforge.filebot.Settings; public class SubsceneSubtitleClient implements SubtitleProvider { private static final String host = "subscene.com"; - private final Map languageFilterMap = initLanguageFilterMap(); - @Override public String getName() { return "Subscene"; } - + @Override public URI getLink() { return URI.create("http://subscene.com"); } - + @Override public Icon getIcon() { return ResourceManager.getIcon("search.subscene"); } - + @Override public List search(String query) throws IOException, SAXException { URL searchUrl = new URL("http", host, "/filmsearch.aspx?q=" + encode(query)); @@ -97,35 +97,25 @@ public class SubsceneSubtitleClient implements SubtitleProvider { return searchResults; } - + @Override public List getSubtitleList(SearchResult searchResult, String languageName) throws Exception { URL subtitleListUrl = getSubtitleListLink(searchResult, languageName).toURL(); - String languageFilter = null; - - if (languageName != null) { - synchronized (languageFilterMap) { - languageFilter = languageFilterMap.get(languageName.toLowerCase()); - } - } + String languageFilter = getLanguageFilter(languageName); Document subtitleListDocument = getSubtitleListDocument(subtitleListUrl, languageFilter); // let's update language filters if they are not known yet if (languageName != null && languageFilter == null) { - synchronized (languageFilterMap) { - languageFilterMap.putAll(getLanguageFilterMap(subtitleListDocument)); - } + updateLanguageFilterMap(subtitleListDocument); } return getSubtitleList(subtitleListUrl, languageName, subtitleListDocument); } - + private List getSubtitleList(URL subtitleListUrl, String languageName, Document subtitleListDocument) { - List nodes = selectNodes("//TABLE[@class='filmSubtitleList']//A[@class='a1']", subtitleListDocument); - List subtitles = new ArrayList(nodes.size()); for (Node node : nodes) { @@ -147,7 +137,7 @@ public class SubsceneSubtitleClient implements SubtitleProvider { return subtitles; } - + protected Document getSubtitleListDocument(URL subtitleListUrl, String languageFilter) throws IOException, SAXException { URLConnection connection = subtitleListUrl.openConnection(); @@ -158,15 +148,26 @@ public class SubsceneSubtitleClient implements SubtitleProvider { return getHtmlDocument(connection); } - - protected Map initLanguageFilterMap() { - return Settings.forPackage(SublightSubtitleClient.class).node("subtitles/subscene/languageFilterMap").asMap(); + + protected String getLanguageFilter(String languageName) { + if (languageName == null || languageName.isEmpty()) { + return null; + } + + Cache cache = CacheManager.getInstance().getCache("web-persistent-datasource"); + String cacheKey = getClass().getName() + ".languageFilter"; + + Element element = cache.get(cacheKey); + if (element == null) { + return null; + } + + return (String) ((Map) element.getValue()).get(languageName.toLowerCase()); } - - protected Map getLanguageFilterMap(Document subtitleListDocument) { + + protected Map updateLanguageFilterMap(Document subtitleListDocument) { Map filters = new HashMap(50); - List nodes = selectNodes("//DIV[@class='languageList']/DIV", subtitleListDocument); for (Node node : nodes) { @@ -181,33 +182,42 @@ public class SubsceneSubtitleClient implements SubtitleProvider { } } + // update cache after sanity check + if (filters.size() > 42) { + Cache cache = CacheManager.getInstance().getCache("web-persistent-datasource"); + String cacheKey = getClass().getName() + ".languageFilter"; + cache.put(new Element(cacheKey, filters)); + } else { + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to scrape language filters: " + filters); + } + return filters; } - + @Override public URI getSubtitleListLink(SearchResult searchResult, String languageName) { return ((HyperLink) searchResult).getURI(); } - + public static class SubsceneSearchResult extends HyperLink { private String shortName; - + public SubsceneSearchResult(String shortName, String title, URL url) { super(title, url); this.shortName = shortName; } - + @Override public String getName() { return shortName; } - + @Override public String toString() { return super.getName(); diff --git a/test/net/sourceforge/filebot/web/SubsceneSubtitleClientTest.java b/test/net/sourceforge/filebot/web/SubsceneSubtitleClientTest.java index 18f11a38..dc0bff91 100644 --- a/test/net/sourceforge/filebot/web/SubsceneSubtitleClientTest.java +++ b/test/net/sourceforge/filebot/web/SubsceneSubtitleClientTest.java @@ -27,17 +27,17 @@ public class SubsceneSubtitleClientTest { */ private static HyperLink lostSearchResult; - + @BeforeClass public static void setUpBeforeClass() throws Exception { twinpeaksSearchResult = new SubsceneSearchResult("Twin Peaks", "Twin Peaks - First Season (1990)", new URL("http://subscene.com/twin-peaks--first-season/subtitles-32482.aspx")); lostSearchResult = new SubsceneSearchResult("Lost", "Lost - Fourth Season (2008)", new URL("http://subscene.com/Lost-Fourth-Season/subtitles-70963.aspx")); } - + private SubsceneSubtitleClient subscene = new SubsceneSubtitleClient(); - + @Test public void search() throws Exception { List results = subscene.search("twin peaks"); @@ -48,7 +48,7 @@ public class SubsceneSubtitleClientTest { assertEquals(twinpeaksSearchResult.getName(), result.getName()); } - + @Test public void searchResultPageRedirect() throws Exception { List results = subscene.search("firefly"); @@ -60,7 +60,7 @@ public class SubsceneSubtitleClientTest { assertEquals("http://subscene.com/Firefly-The-Complete-Series/subtitles-20008.aspx", result.getURL().toString()); } - + @Test public void getSubtitleListSearchResult() throws Exception { List subtitleList = subscene.getSubtitleList(twinpeaksSearchResult, "Italian"); @@ -72,7 +72,7 @@ public class SubsceneSubtitleClientTest { assertEquals("zip", subtitle.getType()); } - + @Test public void getSubtitleListSearchResultMany() throws Exception { List subtitleList = subscene.getSubtitleList(lostSearchResult, "Japanese"); @@ -81,10 +81,10 @@ public class SubsceneSubtitleClientTest { assertEquals(16, subtitleList.size()); } - + @Test public void getLanguageFilterMap() throws Exception { - Map filters = subscene.getLanguageFilterMap(subscene.getSubtitleListDocument(new URL("http://subscene.com/none/subtitles-0.aspx"), null)); + Map filters = subscene.updateLanguageFilterMap(subscene.getSubtitleListDocument(new URL("http://subscene.com/none/subtitles-0.aspx"), null)); assertEquals("1", filters.get("albanian")); assertEquals("13", filters.get("english")); @@ -92,13 +92,13 @@ public class SubsceneSubtitleClientTest { assertEquals("45", filters.get("vietnamese")); } - + @Test public void getSubtitleListLink() throws Exception { assertEquals(twinpeaksSearchResult.getURL().toString(), subscene.getSubtitleListLink(twinpeaksSearchResult, null).toURL().toString()); } - + @Test public void downloadSubtitleArchive() throws Exception { SearchResult selectedResult = subscene.search("firefly").get(0);