1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

* simplify things

This commit is contained in:
Reinhard Pointner 2011-11-04 09:11:11 +00:00
parent dc079a9d13
commit f65f84c593
6 changed files with 30 additions and 105 deletions

View File

@ -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<SearchResult> 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<Node> nodes = selectNodes("//TABLE//A[following-sibling::SMALL[contains(.,'series')]]", dom);

View File

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

View File

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

View File

@ -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<SearchResult> 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<Node> nodes = selectNodes("Data/Series", dom);
List<SearchResult> searchResults = new ArrayList<SearchResult>(nodes.size());
Map<Integer, TheTVDBSearchResult> resultSet = new LinkedHashMap<Integer, TheTVDBSearchResult>();
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<SearchResult>(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);
}
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<Episode> episodes) {
cache.put(new Element(key(host, "EpisodeList", seriesId, language.getLanguage()), episodes));
}

View File

@ -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() {

View File

@ -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;
@ -35,6 +33,7 @@ public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
DONE
}
private URL url;
private long contentLength = -1;
@ -70,7 +69,7 @@ public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
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<ByteBuffer, Void> {
return requestHeaders;
}
protected ByteBuffer encodeParameters(Map<String, String> parameters) {
StringBuilder sb = new StringBuilder();
for (Entry<String, String> 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());
}
}