* thetvdb: prefer DVD episode ordering

This commit is contained in:
Reinhard Pointner 2010-11-15 10:06:24 +00:00
parent 038dcb38e8
commit 051c34c98d
3 changed files with 76 additions and 3 deletions

View File

@ -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<Episode> episodes) {
Collections.sort(episodes, episodeComparator());
}
public static int getLastSeason(Iterable<Episode> episodes) {
int lastSeason = 0;
@ -37,6 +44,41 @@ final class EpisodeListUtilities {
}
public static Comparator<Episode> episodeComparator() {
return new Comparator<Episode>() {
@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 <T> int compareValue(Comparable<T> 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();
}

View File

@ -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);

View File

@ -98,6 +98,22 @@ public class TheTVDBClientTest {
}
@Test
public void getEpisodeListNumbering() throws Exception {
List<Episode> 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());