* refactoring

This commit is contained in:
Reinhard Pointner 2009-07-13 12:41:19 +00:00
parent cdf2487f2c
commit b4f9a3ed24
1 changed files with 23 additions and 26 deletions

View File

@ -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 {
@ -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);
@ -265,6 +256,12 @@ 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;