From 7d814d7b77c03e342af946a1e000dd52d5654c44 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 4 Jan 2014 20:18:47 +0000 Subject: [PATCH] * make it easier to reference the original database in the format expression --- .../net/sourceforge/filebot/WebServices.java | 18 ++++ .../filebot/format/MediaBindingBean.java | 20 +++-- .../web/AbstractEpisodeListProvider.java | 87 ++++++++----------- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/source/net/sourceforge/filebot/WebServices.java b/source/net/sourceforge/filebot/WebServices.java index 2ed22922..45ef4147 100644 --- a/source/net/sourceforge/filebot/WebServices.java +++ b/source/net/sourceforge/filebot/WebServices.java @@ -21,11 +21,13 @@ import java.util.logging.Logger; import net.sourceforge.filebot.web.AcoustID; import net.sourceforge.filebot.web.AnidbClient; import net.sourceforge.filebot.web.AnidbSearchResult; +import net.sourceforge.filebot.web.AudioTrack; import net.sourceforge.filebot.web.EpisodeListProvider; import net.sourceforge.filebot.web.FanartTV; import net.sourceforge.filebot.web.ID3Lookup; import net.sourceforge.filebot.web.IMDbClient; import net.sourceforge.filebot.web.LocalSearch; +import net.sourceforge.filebot.web.Movie; import net.sourceforge.filebot.web.MovieIdentificationService; import net.sourceforge.filebot.web.MusicIdentificationService; import net.sourceforge.filebot.web.OpenSubtitlesClient; @@ -34,6 +36,7 @@ import net.sourceforge.filebot.web.SerienjunkiesClient; import net.sourceforge.filebot.web.SubtitleProvider; import net.sourceforge.filebot.web.TMDbClient; import net.sourceforge.filebot.web.TVRageClient; +import net.sourceforge.filebot.web.TVRageSearchResult; import net.sourceforge.filebot.web.TheTVDBClient; import net.sourceforge.filebot.web.TheTVDBSearchResult; import net.sourceforge.filebot.web.VideoHashSubtitleService; @@ -109,6 +112,21 @@ public final class WebServices { return null; // default } + public static Object getServiceBySearchResult(Object r) { + if (r instanceof TheTVDBSearchResult) + return WebServices.TheTVDB; + if (r instanceof AnidbSearchResult) + return WebServices.AniDB; + if (r instanceof TVRageSearchResult) + return WebServices.TVRage; + if (r instanceof Movie) + return WebServices.TMDb; + if (r instanceof AudioTrack) + return WebServices.AcoustID; + + return null; + } + public static class TheTVDBClientWithLocalSearch extends TheTVDBClient { public TheTVDBClientWithLocalSearch(String apikey) { diff --git a/source/net/sourceforge/filebot/format/MediaBindingBean.java b/source/net/sourceforge/filebot/format/MediaBindingBean.java index 24e333c0..b5322333 100644 --- a/source/net/sourceforge/filebot/format/MediaBindingBean.java +++ b/source/net/sourceforge/filebot/format/MediaBindingBean.java @@ -41,10 +41,12 @@ import net.sourceforge.filebot.web.AnidbSearchResult; import net.sourceforge.filebot.web.AudioTrack; import net.sourceforge.filebot.web.Date; import net.sourceforge.filebot.web.Episode; +import net.sourceforge.filebot.web.EpisodeListProvider; import net.sourceforge.filebot.web.Movie; import net.sourceforge.filebot.web.MoviePart; import net.sourceforge.filebot.web.MultiEpisode; import net.sourceforge.filebot.web.SearchResult; +import net.sourceforge.filebot.web.SortOrder; import net.sourceforge.filebot.web.TheTVDBSearchResult; import net.sourceforge.tuned.FileUtilities; import net.sourceforge.tuned.FileUtilities.ExtensionFileFilter; @@ -521,13 +523,21 @@ public class MediaBindingBean { @Define("episodelist") public Object getEpisodeList() throws Exception { - if (getSeriesObject() instanceof TheTVDBSearchResult) { - return WebServices.TheTVDB.getEpisodeList(getSeriesObject()); + return ((EpisodeListProvider) getDatabase()).getEpisodeList(getSeriesObject(), SortOrder.Airdate, Locale.ENGLISH); + } + + @Define("database") + public Object getDatabase() { + if (infoObject instanceof Episode) { + return WebServices.getServiceBySearchResult(getSeriesObject()); } - if (getSeriesObject() instanceof AnidbSearchResult) { - return WebServices.AniDB.getEpisodeList(getSeriesObject()); + if (infoObject instanceof Movie) { + return WebServices.getServiceBySearchResult(getMovie()); } - return null; // default to original search result + if (infoObject instanceof AudioTrack) { + return WebServices.getServiceBySearchResult(getMusic()); + } + return null; } @Define("duration") diff --git a/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java b/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java index f49c4275..b8cd8bcc 100644 --- a/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java +++ b/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java @@ -1,7 +1,5 @@ - package net.sourceforge.filebot.web; - import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -11,32 +9,26 @@ import java.util.logging.Logger; import net.sourceforge.filebot.Cache; import net.sourceforge.filebot.Cache.Key; - public abstract class AbstractEpisodeListProvider implements EpisodeListProvider { - + @Override public boolean hasSingleSeasonSupport() { return true; } - - + @Override public boolean hasLocaleSupport() { return false; } - - + protected abstract List fetchSearchResult(String query, Locale locale) throws Exception; - - + protected abstract List fetchEpisodeList(SearchResult searchResult, SortOrder sortOrder, Locale locale) throws Exception; - - + public List search(String query) throws Exception { return search(query, getDefaultLocale()); } - - + @Override public List search(String query, Locale locale) throws Exception { ResultCache cache = getCache(); @@ -44,20 +36,22 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider if (results != null) { return results; } - + // perform actual search results = fetchSearchResult(query, locale); - + // cache results and return return (cache != null) ? cache.putSearchResult(query, locale, results) : results; } - - + + public List getEpisodeList(SearchResult searchResult, String sortOrder, String locale) throws Exception { + return getEpisodeList(searchResult, sortOrder == null ? SortOrder.Airdate : SortOrder.forName(sortOrder), new Locale(locale)); + } + public List getEpisodeList(SearchResult searchResult) throws Exception { return getEpisodeList(searchResult, SortOrder.Airdate, getDefaultLocale()); } - - + @Override public List getEpisodeList(SearchResult searchResult, SortOrder sortOrder, Locale locale) throws Exception { ResultCache cache = getCache(); @@ -65,53 +59,46 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider if (episodes != null) { return episodes; } - + // perform actual search episodes = fetchEpisodeList(searchResult, sortOrder, locale); - + // cache results and return return (cache != null) ? cache.putEpisodeList(searchResult, sortOrder, locale, episodes) : episodes; } - - + public Locale getDefaultLocale() { return Locale.ENGLISH; } - - + public ResultCache getCache() { return null; } - - + protected static class ResultCache { - + private final String id; private final Cache cache; - - + public ResultCache(String id, Cache cache) { this.id = id; this.cache = cache; } - - + protected String normalize(String query) { return query == null ? null : query.trim().toLowerCase(); } - - + public List putSearchResult(String query, Locale locale, List value) { try { cache.put(new Key(id, normalize(query), locale), value.toArray(new SearchResult[0])); } catch (Exception e) { Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage()); } - + return value; } - - + public List getSearchResult(String query, Locale locale) { try { SearchResult[] results = cache.get(new Key(id, normalize(query), locale), SearchResult[].class); @@ -121,22 +108,20 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider } catch (Exception e) { Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e); } - + return null; } - - + public List putEpisodeList(SearchResult key, SortOrder sortOrder, Locale locale, List episodes) { try { cache.put(new Key(id, key, sortOrder, locale), episodes.toArray(new Episode[0])); } catch (Exception e) { Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage()); } - + return episodes; } - - + public List getEpisodeList(SearchResult key, SortOrder sortOrder, Locale locale) { try { Episode[] episodes = cache.get(new Key(id, key, sortOrder, locale), Episode[].class); @@ -146,11 +131,10 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider } catch (Exception e) { Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e); } - + return null; } - - + public void putData(Object category, Object key, Locale locale, Object object) { try { cache.put(new Key(id, category, locale, key), object); @@ -158,8 +142,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage()); } } - - + public T getData(Object category, Object key, Locale locale, Class type) { try { T value = cache.get(new Key(id, category, locale, key), type); @@ -169,10 +152,10 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider } catch (Exception e) { Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e); } - + return null; } - + } - + }