From b8c96b8fbe0dea6b3d3678b76c19bdd655f227ec Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 2 Jan 2012 03:07:09 +0000 Subject: [PATCH] * improved caching and other refactoring * disabled TheMovieDB hash lookup since it doesn't work anyway --- .../web/AbstractEpisodeListProvider.java | 8 ++-- .../filebot/web/CachedResource.java | 8 +++- .../filebot/web/OpenSubtitlesClient.java | 42 ------------------- .../filebot/web/OpenSubtitlesXmlRpc.java | 2 +- .../sourceforge/filebot/web/TMDbClient.java | 6 +-- .../filebot/web/TheTVDBClient.java | 31 ++++++++++---- .../filebot/web/OpenSubtitlesXmlRpcTest.java | 11 ----- .../filebot/web/TheTVDBClientTest.java | 16 +++++++ 8 files changed, 52 insertions(+), 72 deletions(-) diff --git a/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java b/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java index df0bd4c3..093d67b6 100644 --- a/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java +++ b/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java @@ -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 { - cache.put(new Element(new Key(id, category, key), object)); + cache.put(new Element(new Key(id, category, locale, key), object)); } catch (Exception e) { Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e); } } - public T getData(Object category, Object key, Class type) { + public T getData(Object category, Object key, Locale locale, Class type) { try { - Element element = cache.get(new Key(id, category, key)); + Element element = cache.get(new Key(id, category, locale, key)); if (element != null) { return type.cast(element.getValue()); } diff --git a/source/net/sourceforge/filebot/web/CachedResource.java b/source/net/sourceforge/filebot/web/CachedResource.java index 1ddbdd22..909322c2 100644 --- a/source/net/sourceforge/filebot/web/CachedResource.java +++ b/source/net/sourceforge/filebot/web/CachedResource.java @@ -35,7 +35,7 @@ public abstract class CachedResource { /** * 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 { @@ -57,7 +57,11 @@ public abstract class CachedResource { ByteBuffer data = fetchIfModified(new URL(resource), element != null ? lastUpdateTime : 0); 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 diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java index 04fd89e8..cc4d3540 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java @@ -5,13 +5,10 @@ package net.sourceforge.filebot.web; import static java.lang.Math.*; import static java.util.Arrays.*; import static net.sourceforge.filebot.web.OpenSubtitlesHasher.*; -import static net.sourceforge.filebot.web.WebRequest.*; import java.io.File; -import java.io.IOException; import java.math.BigInteger; import java.net.URI; -import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -21,12 +18,9 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; -import java.util.Scanner; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.swing.Icon; @@ -344,40 +338,4 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS return null; } - - public List 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 result = new ArrayList(); - 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(); - } - } } diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java b/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java index 1f2c72ba..da49fd55 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java @@ -146,7 +146,7 @@ public class OpenSubtitlesXmlRpc { movies.add(new Movie(name, year, Integer.parseInt(imdbid))); } 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())); } } diff --git a/source/net/sourceforge/filebot/web/TMDbClient.java b/source/net/sourceforge/filebot/web/TMDbClient.java index e65e1551..5aad594f 100644 --- a/source/net/sourceforge/filebot/web/TMDbClient.java +++ b/source/net/sourceforge/filebot/web/TMDbClient.java @@ -69,10 +69,8 @@ public class TMDbClient implements MovieIdentificationService { public List searchMovie(File file, Locale locale) throws IOException, SAXException { - if (file.length() < OpenSubtitlesHasher.HASH_CHUNK_SIZE) - return emptyList(); - - return searchMovie(OpenSubtitlesHasher.computeHash(file), file.length(), locale); + return emptyList(); // API BROKEN + // return searchMovie(OpenSubtitlesHasher.computeHash(file), file.length(), locale); } diff --git a/source/net/sourceforge/filebot/web/TheTVDBClient.java b/source/net/sourceforge/filebot/web/TheTVDBClient.java index 634fd406..95cdc0fa 100644 --- a/source/net/sourceforge/filebot/web/TheTVDBClient.java +++ b/source/net/sourceforge/filebot/web/TheTVDBClient.java @@ -192,12 +192,20 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { public TheTVDBSearchResult lookupByID(int id, Locale language) throws Exception { + TheTVDBSearchResult cachedItem = getCache().getData("lookupByID", id, language, TheTVDBSearchResult.class); + if (cachedItem != null) { + return cachedItem; + } + try { URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml"); Document baseRecord = getDocument(baseRecordLocation); 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) { // illegal series id 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 { + 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()); Document dom = getDocument(query); @@ -216,7 +229,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { if (id == null || id.isEmpty() || name == null || name.isEmpty()) 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 { @SuppressWarnings("unchecked") - Map cachedMirrors = (Map) getCache().getData("mirrors", null, Map.class); + Map cachedMirrors = (Map) getCache().getData("mirrors", null, null, Map.class); if (cachedMirrors != null) { mirrors.putAll(cachedMirrors); 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); @@ -385,7 +400,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { public SeriesInfo getSeriesInfo(TheTVDBSearchResult searchResult, Locale locale) throws Exception { // 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) { return cachedItem; } @@ -407,7 +422,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { } SeriesInfo seriesInfo = new SeriesInfo(fields); - getCache().putData("seriesInfo", searchResult.seriesId, seriesInfo); + getCache().putData("seriesInfo", searchResult.seriesId, locale, seriesInfo); return seriesInfo; } @@ -660,7 +675,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { public List getBannerList(TheTVDBSearchResult series) throws Exception { // 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) { 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; } diff --git a/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java b/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java index 85254c23..be166df5 100644 --- a/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java +++ b/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java @@ -201,15 +201,4 @@ public class OpenSubtitlesXmlRpcTest { xmlrpc.logout(); } - - @Test - public void exportMovie() throws Exception { - List 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()); - } } diff --git a/test/net/sourceforge/filebot/web/TheTVDBClientTest.java b/test/net/sourceforge/filebot/web/TheTVDBClientTest.java index 65bf173a..c3b3e7c8 100644 --- a/test/net/sourceforge/filebot/web/TheTVDBClientTest.java +++ b/test/net/sourceforge/filebot/web/TheTVDBClientTest.java @@ -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 public void getSeriesInfo() throws Exception { SeriesInfo it = thetvdb.getSeriesInfo(new TheTVDBSearchResult(null, 80348), Locale.ENGLISH);