diff --git a/source/net/sourceforge/filebot/web/IMDbClient.java b/source/net/sourceforge/filebot/web/IMDbClient.java index b167a91a..afbac5ee 100644 --- a/source/net/sourceforge/filebot/web/IMDbClient.java +++ b/source/net/sourceforge/filebot/web/IMDbClient.java @@ -10,7 +10,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -46,7 +45,7 @@ public class IMDbClient extends AbstractEpisodeListProvider { @Override public List search(String query, Locale locale) throws IOException, SAXException { - URL searchUrl = new URL("http", host, "/find?s=tt&q=" + URLEncoder.encode(query, "UTF-8")); + URL searchUrl = new URL("http", host, "/find?s=tt&q=" + encode(query)); Document dom = getHtmlDocument(openConnection(searchUrl)); List nodes = selectNodes("//TABLE//A[following-sibling::SMALL[contains(.,'series')]]", dom); diff --git a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java index 5b0ed8db..d32ce534 100644 --- a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java +++ b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java @@ -10,7 +10,6 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.URLConnection; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -56,7 +55,7 @@ public class SubsceneSubtitleClient implements SubtitleProvider { @Override public List search(String query) throws IOException, SAXException { - URL searchUrl = new URL("http", host, "/filmsearch.aspx?q=" + URLEncoder.encode(query, "UTF-8")); + URL searchUrl = new URL("http", host, "/filmsearch.aspx?q=" + encode(query)); Document dom = getHtmlDocument(searchUrl); diff --git a/source/net/sourceforge/filebot/web/TVRageClient.java b/source/net/sourceforge/filebot/web/TVRageClient.java index 067d1b9d..5621db74 100644 --- a/source/net/sourceforge/filebot/web/TVRageClient.java +++ b/source/net/sourceforge/filebot/web/TVRageClient.java @@ -9,7 +9,6 @@ import static net.sourceforge.tuned.XPathUtilities.*; import java.io.IOException; import java.net.URI; import java.net.URL; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -43,7 +42,7 @@ public class TVRageClient extends AbstractEpisodeListProvider { @Override public List search(String query, Locale locale) throws IOException, SAXException { - URL searchUrl = new URL("http", host, "/feeds/full_search.php?show=" + URLEncoder.encode(query, "UTF-8")); + URL searchUrl = new URL("http", host, "/feeds/full_search.php?show=" + encode(query)); Document dom = getDocument(searchUrl); diff --git a/source/net/sourceforge/filebot/web/TheTVDBClient.java b/source/net/sourceforge/filebot/web/TheTVDBClient.java index b58ec217..5f39c2b9 100644 --- a/source/net/sourceforge/filebot/web/TheTVDBClient.java +++ b/source/net/sourceforge/filebot/web/TheTVDBClient.java @@ -9,11 +9,11 @@ import static net.sourceforge.tuned.XPathUtilities.*; import java.io.FileNotFoundException; import java.net.URI; import java.net.URL; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.EnumMap; import java.util.EnumSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -80,28 +80,23 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { @Override public List search(String query, Locale language) throws Exception { - // check if the exact series name is already cached - Integer cachedResult = cache.getSeriesId(query, language); - - if (cachedResult != null) - return Arrays.asList(new SearchResult[] { new TheTVDBSearchResult(query, cachedResult) }); - // perform online search - URL url = getResource(null, "/api/GetSeries.php?seriesname=" + URLEncoder.encode(query, "UTF-8") + "&language=" + language.getLanguage()); + URL url = getResource(null, "/api/GetSeries.php?seriesname=" + encode(query) + "&language=" + language.getLanguage()); Document dom = getDocument(url); List nodes = selectNodes("Data/Series", dom); - List searchResults = new ArrayList(nodes.size()); + Map resultSet = new LinkedHashMap(); for (Node node : nodes) { - int seriesId = getIntegerContent("seriesid", node); + int sid = getIntegerContent("seriesid", node); String seriesName = getTextContent("SeriesName", node); - searchResults.add(new TheTVDBSearchResult(seriesName, seriesId)); - cache.putSeriesId(seriesName, language, seriesId); + if (!resultSet.containsKey(sid)) { + resultSet.put(sid, new TheTVDBSearchResult(seriesName, sid)); + } } - return searchResults; + return new ArrayList(resultSet.values()); } @@ -157,16 +152,6 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { // handle as normal episode episodes.add(new Episode(seriesName, seriesStartDate, seasonNumber, episodeNumber, episodeName, absoluteNumber, null, airdate)); } - - if (episodeNumber == 1) { - try { - // cache seasonId for each season (always when we are at the first episode) - // because it might be required by getEpisodeListLink - cache.putSeasonId(series.getSeriesId(), seasonNumber, getIntegerContent("seasonid", node)); - } catch (NumberFormatException e) { - // season/episode is not a number, just ignore - } - } } // episodes my not be ordered by DVD episode number @@ -216,16 +201,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { int seriesId = ((TheTVDBSearchResult) searchResult).getSeriesId(); try { - Integer seasonId = cache.getSeasonId(seriesId, season); - - if (seasonId == null) { - // get episode xml from first episode of given season - URL url = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + seriesId + "/default/" + season + "/1/en.xml"); - Document dom = getDocument(url); - - seasonId = Integer.valueOf(selectString("Data/Episode/seasonid", dom)); - cache.putSeasonId(seriesId, season, seasonId); - } + // 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)); return new URI("http://" + host + "/?tab=season&seriesid=" + seriesId + "&seasonid=" + seasonId); } catch (Exception e) { @@ -348,36 +326,6 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { } - public void putSeriesId(String seriesName, Locale language, int seriesId) { - cache.put(new Element(key(host, "SeriesId", seriesName, language.getLanguage()), seriesId)); - } - - - public Integer getSeriesId(String seriesName, Locale language) { - Element element = cache.get(key(host, "SeriesId", seriesName, language.getLanguage())); - - if (element != null) - return (Integer) element.getValue(); - - return null; - } - - - public void putSeasonId(int seriesId, int seasonNumber, int seasonId) { - cache.put(new Element(key(host, "SeasonId", seriesId, seasonNumber), seasonId)); - } - - - public Integer getSeasonId(int seriesId, int seasonNumber) { - Element element = cache.get(key(host, "SeasonId", seriesId, seasonNumber)); - - if (element != null) - return (Integer) element.getValue(); - - return null; - } - - public void putEpisodeList(int seriesId, Locale language, List episodes) { cache.put(new Element(key(host, "EpisodeList", seriesId, language.getLanguage()), episodes)); } diff --git a/source/net/sourceforge/filebot/web/WebRequest.java b/source/net/sourceforge/filebot/web/WebRequest.java index 94334561..00964871 100644 --- a/source/net/sourceforge/filebot/web/WebRequest.java +++ b/source/net/sourceforge/filebot/web/WebRequest.java @@ -223,19 +223,22 @@ public final class WebRequest { sb.append(entry.getKey()); sb.append("="); - - try { - sb.append(URLEncoder.encode(entry.getValue(), "UTF-8")); - } catch (UnsupportedEncodingException e) { - // will never happen - throw new RuntimeException(e); - } + sb.append(encode(entry.getValue())); } return sb.toString(); } + public static String encode(String string) { + try { + return URLEncoder.encode(string, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + public static SSLSocketFactory createIgnoreCertificateSocketFactory() { // create a trust manager that does not validate certificate chains TrustManager trustAnyCertificate = new X509TrustManager() { diff --git a/source/net/sourceforge/tuned/DownloadTask.java b/source/net/sourceforge/tuned/DownloadTask.java index 5d5b0601..733fa7ec 100644 --- a/source/net/sourceforge/tuned/DownloadTask.java +++ b/source/net/sourceforge/tuned/DownloadTask.java @@ -2,11 +2,11 @@ package net.sourceforge.tuned; +import static net.sourceforge.filebot.web.WebRequest.*; + import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLEncoder; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; @@ -16,8 +16,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.SwingWorker; @@ -27,7 +25,7 @@ public class DownloadTask extends SwingWorker { public static final String DOWNLOAD_STATE = "download state"; public static final String DOWNLOAD_PROGRESS = "download progress"; - + public static enum DownloadState { PENDING, CONNECTING, @@ -35,6 +33,7 @@ public class DownloadTask extends SwingWorker { DONE } + private URL url; private long contentLength = -1; @@ -44,7 +43,7 @@ public class DownloadTask extends SwingWorker { private Map requestHeaders; private Map> responseHeaders; - + public DownloadTask(URL url) { this.url = url; } @@ -70,7 +69,7 @@ public class DownloadTask extends SwingWorker { HttpURLConnection connection = createConnection(); if (postParameters != null) { - ByteBuffer postData = encodeParameters(postParameters); + ByteBuffer postData = Charset.forName("UTF-8").encode(encodeParameters(postParameters)); // add content type and content length headers connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -170,26 +169,4 @@ public class DownloadTask extends SwingWorker { return requestHeaders; } - - protected ByteBuffer encodeParameters(Map parameters) { - StringBuilder sb = new StringBuilder(); - - for (Entry entry : parameters.entrySet()) { - if (sb.length() > 0) - sb.append("&"); - - sb.append(entry.getKey()); - sb.append("="); - - try { - sb.append(URLEncoder.encode(entry.getValue(), "UTF-8")); - } catch (UnsupportedEncodingException e) { - // will never happen - Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.toString(), e); - } - } - - return Charset.forName("UTF-8").encode(sb.toString()); - } - }