diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index b97599b9..982883d6 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -679,8 +679,25 @@ public class MediaBindingBean { return null; } + @Define("order") + public DynamicBindings getSortOrderObject() { + return new DynamicBindings(SortOrder::names, k -> { + if (infoObject instanceof Episode) { + try { + SortOrder order = SortOrder.forName(k); + SeriesInfo info = getSeriesInfo(); + List episodeList = getEpisodeListProvider(info.getDatabase()).getEpisodeList(info.getId(), order, new Locale(info.getLanguage())); + return createBindingObject(null, createEpisode(episodeList.stream().filter(e -> getEpisodes().contains(e))), null); + } catch (Exception e) { + throw new BindingException(k, e); + } + } + return undefined(k); + }); + } + @Define("localize") - public Object getLocalizedInfoObject() { + public DynamicBindings getLocalizedInfoObject() { return new DynamicBindings(Language::availableLanguages, k -> { Language language = Language.findLanguage(k); if (language == null) { @@ -690,13 +707,13 @@ public class MediaBindingBean { try { if (infoObject instanceof Movie) { MovieInfo m = getMovieInfo(language.getLocale(), true); - return createPropertyBindings(m); + return createPropertyBindings(m); // TODO use createBindingObject -> BREAKING CHANGE } if (infoObject instanceof Episode) { SeriesInfo i = getSeriesInfo(); List es = getEpisodeListProvider(i.getDatabase()).getEpisodeList(i.getId(), SortOrder.forName(i.getOrder()), language.getLocale()); Episode e = es.stream().filter(it -> getEpisode().getNumbers().equals(it.getNumbers())).findFirst().get(); - return createPropertyBindings(e); + return createPropertyBindings(e); // TODO use createBindingObject -> BREAKING CHANGE } } catch (Exception e) { throw new BindingException(k, e); @@ -725,18 +742,6 @@ public class MediaBindingBean { return getEpisodes().stream().anyMatch(it -> isRegular(it)); } - @Define("abs2sxe") - public Episode getSeasonEpisode() { - if (getEpisodes().stream().allMatch(it -> isAnime(it) && isRegular(it) && !isAbsolute(it))) { - try { - return getEpisodeByAbsoluteNumber(getEpisode(), TheTVDB, SortOrder.Airdate); - } catch (Exception e) { - debug.warning(e::toString); - } - } - return getEpisode(); - } - @Define("episodelist") public List getEpisodeList() throws Exception { SeriesInfo i = getSeriesInfo(); @@ -1036,6 +1041,17 @@ public class MediaBindingBean { return getMediaFile(); } + public Episode getSeasonEpisode() { + if (getEpisodes().stream().allMatch(it -> isAnime(it) && isRegular(it) && !isAbsolute(it))) { + try { + return getEpisodeByAbsoluteNumber(getEpisode(), TheTVDB, SortOrder.Airdate); + } catch (Exception e) { + debug.warning(e::toString); + } + } + return getEpisode(); + } + public SeriesInfo getPrimarySeriesInfo() { if (TheTVDB.getIdentifier().equals(getSeriesInfo().getDatabase())) { try { diff --git a/source/net/filebot/web/EpisodeUtilities.java b/source/net/filebot/web/EpisodeUtilities.java index 216e0ccf..de003b51 100644 --- a/source/net/filebot/web/EpisodeUtilities.java +++ b/source/net/filebot/web/EpisodeUtilities.java @@ -8,13 +8,25 @@ import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.Locale; +import java.util.NoSuchElementException; import java.util.Set; import java.util.TreeSet; +import java.util.stream.Stream; import net.filebot.WebServices; public final class EpisodeUtilities { + public static Episode createEpisode(Stream episode) { + List es = episode.sorted(EPISODE_NUMBERS_COMPARATOR).collect(toList()); + + if (es.isEmpty()) { + throw new NoSuchElementException("No such Episode"); + } + + return es.size() == 1 ? es.get(0) : new MultiEpisode(es); + } + public static List getMultiEpisodeList(Episode e) { return e instanceof MultiEpisode ? ((MultiEpisode) e).getEpisodes() : singletonList(e); } diff --git a/source/net/filebot/web/MultiEpisode.java b/source/net/filebot/web/MultiEpisode.java index 7bdbc2ba..6e105c2b 100644 --- a/source/net/filebot/web/MultiEpisode.java +++ b/source/net/filebot/web/MultiEpisode.java @@ -55,6 +55,10 @@ public class MultiEpisode extends Episode { return episodes[0].getAirdate(); } + public Integer getId() { + return episodes[0].getId(); + } + public SeriesInfo getSeriesInfo() { return episodes[0].getSeriesInfo(); } diff --git a/source/net/filebot/web/SortOrder.java b/source/net/filebot/web/SortOrder.java index 1b1bd884..7f449574 100644 --- a/source/net/filebot/web/SortOrder.java +++ b/source/net/filebot/web/SortOrder.java @@ -1,9 +1,23 @@ package net.filebot.web; +import static java.util.Arrays.*; +import static java.util.stream.Collectors.*; + +import java.util.List; + public enum SortOrder { Airdate, DVD, Absolute; + @Override + public String toString() { + return name() + " Order"; + } + + public static List names() { + return stream(values()).map(SortOrder::name).collect(toList()); + } + public static SortOrder forName(String name) { for (SortOrder order : SortOrder.values()) { if (order.name().equalsIgnoreCase(name)) { @@ -11,11 +25,7 @@ public enum SortOrder { } } - throw new IllegalArgumentException("Invalid SortOrder: " + name); + throw new IllegalArgumentException("Illegal SortOrder: " + name); } - @Override - public String toString() { - return String.format("%s Order", name()); - } }