* use web-persistent ehcache for remembering language filters
This commit is contained in:
parent
d29cfeced5
commit
0aeea04062
|
@ -284,9 +284,9 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
|
|||
@SuppressWarnings("unchecked")
|
||||
protected synchronized Map<String, String> 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<String, String> 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<String, String>) element.getValue();
|
||||
|
|
|
@ -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<String, String> 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<SearchResult> 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<SubtitleDescriptor> 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<SubtitleDescriptor> getSubtitleList(URL subtitleListUrl, String languageName, Document subtitleListDocument) {
|
||||
|
||||
List<Node> nodes = selectNodes("//TABLE[@class='filmSubtitleList']//A[@class='a1']", subtitleListDocument);
|
||||
|
||||
List<SubtitleDescriptor> subtitles = new ArrayList<SubtitleDescriptor>(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<String, String> 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<String, String> getLanguageFilterMap(Document subtitleListDocument) {
|
||||
|
||||
protected Map<String, String> updateLanguageFilterMap(Document subtitleListDocument) {
|
||||
Map<String, String> filters = new HashMap<String, String>(50);
|
||||
|
||||
List<Node> 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();
|
||||
|
|
|
@ -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<SearchResult> results = subscene.search("twin peaks");
|
||||
|
@ -48,7 +48,7 @@ public class SubsceneSubtitleClientTest {
|
|||
assertEquals(twinpeaksSearchResult.getName(), result.getName());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void searchResultPageRedirect() throws Exception {
|
||||
List<SearchResult> 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<SubtitleDescriptor> subtitleList = subscene.getSubtitleList(twinpeaksSearchResult, "Italian");
|
||||
|
@ -72,7 +72,7 @@ public class SubsceneSubtitleClientTest {
|
|||
assertEquals("zip", subtitle.getType());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void getSubtitleListSearchResultMany() throws Exception {
|
||||
List<SubtitleDescriptor> subtitleList = subscene.getSubtitleList(lostSearchResult, "Japanese");
|
||||
|
@ -81,10 +81,10 @@ public class SubsceneSubtitleClientTest {
|
|||
assertEquals(16, subtitleList.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void getLanguageFilterMap() throws Exception {
|
||||
Map<String, String> filters = subscene.getLanguageFilterMap(subscene.getSubtitleListDocument(new URL("http://subscene.com/none/subtitles-0.aspx"), null));
|
||||
Map<String, String> 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);
|
||||
|
|
Loading…
Reference in New Issue