1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 16:58:51 -05:00

* improved caching and other refactoring

* disabled TheMovieDB hash lookup since it doesn't work anyway
This commit is contained in:
Reinhard Pointner 2012-01-02 03:07:09 +00:00
parent 208216c0e3
commit b8c96b8fbe
8 changed files with 52 additions and 72 deletions

View File

@ -170,18 +170,18 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
} }
public void putData(Object category, Object key, Object object) { public void putData(Object category, Object key, Locale locale, Object object) {
try { try {
cache.put(new Element(new Key(id, category, key), object)); cache.put(new Element(new Key(id, category, locale, key), object));
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e); Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e);
} }
} }
public <T> T getData(Object category, Object key, Class<T> type) { public <T> T getData(Object category, Object key, Locale locale, Class<T> type) {
try { try {
Element element = cache.get(new Key(id, category, key)); Element element = cache.get(new Key(id, category, locale, key));
if (element != null) { if (element != null) {
return type.cast(element.getValue()); return type.cast(element.getValue());
} }

View File

@ -35,7 +35,7 @@ public abstract class CachedResource<T extends Serializable> {
/** /**
* Convert resource data into usable data * Convert resource data into usable data
*/ */
public abstract T process(ByteBuffer data); public abstract T process(ByteBuffer data) throws Exception;
public synchronized T get() throws IOException { public synchronized T get() throws IOException {
@ -57,7 +57,11 @@ public abstract class CachedResource<T extends Serializable> {
ByteBuffer data = fetchIfModified(new URL(resource), element != null ? lastUpdateTime : 0); ByteBuffer data = fetchIfModified(new URL(resource), element != null ? lastUpdateTime : 0);
if (data != null) { if (data != null) {
element = new Element(cacheKey, process(data)); try {
element = new Element(cacheKey, process(data));
} catch (Exception e) {
throw new IOException(e);
}
} }
// update cached data and last-updated time // update cached data and last-updated time

View File

@ -5,13 +5,10 @@ package net.sourceforge.filebot.web;
import static java.lang.Math.*; import static java.lang.Math.*;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static net.sourceforge.filebot.web.OpenSubtitlesHasher.*; import static net.sourceforge.filebot.web.OpenSubtitlesHasher.*;
import static net.sourceforge.filebot.web.WebRequest.*;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
@ -21,12 +18,9 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Scanner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.Icon; import javax.swing.Icon;
@ -344,40 +338,4 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
return null; return null;
} }
public List<Movie> exportMovie() throws IOException {
Cache cache = CacheManager.getInstance().getCache("web-persistent-datasource");
String cacheKey = getClass().getName() + ".exportMovie";
Element element = cache.get(cacheKey);
if (element != null) {
return asList((Movie[]) element.getValue());
}
URL url = new URL("http://www.opensubtitles.org/addons/export_movie.php");
Scanner in = new Scanner(getReader(url.openConnection()));
try {
// e.g. IDMovie IDMovieImdb MovieName MovieYear
Pattern linePattern = Pattern.compile("(\\d+)\\t(\\d+)\\t([^\\t]+)\\t(\\d{4})");
List<Movie> result = new ArrayList<Movie>();
while (in.hasNextLine()) {
String line = in.nextLine().trim();
Matcher matcher = linePattern.matcher(line);
if (matcher.matches()) {
int idMovieImdb = Integer.parseInt(matcher.group(2));
String movieName = matcher.group(3);
int movieYear = Integer.parseInt(matcher.group(4));
result.add(new Movie(movieName, movieYear, idMovieImdb));
}
}
// cache data
cache.put(new Element(cacheKey, result.toArray(new Movie[0])));
return result;
} finally {
in.close();
}
}
} }

View File

@ -146,7 +146,7 @@ public class OpenSubtitlesXmlRpc {
movies.add(new Movie(name, year, Integer.parseInt(imdbid))); movies.add(new Movie(name, year, Integer.parseInt(imdbid)));
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger(OpenSubtitlesXmlRpc.class.getName()).log(Level.WARNING, String.format("Ignore movie %s: %s", movie, e.getMessage())); Logger.getLogger(OpenSubtitlesXmlRpc.class.getName()).log(Level.INFO, String.format("Ignore movie %s: %s", movie, e.getMessage()));
} }
} }

View File

@ -69,10 +69,8 @@ public class TMDbClient implements MovieIdentificationService {
public List<Movie> searchMovie(File file, Locale locale) throws IOException, SAXException { public List<Movie> searchMovie(File file, Locale locale) throws IOException, SAXException {
if (file.length() < OpenSubtitlesHasher.HASH_CHUNK_SIZE) return emptyList(); // API BROKEN
return emptyList(); // return searchMovie(OpenSubtitlesHasher.computeHash(file), file.length(), locale);
return searchMovie(OpenSubtitlesHasher.computeHash(file), file.length(), locale);
} }

View File

@ -192,12 +192,20 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
public TheTVDBSearchResult lookupByID(int id, Locale language) throws Exception { public TheTVDBSearchResult lookupByID(int id, Locale language) throws Exception {
TheTVDBSearchResult cachedItem = getCache().getData("lookupByID", id, language, TheTVDBSearchResult.class);
if (cachedItem != null) {
return cachedItem;
}
try { try {
URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml"); URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml");
Document baseRecord = getDocument(baseRecordLocation); Document baseRecord = getDocument(baseRecordLocation);
String name = selectString("//SeriesName", baseRecord); String name = selectString("//SeriesName", baseRecord);
return new TheTVDBSearchResult(name, id);
TheTVDBSearchResult series = new TheTVDBSearchResult(name, id);
getCache().putData("lookupByID", id, language, series);
return series;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// illegal series id // illegal series id
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to retrieve base series record: " + e.getMessage()); Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to retrieve base series record: " + e.getMessage());
@ -207,6 +215,11 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
public TheTVDBSearchResult lookupByIMDbID(int imdbid, Locale language) throws Exception { public TheTVDBSearchResult lookupByIMDbID(int imdbid, Locale language) throws Exception {
TheTVDBSearchResult cachedItem = getCache().getData("lookupByIMDbID", imdbid, language, TheTVDBSearchResult.class);
if (cachedItem != null) {
return cachedItem;
}
URL query = getResource(null, "/api/GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + language.getLanguage()); URL query = getResource(null, "/api/GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + language.getLanguage());
Document dom = getDocument(query); Document dom = getDocument(query);
@ -216,7 +229,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
if (id == null || id.isEmpty() || name == null || name.isEmpty()) if (id == null || id.isEmpty() || name == null || name.isEmpty())
return null; return null;
return new TheTVDBSearchResult(name, Integer.parseInt(id)); TheTVDBSearchResult series = new TheTVDBSearchResult(name, Integer.parseInt(id));
getCache().putData("lookupByIMDbID", imdbid, language, series);
return series;
} }
@ -253,7 +268,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
// try cache first // try cache first
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<MirrorType, String> cachedMirrors = (Map<MirrorType, String>) getCache().getData("mirrors", null, Map.class); Map<MirrorType, String> cachedMirrors = (Map<MirrorType, String>) getCache().getData("mirrors", null, null, Map.class);
if (cachedMirrors != null) { if (cachedMirrors != null) {
mirrors.putAll(cachedMirrors); mirrors.putAll(cachedMirrors);
return mirrors.get(mirrorType); return mirrors.get(mirrorType);
@ -296,7 +311,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
} }
getCache().putData("mirrors", null, mirrors); getCache().putData("mirrors", null, null, mirrors);
} }
return mirrors.get(mirrorType); return mirrors.get(mirrorType);
@ -385,7 +400,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
public SeriesInfo getSeriesInfo(TheTVDBSearchResult searchResult, Locale locale) throws Exception { public SeriesInfo getSeriesInfo(TheTVDBSearchResult searchResult, Locale locale) throws Exception {
// check cache first // check cache first
SeriesInfo cachedItem = getCache().getData("seriesInfo", searchResult.seriesId, SeriesInfo.class); SeriesInfo cachedItem = getCache().getData("seriesInfo", searchResult.seriesId, locale, SeriesInfo.class);
if (cachedItem != null) { if (cachedItem != null) {
return cachedItem; return cachedItem;
} }
@ -407,7 +422,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
SeriesInfo seriesInfo = new SeriesInfo(fields); SeriesInfo seriesInfo = new SeriesInfo(fields);
getCache().putData("seriesInfo", searchResult.seriesId, seriesInfo); getCache().putData("seriesInfo", searchResult.seriesId, locale, seriesInfo);
return seriesInfo; return seriesInfo;
} }
@ -660,7 +675,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
public List<BannerDescriptor> getBannerList(TheTVDBSearchResult series) throws Exception { public List<BannerDescriptor> getBannerList(TheTVDBSearchResult series) throws Exception {
// check cache first // check cache first
BannerDescriptor[] cachedList = getCache().getData("banners", series.seriesId, BannerDescriptor[].class); BannerDescriptor[] cachedList = getCache().getData("banners", series.seriesId, null, BannerDescriptor[].class);
if (cachedList != null) { if (cachedList != null) {
return asList(cachedList); return asList(cachedList);
} }
@ -692,7 +707,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
} }
getCache().putData("banners", series.seriesId, banners.toArray(new BannerDescriptor[0])); getCache().putData("banners", series.seriesId, null, banners.toArray(new BannerDescriptor[0]));
return banners; return banners;
} }

View File

@ -201,15 +201,4 @@ public class OpenSubtitlesXmlRpcTest {
xmlrpc.logout(); xmlrpc.logout();
} }
@Test
public void exportMovie() throws Exception {
List<Movie> list = new OpenSubtitlesClient(null).exportMovie();
Movie sample = (Movie) list.get(17);
// check sample entry
assertEquals("Back to the Future", sample.getName());
assertEquals(1985, sample.getYear());
assertEquals(88763, sample.getImdbId());
}
} }

View File

@ -149,6 +149,22 @@ public class TheTVDBClientTest {
} }
@Test
public void lookupByID() throws Exception {
TheTVDBSearchResult series = thetvdb.lookupByID(78874, Locale.ENGLISH);
assertEquals("Firefly", series.getName());
assertEquals(70726, series.getSeriesId());
}
@Test
public void lookupByIMDbID() throws Exception {
TheTVDBSearchResult series = thetvdb.lookupByIMDbID(78874, Locale.ENGLISH);
assertEquals("Firefly", series.getName());
assertEquals(70726, series.getSeriesId());
}
@Test @Test
public void getSeriesInfo() throws Exception { public void getSeriesInfo() throws Exception {
SeriesInfo it = thetvdb.getSeriesInfo(new TheTVDBSearchResult(null, 80348), Locale.ENGLISH); SeriesInfo it = thetvdb.getSeriesInfo(new TheTVDBSearchResult(null, 80348), Locale.ENGLISH);