* 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,16 +24,16 @@ 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() {
|
||||
|
@ -101,21 +101,13 @@ public class SubsceneSubtitleClient implements SubtitleProvider {
|
|||
@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);
|
||||
|
@ -123,9 +115,7 @@ public class SubsceneSubtitleClient implements SubtitleProvider {
|
|||
|
||||
|
||||
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) {
|
||||
|
@ -159,14 +149,25 @@ public class SubsceneSubtitleClient implements SubtitleProvider {
|
|||
}
|
||||
|
||||
|
||||
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,6 +182,15 @@ 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ public class SubsceneSubtitleClientTest {
|
|||
|
||||
@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"));
|
||||
|
|
Loading…
Reference in New Issue