2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
package net.sourceforge.filebot.web;
|
|
|
|
|
|
|
|
|
2011-10-28 04:07:02 -04:00
|
|
|
import static net.sourceforge.filebot.web.EpisodeUtilities.*;
|
2009-08-18 12:15:58 -04:00
|
|
|
import static net.sourceforge.filebot.web.WebRequest.*;
|
2009-05-17 13:22:44 -04:00
|
|
|
import static net.sourceforge.tuned.XPathUtilities.*;
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
2011-12-20 08:00:38 -05:00
|
|
|
import java.net.MalformedURLException;
|
2009-01-04 13:28:28 -05:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.EnumSet;
|
2011-11-04 05:11:11 -04:00
|
|
|
import java.util.LinkedHashMap;
|
2009-01-04 13:28:28 -05:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
|
|
|
|
import javax.swing.Icon;
|
2009-07-13 08:41:19 -04:00
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
import net.sf.ehcache.CacheManager;
|
|
|
|
import net.sourceforge.filebot.ResourceManager;
|
2011-12-20 08:00:38 -05:00
|
|
|
import net.sourceforge.filebot.web.TheTVDBClient.BannerDescriptor.BannerProperty;
|
|
|
|
import net.sourceforge.tuned.FileUtilities;
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
|
2011-08-08 13:37:45 -04:00
|
|
|
public class TheTVDBClient extends AbstractEpisodeListProvider {
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2011-11-13 13:22:50 -05:00
|
|
|
private final String host = "www.thetvdb.com";
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
private final Map<MirrorType, String> mirrors = new EnumMap<MirrorType, String>(MirrorType.class);
|
|
|
|
|
2011-11-13 13:22:50 -05:00
|
|
|
private final String apikey;
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
public TheTVDBClient(String apikey) {
|
2009-01-24 19:08:57 -05:00
|
|
|
if (apikey == null)
|
|
|
|
throw new NullPointerException("apikey must not be null");
|
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
this.apikey = apikey;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "TheTVDB";
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
|
|
|
public Icon getIcon() {
|
|
|
|
return ResourceManager.getIcon("search.thetvdb");
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
|
|
|
public boolean hasSingleSeasonSupport() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
2011-08-08 13:37:45 -04:00
|
|
|
public boolean hasLocaleSupport() {
|
|
|
|
return true;
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2011-08-08 13:37:45 -04:00
|
|
|
@Override
|
2011-11-26 04:44:49 -05:00
|
|
|
public ResultCache getCache() {
|
|
|
|
return new ResultCache(host, CacheManager.getInstance().getCache("web-datasource"));
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2011-11-26 04:44:49 -05:00
|
|
|
@Override
|
|
|
|
public List<SearchResult> fetchSearchResult(String query, Locale language) throws Exception {
|
2010-11-09 03:04:12 -05:00
|
|
|
// perform online search
|
2011-11-04 05:11:11 -04:00
|
|
|
URL url = getResource(null, "/api/GetSeries.php?seriesname=" + encode(query) + "&language=" + language.getLanguage());
|
2009-08-18 12:15:58 -04:00
|
|
|
Document dom = getDocument(url);
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
List<Node> nodes = selectNodes("Data/Series", dom);
|
2011-11-04 05:11:11 -04:00
|
|
|
Map<Integer, TheTVDBSearchResult> resultSet = new LinkedHashMap<Integer, TheTVDBSearchResult>();
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
for (Node node : nodes) {
|
2011-11-04 05:11:11 -04:00
|
|
|
int sid = getIntegerContent("seriesid", node);
|
2009-02-21 07:48:52 -05:00
|
|
|
String seriesName = getTextContent("SeriesName", node);
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2011-11-04 05:11:11 -04:00
|
|
|
if (!resultSet.containsKey(sid)) {
|
|
|
|
resultSet.put(sid, new TheTVDBSearchResult(seriesName, sid));
|
|
|
|
}
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
2011-11-04 05:11:11 -04:00
|
|
|
return new ArrayList<SearchResult>(resultSet.values());
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
2011-11-26 04:44:49 -05:00
|
|
|
public List<Episode> fetchEpisodeList(SearchResult searchResult, Locale language) throws Exception {
|
2011-08-08 13:37:45 -04:00
|
|
|
TheTVDBSearchResult series = (TheTVDBSearchResult) searchResult;
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2011-08-08 13:37:45 -04:00
|
|
|
Document seriesRecord = getSeriesRecord(series, language);
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
// we could get the series name from the search result, but the language may not match the given parameter
|
|
|
|
String seriesName = selectString("Data/Series/SeriesName", seriesRecord);
|
2011-10-01 00:08:46 -04:00
|
|
|
Date seriesStartDate = Date.parse(selectString("Data/Series/FirstAired", seriesRecord), "yyyy-MM-dd");
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
List<Node> nodes = selectNodes("Data/Episode", seriesRecord);
|
2010-02-03 16:36:04 -05:00
|
|
|
|
2011-11-26 04:44:49 -05:00
|
|
|
List<Episode> episodes = new ArrayList<Episode>(nodes.size());
|
2010-02-03 16:36:04 -05:00
|
|
|
List<Episode> specials = new ArrayList<Episode>(5);
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
for (Node node : nodes) {
|
|
|
|
String episodeName = getTextContent("EpisodeName", node);
|
2010-11-15 05:06:24 -05:00
|
|
|
String dvdSeasonNumber = getTextContent("DVD_season", node);
|
|
|
|
String dvdEpisodeNumber = getTextContent("DVD_episodenumber", node);
|
2010-11-09 03:04:12 -05:00
|
|
|
Integer absoluteNumber = getIntegerContent("absolute_number", node);
|
2010-10-23 08:47:43 -04:00
|
|
|
Date airdate = Date.parse(getTextContent("FirstAired", node), "yyyy-MM-dd");
|
2009-05-03 11:21:04 -04:00
|
|
|
|
2010-11-15 05:06:24 -05:00
|
|
|
// 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) {
|
2010-02-03 16:36:04 -05:00
|
|
|
// handle as special episode
|
2010-10-24 08:10:30 -04:00
|
|
|
Integer airsBefore = getIntegerContent("airsbefore_season", node);
|
|
|
|
if (airsBefore != null) {
|
2010-02-03 16:36:04 -05:00
|
|
|
seasonNumber = airsBefore;
|
|
|
|
}
|
|
|
|
|
2011-10-23 10:09:41 -04:00
|
|
|
// use given episode number as special number or count specials by ourselves
|
|
|
|
Integer specialNumber = (episodeNumber != null) ? episodeNumber : filterBySeason(specials, seasonNumber).size() + 1;
|
2011-10-01 00:08:46 -04:00
|
|
|
specials.add(new Episode(seriesName, seriesStartDate, seasonNumber, null, episodeName, null, specialNumber, airdate));
|
2010-02-03 16:36:04 -05:00
|
|
|
} else {
|
|
|
|
// handle as normal episode
|
2011-10-01 00:08:46 -04:00
|
|
|
episodes.add(new Episode(seriesName, seriesStartDate, seasonNumber, episodeNumber, episodeName, absoluteNumber, null, airdate));
|
2010-02-03 16:36:04 -05:00
|
|
|
}
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
2010-11-15 05:06:24 -05:00
|
|
|
// episodes my not be ordered by DVD episode number
|
|
|
|
sortEpisodes(episodes);
|
|
|
|
|
2010-02-03 16:36:04 -05:00
|
|
|
// add specials at the end
|
|
|
|
episodes.addAll(specials);
|
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-07-13 08:41:19 -04:00
|
|
|
public Document getSeriesRecord(TheTVDBSearchResult searchResult, Locale language) throws Exception {
|
2009-08-18 12:15:58 -04:00
|
|
|
URL seriesRecord = getResource(MirrorType.ZIP, "/api/" + apikey + "/series/" + searchResult.getSeriesId() + "/all/" + language.getLanguage() + ".zip");
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2009-07-13 08:41:19 -04:00
|
|
|
ZipInputStream zipInputStream = new ZipInputStream(seriesRecord.openStream());
|
2009-01-04 13:28:28 -05:00
|
|
|
ZipEntry zipEntry;
|
|
|
|
|
|
|
|
try {
|
|
|
|
String seriesRecordName = language.getLanguage() + ".xml";
|
|
|
|
|
|
|
|
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
|
|
|
if (seriesRecordName.equals(zipEntry.getName())) {
|
2009-07-13 08:41:19 -04:00
|
|
|
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(zipInputStream);
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// zip file must contain the series record
|
2009-07-13 08:41:19 -04:00
|
|
|
throw new FileNotFoundException(String.format("Archive must contain %s: %s", seriesRecordName, seriesRecord));
|
2009-01-04 13:28:28 -05:00
|
|
|
} finally {
|
|
|
|
zipInputStream.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2011-12-13 20:19:36 -05:00
|
|
|
public TheTVDBSearchResult lookupByID(int id, Locale language) throws Exception {
|
2011-12-12 09:06:26 -05:00
|
|
|
try {
|
|
|
|
URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml");
|
2011-12-13 20:19:36 -05:00
|
|
|
Document baseRecord = getDocument(baseRecordLocation);
|
2011-12-12 09:06:26 -05:00
|
|
|
|
|
|
|
String name = selectString("//SeriesName", baseRecord);
|
|
|
|
return new TheTVDBSearchResult(name, id);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// illegal series id
|
2011-12-13 20:19:36 -05:00
|
|
|
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to retrieve base series record: " + e.getMessage());
|
2011-12-12 09:06:26 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-13 20:19:36 -05:00
|
|
|
public TheTVDBSearchResult lookupByIMDbID(int imdbid, Locale language) throws Exception {
|
2011-12-13 20:31:27 -05:00
|
|
|
URL query = getResource(null, "/api/GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + language.getLanguage());
|
2011-12-13 20:19:36 -05:00
|
|
|
Document dom = getDocument(query);
|
|
|
|
|
|
|
|
String id = selectString("//seriesid", dom);
|
|
|
|
String name = selectString("//SeriesName", dom);
|
|
|
|
|
|
|
|
if (id == null || id.isEmpty() || name == null || name.isEmpty())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return new TheTVDBSearchResult(name, Integer.parseInt(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
|
|
|
public URI getEpisodeListLink(SearchResult searchResult) {
|
|
|
|
int seriesId = ((TheTVDBSearchResult) searchResult).getSeriesId();
|
|
|
|
|
2009-03-17 17:59:19 -04:00
|
|
|
return URI.create("http://" + host + "/?tab=seasonall&id=" + seriesId);
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
@Override
|
|
|
|
public URI getEpisodeListLink(SearchResult searchResult, int season) {
|
|
|
|
int seriesId = ((TheTVDBSearchResult) searchResult).getSeriesId();
|
|
|
|
|
|
|
|
try {
|
2011-11-04 05:11:11 -04:00
|
|
|
// get episode xml from first episode of given season
|
|
|
|
Document dom = getDocument(getResource(MirrorType.XML, "/api/" + apikey + "/series/" + seriesId + "/default/" + season + "/1/en.xml"));
|
|
|
|
int seasonId = Integer.valueOf(selectString("Data/Episode/seasonid", dom));
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2009-02-06 12:57:18 -05:00
|
|
|
return new URI("http://" + host + "/?tab=season&seriesid=" + seriesId + "&seasonid=" + seasonId);
|
2009-03-17 17:59:19 -04:00
|
|
|
} catch (Exception e) {
|
2009-02-06 12:57:18 -05:00
|
|
|
// log and ignore any IOException
|
2009-03-14 06:20:59 -04:00
|
|
|
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to retrieve season id", e);
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-07-13 08:41:19 -04:00
|
|
|
protected String getMirror(MirrorType mirrorType) throws Exception {
|
2009-01-04 13:28:28 -05:00
|
|
|
synchronized (mirrors) {
|
|
|
|
if (mirrors.isEmpty()) {
|
|
|
|
// initialize mirrors
|
2009-08-18 12:15:58 -04:00
|
|
|
Document dom = getDocument(getResource(null, "/api/" + apikey + "/mirrors.xml"));
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
// all mirrors by type
|
|
|
|
Map<MirrorType, List<String>> mirrorListMap = new EnumMap<MirrorType, List<String>>(MirrorType.class);
|
|
|
|
|
|
|
|
// initialize mirror list per type
|
|
|
|
for (MirrorType type : MirrorType.values()) {
|
|
|
|
mirrorListMap.put(type, new ArrayList<String>(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
// traverse all mirrors
|
|
|
|
for (Node node : selectNodes("Mirrors/Mirror", dom)) {
|
|
|
|
// mirror data
|
2009-02-21 07:48:52 -05:00
|
|
|
String mirror = getTextContent("mirrorpath", node);
|
|
|
|
int typeMask = Integer.parseInt(getTextContent("typemask", node));
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
// add mirror to the according type lists
|
|
|
|
for (MirrorType type : MirrorType.fromTypeMask(typeMask)) {
|
|
|
|
mirrorListMap.get(type).add(mirror);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// put random entry from each type list into mirrors
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
|
|
for (MirrorType type : MirrorType.values()) {
|
|
|
|
List<String> list = mirrorListMap.get(type);
|
|
|
|
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
mirrors.put(type, list.get(random.nextInt(list.size())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mirrors.get(mirrorType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-08-18 12:15:58 -04:00
|
|
|
protected URL getResource(MirrorType mirrorType, String path) throws Exception {
|
|
|
|
// use default server
|
|
|
|
if (mirrorType == null)
|
|
|
|
return new URL("http", host, path);
|
|
|
|
|
|
|
|
// use mirror
|
|
|
|
return new URL(getMirror(mirrorType) + path);
|
2009-07-13 08:41:19 -04:00
|
|
|
}
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
public static class TheTVDBSearchResult extends SearchResult {
|
|
|
|
|
2011-11-26 04:44:49 -05:00
|
|
|
protected int seriesId;
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2011-11-26 04:44:49 -05:00
|
|
|
protected TheTVDBSearchResult() {
|
|
|
|
// used by serializer
|
|
|
|
}
|
2009-01-04 13:28:28 -05:00
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
public TheTVDBSearchResult(String seriesName, int seriesId) {
|
|
|
|
super(seriesName);
|
|
|
|
this.seriesId = seriesId;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
public int getSeriesId() {
|
|
|
|
return seriesId;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2011-11-26 04:44:49 -05:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return seriesId;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2011-11-26 04:44:49 -05:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object object) {
|
|
|
|
if (object instanceof TheTVDBSearchResult) {
|
|
|
|
TheTVDBSearchResult other = (TheTVDBSearchResult) object;
|
|
|
|
return this.seriesId == other.seriesId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
protected static enum MirrorType {
|
|
|
|
XML(1),
|
|
|
|
BANNER(2),
|
|
|
|
ZIP(4);
|
|
|
|
|
|
|
|
private final int bitMask;
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
private MirrorType(int bitMask) {
|
|
|
|
this.bitMask = bitMask;
|
|
|
|
}
|
|
|
|
|
2011-12-12 09:06:26 -05:00
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
public static EnumSet<MirrorType> fromTypeMask(int typeMask) {
|
|
|
|
// initialize enum set with all types
|
|
|
|
EnumSet<MirrorType> enumSet = EnumSet.allOf(MirrorType.class);
|
|
|
|
|
|
|
|
for (MirrorType type : values()) {
|
|
|
|
if ((typeMask & type.bitMask) == 0) {
|
|
|
|
// remove types that are not set
|
|
|
|
enumSet.remove(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return enumSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:37:36 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a series banner matching the given parameters
|
|
|
|
*
|
|
|
|
* @see http://thetvdb.com/wiki/index.php/API:banners.xml
|
|
|
|
*/
|
2011-12-20 08:00:38 -05:00
|
|
|
public BannerDescriptor getBanner(TheTVDBSearchResult series, String bannerType, String bannerType2, Integer season, Locale locale) throws Exception {
|
2011-12-19 21:37:36 -05:00
|
|
|
// search for a banner matching the selector
|
2011-12-20 08:00:38 -05:00
|
|
|
for (BannerDescriptor it : getBannerList(series.seriesId)) {
|
|
|
|
if ((bannerType == null || it.getBannerType().equalsIgnoreCase(bannerType)) && (bannerType2 == null || it.getBannerType2().equalsIgnoreCase(bannerType2)) && (season == null || it.getSeason().equals(season))
|
|
|
|
&& ((locale == null && it.getLocale().getLanguage().equals("en")) || it.getLocale().getLanguage().equals(locale.getLanguage()))) {
|
2011-12-19 21:37:36 -05:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-20 08:00:38 -05:00
|
|
|
public List<BannerDescriptor> getBannerList(int seriesid) throws Exception {
|
2011-12-19 21:37:36 -05:00
|
|
|
Document dom = getDocument(getResource(MirrorType.XML, "/api/" + apikey + "/series/" + seriesid + "/banners.xml"));
|
|
|
|
|
|
|
|
List<Node> nodes = selectNodes("//Banner", dom);
|
2011-12-20 08:00:38 -05:00
|
|
|
List<BannerDescriptor> banners = new ArrayList<BannerDescriptor>();
|
2011-12-19 21:37:36 -05:00
|
|
|
|
|
|
|
for (Node node : nodes) {
|
|
|
|
try {
|
2011-12-20 08:00:38 -05:00
|
|
|
EnumMap<BannerProperty, String> item = new EnumMap<BannerProperty, String>(BannerProperty.class);
|
|
|
|
|
|
|
|
// insert banner mirror
|
|
|
|
item.put(BannerProperty.BannerMirror, getResource(MirrorType.BANNER, "/banners/").toString());
|
2011-12-19 21:37:36 -05:00
|
|
|
|
|
|
|
// copy values from xml
|
|
|
|
for (BannerProperty key : BannerProperty.values()) {
|
|
|
|
String value = getTextContent(key.name(), node);
|
|
|
|
if (value != null && value.length() > 0) {
|
|
|
|
item.put(key, getTextContent(key.name(), node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 08:00:38 -05:00
|
|
|
banners.add(new BannerDescriptor(item));
|
2011-12-19 21:37:36 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
// log and ignore
|
|
|
|
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Invalid banner descriptor", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return banners;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-20 08:00:38 -05:00
|
|
|
public static class BannerDescriptor {
|
|
|
|
|
|
|
|
public static enum BannerProperty {
|
|
|
|
id,
|
|
|
|
BannerMirror,
|
|
|
|
BannerPath,
|
|
|
|
BannerType,
|
|
|
|
BannerType2,
|
|
|
|
Season,
|
|
|
|
Colors,
|
|
|
|
Language,
|
|
|
|
Rating,
|
|
|
|
RatingCount,
|
|
|
|
SeriesName,
|
|
|
|
ThumbnailPath,
|
|
|
|
VignettePath
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private EnumMap<BannerProperty, String> fields;
|
|
|
|
|
|
|
|
|
|
|
|
protected BannerDescriptor(Map<BannerProperty, String> fields) {
|
|
|
|
this.fields = new EnumMap<BannerProperty, String>(fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public URL getMirrorUrl() throws MalformedURLException {
|
|
|
|
return new URL(fields.get(BannerProperty.BannerMirror));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public URL getUrl() throws MalformedURLException {
|
|
|
|
return new URL(getMirrorUrl(), fields.get(BannerProperty.BannerPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getExtension() {
|
|
|
|
return FileUtilities.getExtension(fields.get(BannerProperty.BannerPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getId() {
|
|
|
|
return Integer.parseInt(fields.get(BannerProperty.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getBannerType() {
|
|
|
|
return fields.get(BannerProperty.BannerType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getBannerType2() {
|
|
|
|
return fields.get(BannerProperty.BannerType2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Integer getSeason() {
|
|
|
|
try {
|
|
|
|
return new Integer(fields.get(BannerProperty.Season));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-19 21:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-20 08:00:38 -05:00
|
|
|
public String getColors() {
|
|
|
|
return fields.get(BannerProperty.Colors);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Locale getLocale() {
|
|
|
|
return new Locale(fields.get(BannerProperty.Language));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double getRating() {
|
|
|
|
return Double.parseDouble(fields.get(BannerProperty.Rating));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getRatingCount() {
|
|
|
|
return Integer.parseInt(fields.get(BannerProperty.RatingCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean hasSeriesName() {
|
|
|
|
return Boolean.parseBoolean(fields.get(BannerProperty.SeriesName));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public URL getThumbnailUrl() throws MalformedURLException {
|
|
|
|
return new URL(getMirrorUrl(), fields.get(BannerProperty.ThumbnailPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public URL getVignetteUrl() throws MalformedURLException {
|
|
|
|
return new URL(getMirrorUrl(), fields.get(BannerProperty.VignettePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return fields.toString();
|
2011-12-19 21:37:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 13:28:28 -05:00
|
|
|
}
|