From 2c117561e339910379ccd364bb9da2ace833bff7 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 23 Apr 2016 00:47:43 +0800 Subject: [PATCH] try to convert absolute numbers to SxE numbers when using {s00e00} binding (as best as possible, SxE numbers may not exactly match TheTVDB, but should in most common cases) --- .../net/filebot/format/MediaBindingBean.java | 23 ++++++++++++------- source/net/filebot/web/MultiEpisode.java | 5 ++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index ab0867fc..7b26db10 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -686,18 +686,25 @@ public class MediaBindingBean { // match AniDB episode to TheTVDB episode if (WebServices.AniDB.getIdentifier().equals(seriesInfo.getDatabase())) { Locale locale = new Locale(seriesInfo.getLanguage()); - List series = WebServices.TheTVDB.search(seriesInfo.getName(), locale); - if (series.size() > 0) { - List airdateEpisodeList = WebServices.TheTVDB.getEpisodeList(series.get(0), SortOrder.Airdate, locale); + List episode = getEpisodes(); + + for (SearchResult series : WebServices.TheTVDB.search(seriesInfo.getName(), locale)) { + // sanity check search result + if (!series.getEffectiveNames().contains(seriesInfo.getName())) + continue; + + List airdateEpisodeList = WebServices.TheTVDB.getEpisodeList(series, SortOrder.Airdate, locale); // match by absolute number or airdate if possible, default to absolute number otherwise - Episode[] episodes = getEpisodes().stream().map(abs -> { + List airdateEpisode = episode.stream().flatMap(abs -> { return airdateEpisodeList.stream().filter(sxe -> abs.getSpecial() == null && sxe.getSpecial() == null).filter(sxe -> { - return abs.getAbsolute() != null && abs.getAbsolute().equals(sxe.getAbsolute()) || abs.getAirdate() != null && abs.getAirdate().equals(sxe.getAirdate()); - }).findFirst().orElse(abs); - }).toArray(Episode[]::new); + return abs.getAbsolute() != null && abs.getAbsolute().equals(sxe.getAbsolute()); + }); + }).collect(toList()); - return episodes.length == 1 ? episodes[0] : new MultiEpisode(episodes); + if (airdateEpisode.size() == episode.size()) { + return airdateEpisode.size() == 1 ? airdateEpisode.get(0) : new MultiEpisode(airdateEpisode); + } } } diff --git a/source/net/filebot/web/MultiEpisode.java b/source/net/filebot/web/MultiEpisode.java index 3c87739f..a1ba8e65 100644 --- a/source/net/filebot/web/MultiEpisode.java +++ b/source/net/filebot/web/MultiEpisode.java @@ -19,6 +19,11 @@ public class MultiEpisode extends Episode { this.episodes = episodes.clone(); } + public MultiEpisode(List episodes) { + super(episodes.get(0)); + this.episodes = episodes.toArray(new Episode[0]); + } + public List getEpisodes() { return unmodifiableList(asList(episodes)); }