diff --git a/source/net/sourceforge/filebot/web/EpisodeListUtilities.java b/source/net/sourceforge/filebot/web/EpisodeListUtilities.java index 7d79eacd..9970024c 100644 --- a/source/net/sourceforge/filebot/web/EpisodeListUtilities.java +++ b/source/net/sourceforge/filebot/web/EpisodeListUtilities.java @@ -3,6 +3,8 @@ package net.sourceforge.filebot.web; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; @@ -23,6 +25,11 @@ final class EpisodeListUtilities { } + public static void sortEpisodes(List episodes) { + Collections.sort(episodes, episodeComparator()); + } + + public static int getLastSeason(Iterable episodes) { int lastSeason = 0; @@ -37,6 +44,41 @@ final class EpisodeListUtilities { } + public static Comparator episodeComparator() { + return new Comparator() { + + @Override + public int compare(Episode a, Episode b) { + int diff = compareValue(a.getSeriesName(), b.getSeriesName()); + if (diff != 0) + return diff; + + diff = compareValue(a.getSeason(), b.getSeason()); + if (diff != 0) + return diff; + + diff = compareValue(a.getEpisode(), b.getEpisode()); + if (diff != 0) + return diff; + + return compareValue(a.getTitle(), b.getTitle()); + } + + + private int compareValue(Comparable o1, T o2) { + if (o1 == null && o1 == null) + return 0; + if (o1 == null && o1 != null) + return Integer.MAX_VALUE; + if (o1 != null && o2 == null) + return Integer.MIN_VALUE; + + return o1.compareTo(o2); + } + }; + } + + private EpisodeListUtilities() { throw new UnsupportedOperationException(); } diff --git a/source/net/sourceforge/filebot/web/TheTVDBClient.java b/source/net/sourceforge/filebot/web/TheTVDBClient.java index 9189a24e..18d23ed4 100644 --- a/source/net/sourceforge/filebot/web/TheTVDBClient.java +++ b/source/net/sourceforge/filebot/web/TheTVDBClient.java @@ -141,12 +141,24 @@ public class TheTVDBClient implements EpisodeListProvider { for (Node node : nodes) { String episodeName = getTextContent("EpisodeName", node); - Integer episodeNumber = getIntegerContent("EpisodeNumber", node); + String dvdSeasonNumber = getTextContent("DVD_season", node); + String dvdEpisodeNumber = getTextContent("DVD_episodenumber", node); Integer absoluteNumber = getIntegerContent("absolute_number", node); - Integer seasonNumber = getIntegerContent("SeasonNumber", node); Date airdate = Date.parse(getTextContent("FirstAired", node), "yyyy-MM-dd"); - if (seasonNumber == 0) { + // prefer DVD SxE numbers if available + Integer seasonNumber; + Integer episodeNumber; + + try { + seasonNumber = new Integer(dvdSeasonNumber); + episodeNumber = new Float(dvdEpisodeNumber).intValue(); + } catch (Exception e) { + seasonNumber = getIntegerContent("SeasonNumber", node); + episodeNumber = getIntegerContent("EpisodeNumber", node); + } + + if (seasonNumber == null || seasonNumber == 0) { // handle as special episode Integer airsBefore = getIntegerContent("airsbefore_season", node); if (airsBefore != null) { @@ -171,6 +183,9 @@ public class TheTVDBClient implements EpisodeListProvider { } } + // episodes my not be ordered by DVD episode number + sortEpisodes(episodes); + // add specials at the end episodes.addAll(specials); diff --git a/test/net/sourceforge/filebot/web/TheTVDBClientTest.java b/test/net/sourceforge/filebot/web/TheTVDBClientTest.java index cf65c480..6db290a0 100644 --- a/test/net/sourceforge/filebot/web/TheTVDBClientTest.java +++ b/test/net/sourceforge/filebot/web/TheTVDBClientTest.java @@ -98,6 +98,22 @@ public class TheTVDBClientTest { } + @Test + public void getEpisodeListNumbering() throws Exception { + List list = thetvdb.getEpisodeList(new TheTVDBSearchResult("Firefly", 78874), 1); + + assertEquals(14, list.size()); + + Episode first = list.get(0); + assertEquals("Firefly", first.getSeriesName()); + assertEquals("Serenity", first.getTitle()); + assertEquals("1", first.getEpisode().toString()); + assertEquals("1", first.getSeason().toString()); + assertEquals("1", first.getAbsolute().toString()); + assertEquals("2002-12-20", first.airdate().toString()); + } + + @Test public void getEpisodeListLink() { assertEquals("http://www.thetvdb.com/?tab=seasonall&id=78874", thetvdb.getEpisodeListLink(new TheTVDBSearchResult("Firefly", 78874)).toString());