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")