1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-02 08:25:02 -04:00

Translate AniDB::Absolute order to TheTVDB::Airdate order (matching by absolute episode number) only if preferred episode order is Airdate or DVD

@see https://www.filebot.net/forums/viewtopic.php?f=10&t=3805
This commit is contained in:
Reinhard Pointner 2016-05-22 20:50:32 +08:00
parent 1d02a00432
commit 837208a09b
3 changed files with 79 additions and 67 deletions

View File

@ -15,7 +15,7 @@ import static net.filebot.subtitle.SubtitleUtilities.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.RegularExpressions.*; import static net.filebot.util.RegularExpressions.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
import static net.filebot.web.EpisodeFormat.*; import static net.filebot.web.EpisodeUtilities.*;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -56,11 +56,10 @@ import net.filebot.util.FileUtilities;
import net.filebot.util.WeakValueHashMap; import net.filebot.util.WeakValueHashMap;
import net.filebot.web.AudioTrack; import net.filebot.web.AudioTrack;
import net.filebot.web.Episode; import net.filebot.web.Episode;
import net.filebot.web.EpisodeFormat;
import net.filebot.web.EpisodeListProvider; import net.filebot.web.EpisodeListProvider;
import net.filebot.web.Movie; import net.filebot.web.Movie;
import net.filebot.web.MoviePart; import net.filebot.web.MoviePart;
import net.filebot.web.MultiEpisode;
import net.filebot.web.SearchResult;
import net.filebot.web.SeriesInfo; import net.filebot.web.SeriesInfo;
import net.filebot.web.SimpleDate; import net.filebot.web.SimpleDate;
import net.filebot.web.SortOrder; import net.filebot.web.SortOrder;
@ -155,17 +154,12 @@ public class MediaBindingBean {
@Define("sxe") @Define("sxe")
public String getSxE() { public String getSxE() {
return SeasonEpisode.formatSxE(getEpisode()); return EpisodeFormat.SeasonEpisode.formatSxE(getSeasonEpisode()); // try to convert absolute numbers to SxE numbers
} }
@Define("s00e00") @Define("s00e00")
public String getS00E00() { public String getS00E00() {
try { return EpisodeFormat.SeasonEpisode.formatS00E00(getSeasonEpisode()); // try to convert absolute numbers to SxE numbers
return SeasonEpisode.formatS00E00(getSeasonEpisode()); // try to convert absolute numbers to SxE numbers
} catch (Exception e) {
debug.warning(e::toString);
}
return SeasonEpisode.formatS00E00(getEpisode());
} }
@Define("t") @Define("t")
@ -175,7 +169,7 @@ public class MediaBindingBean {
} }
// enforce title length limit by default // enforce title length limit by default
return truncateText(SeasonEpisode.formatMultiTitle(getEpisodes()), NamingStandard.TITLE_MAX_LENGTH); return truncateText(EpisodeFormat.SeasonEpisode.formatMultiTitle(getEpisodes()), NamingStandard.TITLE_MAX_LENGTH);
} }
@Define("d") @Define("d")
@ -693,48 +687,23 @@ public class MediaBindingBean {
@Define("anime") @Define("anime")
public boolean isAnimeEpisode() { public boolean isAnimeEpisode() {
return WebServices.AniDB.getIdentifier().equals(getEpisode().getSeriesInfo().getDatabase()); return getEpisodes().stream().anyMatch(it -> isAnime(it));
} }
@Define("regular") @Define("regular")
public boolean isRegularEpisode() { public boolean isRegularEpisode() {
return getEpisodes().stream().allMatch(it -> it.getEpisode() != null && it.getSpecial() == null); return getEpisodes().stream().anyMatch(it -> isRegular(it));
} }
@Define("abs2sxe") @Define("abs2sxe")
public Episode getSeasonEpisode() throws Exception { public Episode getSeasonEpisode() {
// match AniDB episode to TheTVDB episode if (getEpisodes().stream().allMatch(it -> isAnime(it) && isRegular(it) && !isAbsolute(it))) {
if (isAnimeEpisode()) { try {
SeriesInfo seriesInfo = getEpisode().getSeriesInfo(); return getEpisodeByAbsoluteNumber(getEpisode(), WebServices.TheTVDB, SortOrder.Airdate);
Locale locale = new Locale(seriesInfo.getLanguage()); } catch (Exception e) {
debug.warning(e::toString);
// episode may be a multi-episode
List<Episode> episode = getEpisodes();
for (SearchResult series : WebServices.TheTVDB.search(seriesInfo.getName(), locale)) {
// sanity check
if (!series.getEffectiveNames().contains(seriesInfo.getName())) {
continue;
}
// match by absolute number or airdate if possible, default to absolute number otherwise
List<Episode> airdateEpisodeList = WebServices.TheTVDB.getEpisodeList(series, SortOrder.Airdate, locale);
List<Episode> 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());
});
}).collect(toList());
// sanity check
if (airdateEpisode.size() != episode.size()) {
break;
}
return airdateEpisode.size() == 1 ? airdateEpisode.get(0) : new MultiEpisode(airdateEpisode);
} }
} }
// return episode object as is by default
return getEpisode(); return getEpisode();
} }
@ -846,7 +815,7 @@ public class MediaBindingBean {
@Define("episodes") @Define("episodes")
public List<Episode> getEpisodes() { public List<Episode> getEpisodes() {
return infoObject instanceof MultiEpisode ? ((MultiEpisode) infoObject).getEpisodes() : singletonList(getEpisode()); return getMultiEpisodeList(getEpisode());
} }
@Define("movie") @Define("movie")

View File

@ -66,7 +66,7 @@ public class AnidbClient extends AbstractEpisodeListProvider {
@Override @Override
protected SortOrder vetoRequestParameter(SortOrder order) { protected SortOrder vetoRequestParameter(SortOrder order) {
return SortOrder.Absolute; return order == null ? SortOrder.Absolute : order;
} }
@Override @Override

View File

@ -1,42 +1,84 @@
package net.filebot.web; package net.filebot.web;
import java.util.ArrayList; import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale;
import net.filebot.WebServices;
public final class EpisodeUtilities { public final class EpisodeUtilities {
public static List<Episode> filterBySeason(Iterable<Episode> episodes, int season) { public static List<Episode> getMultiEpisodeList(Episode e) {
List<Episode> results = new ArrayList<Episode>(25); return e instanceof MultiEpisode ? ((MultiEpisode) e).getEpisodes() : singletonList(e);
// filter given season from all seasons
for (Episode episode : episodes) {
if (episode.getSeason() != null && season == episode.getSeason()) {
results.add(episode);
}
} }
return results; public static boolean isAnime(Episode e) {
return WebServices.AniDB.getIdentifier().equals(e.getSeriesInfo().getDatabase());
} }
public static int getLastSeason(Iterable<Episode> episodes) { public static boolean isRegular(Episode e) {
int lastSeason = 0; return e.getEpisode() != null && e.getSpecial() == null;
// filter given season from all seasons
for (Episode episode : episodes) {
if (episode.getSeason() != null && episode.getSeason() > lastSeason) {
lastSeason = episode.getSeason();
}
} }
return lastSeason; public static boolean isAbsolute(Episode e) {
return e.getAbsolute() != null && e.getSeriesInfo().getOrder() != null && SortOrder.Absolute == SortOrder.valueOf(e.getSeriesInfo().getOrder());
}
public static Episode getEpisodeByAbsoluteNumber(Episode e, EpisodeListProvider service, SortOrder order) throws Exception {
// e.g. match AniDB episode to TheTVDB episode
SeriesInfo seriesInfo = e.getSeriesInfo();
Locale locale = new Locale(seriesInfo.getLanguage());
// episode may be a multi-episode
List<Episode> multiEpisode = getMultiEpisodeList(e);
for (SearchResult series : service.search(seriesInfo.getName(), locale)) {
// sanity check
if (!series.getEffectiveNames().contains(seriesInfo.getName())) {
continue;
}
// match by absolute number or airdate if possible, default to absolute number otherwise
List<Episode> airdateEpisodeList = service.getEpisodeList(series, order, locale);
List<Episode> airdateEpisode = multiEpisode.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());
});
}).collect(toList());
// sanity check
if (airdateEpisode.size() != multiEpisode.size()) {
break;
}
return airdateEpisode.size() == 1 ? airdateEpisode.get(0) : new MultiEpisode(airdateEpisode);
}
// return episode object as is by default
return e;
}
public static List<Episode> filterBySeason(Collection<Episode> episodes, int season) {
return episodes.stream().filter(it -> {
return it.getSeason() != null && season == it.getSeason();
}).collect(toList());
}
public static int getLastSeason(Collection<Episode> episodes) {
return episodes.stream().mapToInt(it -> {
return it.getSeason() == null ? 0 : it.getSeason();
}).max().orElse(0);
} }
public static Comparator<Episode> episodeComparator() { public static Comparator<Episode> episodeComparator() {
return NUMBERS_COMPARATOR; return EPISODE_NUMBERS_COMPARATOR;
} }
public static final Comparator<Episode> NUMBERS_COMPARATOR = new Comparator<Episode>() { public static final Comparator<Episode> EPISODE_NUMBERS_COMPARATOR = new Comparator<Episode>() {
@Override @Override
public int compare(Episode a, Episode b) { public int compare(Episode a, Episode b) {
@ -70,4 +112,5 @@ public final class EpisodeUtilities {
private EpisodeUtilities() { private EpisodeUtilities() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
} }