* refactoring
This commit is contained in:
parent
cdf2487f2c
commit
b4f9a3ed24
|
@ -3,11 +3,9 @@ package net.sourceforge.filebot.web;
|
||||||
|
|
||||||
|
|
||||||
import static net.sourceforge.filebot.web.EpisodeListUtilities.*;
|
import static net.sourceforge.filebot.web.EpisodeListUtilities.*;
|
||||||
import static net.sourceforge.filebot.web.WebRequest.*;
|
|
||||||
import static net.sourceforge.tuned.XPathUtilities.*;
|
import static net.sourceforge.tuned.XPathUtilities.*;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
@ -25,17 +23,16 @@ import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
import javax.swing.Icon;
|
import javax.swing.Icon;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
import net.sf.ehcache.Cache;
|
import net.sf.ehcache.Cache;
|
||||||
import net.sf.ehcache.CacheManager;
|
import net.sf.ehcache.CacheManager;
|
||||||
import net.sf.ehcache.Element;
|
import net.sf.ehcache.Element;
|
||||||
import net.sourceforge.filebot.ResourceManager;
|
import net.sourceforge.filebot.ResourceManager;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Node;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
|
|
||||||
public class TheTVDBClient implements EpisodeListProvider {
|
public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
|
@ -47,7 +44,7 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
private final TheTVDBCache cache = new TheTVDBCache(CacheManager.getInstance().getCache("web"));
|
private final TheTVDBCache cache = new TheTVDBCache(CacheManager.getInstance().getCache("web"));
|
||||||
|
|
||||||
|
|
||||||
public TheTVDBClient(String apikey) {
|
public TheTVDBClient(String apikey) {
|
||||||
if (apikey == null)
|
if (apikey == null)
|
||||||
throw new NullPointerException("apikey must not be null");
|
throw new NullPointerException("apikey must not be null");
|
||||||
|
@ -82,12 +79,9 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
public List<SearchResult> search(String query, Locale language) throws Exception {
|
public List<SearchResult> search(String query, Locale language) throws Exception {
|
||||||
|
|
||||||
URL searchUrl = new URL("http", host, "/api/GetSeries.php?seriesname=" + URLEncoder.encode(query, "UTF-8") + "&language=" + language.getLanguage());
|
Document dom = getXmlDocument("/api/GetSeries.php?seriesname=" + URLEncoder.encode(query, "UTF-8") + "&language=" + language.getLanguage());
|
||||||
|
|
||||||
Document dom = getDocument(searchUrl);
|
|
||||||
|
|
||||||
List<Node> nodes = selectNodes("Data/Series", dom);
|
List<Node> nodes = selectNodes("Data/Series", dom);
|
||||||
|
|
||||||
List<SearchResult> searchResults = new ArrayList<SearchResult>(nodes.size());
|
List<SearchResult> searchResults = new ArrayList<SearchResult>(nodes.size());
|
||||||
|
|
||||||
for (Node node : nodes) {
|
for (Node node : nodes) {
|
||||||
|
@ -133,7 +127,6 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
String seriesName = selectString("Data/Series/SeriesName", seriesRecord);
|
String seriesName = selectString("Data/Series/SeriesName", seriesRecord);
|
||||||
|
|
||||||
List<Node> nodes = selectNodes("Data/Episode", seriesRecord);
|
List<Node> nodes = selectNodes("Data/Episode", seriesRecord);
|
||||||
|
|
||||||
episodes = new ArrayList<Episode>(nodes.size());
|
episodes = new ArrayList<Episode>(nodes.size());
|
||||||
|
|
||||||
for (Node node : nodes) {
|
for (Node node : nodes) {
|
||||||
|
@ -162,11 +155,11 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Document getSeriesRecord(TheTVDBSearchResult searchResult, Locale language) throws IOException, SAXException, ParserConfigurationException {
|
public Document getSeriesRecord(TheTVDBSearchResult searchResult, Locale language) throws Exception {
|
||||||
|
|
||||||
URL seriesRecordUrl = new URL(getMirror(MirrorType.ZIP) + "/api/" + apikey + "/series/" + searchResult.getSeriesId() + "/all/" + language.getLanguage() + ".zip");
|
URL seriesRecord = new URL(getMirror(MirrorType.ZIP) + "/api/" + apikey + "/series/" + searchResult.getSeriesId() + "/all/" + language.getLanguage() + ".zip");
|
||||||
|
|
||||||
ZipInputStream zipInputStream = new ZipInputStream(seriesRecordUrl.openStream());
|
ZipInputStream zipInputStream = new ZipInputStream(seriesRecord.openStream());
|
||||||
ZipEntry zipEntry;
|
ZipEntry zipEntry;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -174,12 +167,12 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||||
if (seriesRecordName.equals(zipEntry.getName())) {
|
if (seriesRecordName.equals(zipEntry.getName())) {
|
||||||
return getDocument(zipInputStream);
|
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(zipInputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// zip file must contain the series record
|
// zip file must contain the series record
|
||||||
throw new FileNotFoundException(String.format("Archive must contain %s: %s", seriesRecordName, seriesRecordUrl));
|
throw new FileNotFoundException(String.format("Archive must contain %s: %s", seriesRecordName, seriesRecord));
|
||||||
} finally {
|
} finally {
|
||||||
zipInputStream.close();
|
zipInputStream.close();
|
||||||
}
|
}
|
||||||
|
@ -203,7 +196,7 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
if (seasonId == null) {
|
if (seasonId == null) {
|
||||||
// get episode xml from first episode of given season
|
// get episode xml from first episode of given season
|
||||||
Document dom = getDocument(new URL("http", host, "/api/" + apikey + "/series/" + seriesId + "/default/" + season + "/1/en.xml"));
|
Document dom = getXmlDocument("/api/" + apikey + "/series/" + seriesId + "/default/" + season + "/1/en.xml");
|
||||||
|
|
||||||
seasonId = Integer.valueOf(selectString("Data/Episode/seasonid", dom));
|
seasonId = Integer.valueOf(selectString("Data/Episode/seasonid", dom));
|
||||||
|
|
||||||
|
@ -220,13 +213,11 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String getMirror(MirrorType mirrorType) throws IOException, SAXException, ParserConfigurationException {
|
protected String getMirror(MirrorType mirrorType) throws Exception {
|
||||||
synchronized (mirrors) {
|
synchronized (mirrors) {
|
||||||
if (mirrors.isEmpty()) {
|
if (mirrors.isEmpty()) {
|
||||||
// initialize mirrors
|
// initialize mirrors
|
||||||
URL mirrorUrl = new URL("http", host, "/api/" + apikey + "/mirrors.xml");
|
Document dom = getXmlDocument("/api/" + apikey + "/mirrors.xml");
|
||||||
|
|
||||||
Document dom = getDocument(mirrorUrl);
|
|
||||||
|
|
||||||
// all mirrors by type
|
// all mirrors by type
|
||||||
Map<MirrorType, List<String>> mirrorListMap = new EnumMap<MirrorType, List<String>>(MirrorType.class);
|
Map<MirrorType, List<String>> mirrorListMap = new EnumMap<MirrorType, List<String>>(MirrorType.class);
|
||||||
|
@ -264,12 +255,18 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected Document getXmlDocument(String path) throws Exception {
|
||||||
|
URL url = new URL("http", host, path);
|
||||||
|
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class TheTVDBSearchResult extends SearchResult {
|
public static class TheTVDBSearchResult extends SearchResult {
|
||||||
|
|
||||||
private final int seriesId;
|
private final int seriesId;
|
||||||
|
|
||||||
|
|
||||||
public TheTVDBSearchResult(String seriesName, int seriesId) {
|
public TheTVDBSearchResult(String seriesName, int seriesId) {
|
||||||
super(seriesName);
|
super(seriesName);
|
||||||
this.seriesId = seriesId;
|
this.seriesId = seriesId;
|
||||||
|
@ -290,7 +287,7 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
private final int bitMask;
|
private final int bitMask;
|
||||||
|
|
||||||
|
|
||||||
private MirrorType(int bitMask) {
|
private MirrorType(int bitMask) {
|
||||||
this.bitMask = bitMask;
|
this.bitMask = bitMask;
|
||||||
}
|
}
|
||||||
|
@ -317,7 +314,7 @@ public class TheTVDBClient implements EpisodeListProvider {
|
||||||
|
|
||||||
private final Cache cache;
|
private final Cache cache;
|
||||||
|
|
||||||
|
|
||||||
public TheTVDBCache(Cache cache) {
|
public TheTVDBCache(Cache cache) {
|
||||||
this.cache = cache;
|
this.cache = cache;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue