From a226a4de0f9aeff859847f327704f085fcc19e64 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 17 Jan 2016 07:01:53 +0000 Subject: [PATCH] + added {localize} binding to allow just-in-time localization of movie/episode data Episode Format Example: {localize.German.Title} Movie Format Example: {localize.Chinese.Name} --- .../net/filebot/format/DynamicBindings.java | 20 ++++++++++++ .../net/filebot/format/MediaBindingBean.java | 31 ++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 source/net/filebot/format/DynamicBindings.java diff --git a/source/net/filebot/format/DynamicBindings.java b/source/net/filebot/format/DynamicBindings.java new file mode 100644 index 00000000..00adaf5b --- /dev/null +++ b/source/net/filebot/format/DynamicBindings.java @@ -0,0 +1,20 @@ +package net.filebot.format; + +import groovy.lang.GroovyObjectSupport; + +import java.util.function.Function; + +public class DynamicBindings extends GroovyObjectSupport { + + private Function map; + + public DynamicBindings(Function map) { + this.map = map; + } + + @Override + public Object getProperty(String property) { + return map.apply(property); + } + +} diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 7e857541..b9f31240 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -612,12 +612,33 @@ public class MediaBindingBean { @Define("episodelist") public Object getEpisodeList() throws Exception { - for (EpisodeListProvider service : WebServices.getEpisodeListProviders()) { - if (getSeriesInfo().getDatabase().equals(service.getName())) { - return service.getEpisodeList(getSeriesInfo().getId(), SortOrder.forName(getSeriesInfo().getOrder()), new Locale(getSeriesInfo().getLanguage())); + return WebServices.getEpisodeListProvider(getSeriesInfo().getDatabase()).getEpisodeList(getSeriesInfo().getId(), SortOrder.forName(getSeriesInfo().getOrder()), new Locale(getSeriesInfo().getLanguage())); + } + + @Define("localize") + public Object getLocalizedInfoObject() throws Exception { + return new DynamicBindings(key -> { + Language language = Language.findLanguage(key); + if (language != null) { + Object localizedInfo = null; + try { + if (infoObject instanceof Movie) { + localizedInfo = WebServices.TheMovieDB.getMovieInfo(getMovie(), language.getLocale(), false); + } else if (infoObject instanceof Episode) { + EpisodeListProvider db = WebServices.getEpisodeListProvider(getSeriesInfo().getDatabase()); + List episodes = db.getEpisodeList(getSeriesInfo().getId(), SortOrder.forName(getSeriesInfo().getOrder()), language.getLocale()); + localizedInfo = episodes.stream().filter(it -> getEpisode().getNumbers().equals(it.getNumbers())).findFirst().orElse(null); + } + } catch (Exception e) { + throw new BindingException(key, e); + } + + if (localizedInfo != null) { + return createMapBindings(new PropertyBindings(localizedInfo, null)); + } } - } - return null; + return undefined(key); + }); } @Define("bitrate")