+ updated Serienjunkies support (support episode airdate and website links)

This commit is contained in:
Reinhard Pointner 2011-08-07 07:22:07 +00:00
parent 40c64041a1
commit 38bbaaf5d8
3 changed files with 69 additions and 42 deletions

View File

@ -10,7 +10,6 @@ import java.io.Reader;
import java.io.Serializable; import java.io.Serializable;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -51,7 +50,25 @@ public class SerienjunkiesClient implements EpisodeListProvider {
@Override @Override
public List<SearchResult> search(String query) throws Exception { public String getName() {
return "Serienjunkies";
}
@Override
public Icon getIcon() {
return ResourceManager.getIcon("search.serienjunkies");
}
@Override
public boolean hasSingleSeasonSupport() {
return true;
}
@Override
public List<SearchResult> search(String query) throws IOException {
// normalize // normalize
query = query.toLowerCase(); query = query.toLowerCase();
@ -103,7 +120,7 @@ public class SerienjunkiesClient implements EpisodeListProvider {
} }
protected List<SerienjunkiesSearchResult> getSeriesTitles() throws Exception { protected List<SerienjunkiesSearchResult> getSeriesTitles() throws IOException {
// try cache first // try cache first
List<SerienjunkiesSearchResult> seriesList = cache.getSeriesList(); List<SerienjunkiesSearchResult> seriesList = cache.getSeriesList();
if (seriesList != null) if (seriesList != null)
@ -118,11 +135,12 @@ public class SerienjunkiesClient implements EpisodeListProvider {
for (Object element : list) { for (Object element : list) {
JSONObject obj = (JSONObject) element; JSONObject obj = (JSONObject) element;
String sid = (String) obj.get("id"); Integer sid = new Integer((String) obj.get("id"));
String link = (String) obj.get("link");
String mainTitle = (String) obj.get("short"); String mainTitle = (String) obj.get("short");
String germanTitle = (String) obj.get("short_german"); String germanTitle = (String) obj.get("short_german");
seriesList.add(new SerienjunkiesSearchResult(Integer.parseInt(sid), mainTitle, germanTitle != null && germanTitle.length() > 0 ? germanTitle : null)); seriesList.add(new SerienjunkiesSearchResult(sid, link, mainTitle, germanTitle != null && germanTitle.length() > 0 ? germanTitle : null));
} }
// populate cache // populate cache
@ -133,7 +151,7 @@ public class SerienjunkiesClient implements EpisodeListProvider {
@Override @Override
public List<Episode> getEpisodeList(SearchResult searchResult) throws Exception { public List<Episode> getEpisodeList(SearchResult searchResult) throws IOException {
SerienjunkiesSearchResult series = (SerienjunkiesSearchResult) searchResult; SerienjunkiesSearchResult series = (SerienjunkiesSearchResult) searchResult;
// try cache first // try cache first
@ -150,11 +168,12 @@ public class SerienjunkiesClient implements EpisodeListProvider {
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
JSONObject obj = (JSONObject) list.get(i); JSONObject obj = (JSONObject) list.get(i);
String season = (String) obj.get("season"); Integer season = new Integer((String) obj.get("season"));
String episode = (String) obj.get("episode"); Integer episode = new Integer((String) obj.get("episode"));
String title = (String) obj.get("german"); String title = (String) obj.get("german");
Date airdate = Date.parse((String) ((JSONObject) obj.get("airdates")).get("premiere"), "yyyy-MM-dd");
episodes.add(new Episode(series.getName(), new Integer(season), new Integer(episode), title, i + 1, null, null)); episodes.add(new Episode(series.getName(), season, episode, title, i + 1, null, airdate));
} }
// populate cache // populate cache
@ -167,7 +186,20 @@ public class SerienjunkiesClient implements EpisodeListProvider {
} }
private Object request(String resource) throws IOException, GeneralSecurityException { @Override
public List<Episode> getEpisodeList(SearchResult searchResult, int season) throws IOException {
List<Episode> all = getEpisodeList(searchResult);
List<Episode> eps = filterBySeason(all, season);
if (eps.isEmpty()) {
throw new SeasonOutOfBoundsException(searchResult.getName(), season, getLastSeason(all));
}
return eps;
}
protected Object request(String resource) throws IOException {
URL url = new URL("https", host, resource); URL url = new URL("https", host, resource);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
@ -184,45 +216,27 @@ public class SerienjunkiesClient implements EpisodeListProvider {
} }
@Override
public List<Episode> getEpisodeList(SearchResult searchResult, int season) throws Exception {
return null;
}
@Override @Override
public URI getEpisodeListLink(SearchResult searchResult) { public URI getEpisodeListLink(SearchResult searchResult) {
return null; return getEpisodeListLink(searchResult, "alle-serien-staffeln");
} }
@Override @Override
public URI getEpisodeListLink(SearchResult searchResult, int season) { public URI getEpisodeListLink(SearchResult searchResult, int season) {
return null; return getEpisodeListLink(searchResult, "season" + season);
} }
@Override public URI getEpisodeListLink(SearchResult searchResult, String page) {
public String getName() { return URI.create(String.format("http://www.serienjunkies.de/%s/%s.html", ((SerienjunkiesSearchResult) searchResult).getLink(), page));
return "Serienjunkies";
}
@Override
public Icon getIcon() {
return ResourceManager.getIcon("search.serienjunkies");
}
@Override
public boolean hasSingleSeasonSupport() {
return false;
} }
public static class SerienjunkiesSearchResult extends SearchResult implements Serializable { public static class SerienjunkiesSearchResult extends SearchResult implements Serializable {
protected int sid; protected int sid;
protected String link;
protected String mainTitle; protected String mainTitle;
protected String germanTitle; protected String germanTitle;
@ -232,21 +246,27 @@ public class SerienjunkiesClient implements EpisodeListProvider {
} }
public SerienjunkiesSearchResult(int sid, String mainTitle, String germanTitle) { public SerienjunkiesSearchResult(int sid, String link, String mainTitle, String germanTitle) {
this.sid = sid; this.sid = sid;
this.link = link;
this.mainTitle = mainTitle; this.mainTitle = mainTitle;
this.germanTitle = germanTitle; this.germanTitle = germanTitle;
} }
@Override
public String getName() {
return germanTitle != null ? germanTitle : mainTitle; // prefer german title
}
public int getSeriesId() { public int getSeriesId() {
return sid; return sid;
} }
@Override public String getLink() {
public String getName() { return link;
return germanTitle != null ? germanTitle : mainTitle; // prefer german title
} }

View File

@ -226,7 +226,7 @@ public final class WebRequest {
} }
public static SSLSocketFactory createIgnoreCertificateSocketFactory() throws GeneralSecurityException { public static SSLSocketFactory createIgnoreCertificateSocketFactory() {
// create a trust manager that does not validate certificate chains // create a trust manager that does not validate certificate chains
TrustManager trustAnyCertificate = new X509TrustManager() { TrustManager trustAnyCertificate = new X509TrustManager() {
@ -243,9 +243,13 @@ public final class WebRequest {
} }
}; };
try {
SSLContext sc = SSLContext.getInstance("SSL"); SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { trustAnyCertificate }, new SecureRandom()); sc.init(null, new TrustManager[] { trustAnyCertificate }, new SecureRandom());
return sc.getSocketFactory(); return sc.getSocketFactory();
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
} }

View File

@ -26,6 +26,7 @@ public class SerienjunkiesClientTest {
SerienjunkiesSearchResult first = (SerienjunkiesSearchResult) results.get(0); SerienjunkiesSearchResult first = (SerienjunkiesSearchResult) results.get(0);
assertEquals(34, first.getSeriesId()); assertEquals(34, first.getSeriesId());
assertEquals("Alias", first.getLink());
assertEquals("Alias - Die Agentin", first.getName()); assertEquals("Alias - Die Agentin", first.getName());
assertEquals("Alias", first.getMainTitle()); assertEquals("Alias", first.getMainTitle());
assertEquals("Alias - Die Agentin", first.getGermanTitle()); assertEquals("Alias - Die Agentin", first.getGermanTitle());
@ -34,7 +35,7 @@ public class SerienjunkiesClientTest {
@Test @Test
public void getEpisodeListAll() throws Exception { public void getEpisodeListAll() throws Exception {
List<Episode> list = serienjunkies.getEpisodeList(new SerienjunkiesSearchResult(260, "Grey's Anatomy", null)); List<Episode> list = serienjunkies.getEpisodeList(new SerienjunkiesSearchResult(260, "greys-anatomy", "Grey's Anatomy", null));
// check ordinary episode // check ordinary episode
Episode eps = list.get(0); Episode eps = list.get(0);
@ -43,6 +44,7 @@ public class SerienjunkiesClientTest {
assertEquals("1", eps.getEpisode().toString()); assertEquals("1", eps.getEpisode().toString());
assertEquals("1", eps.getSeason().toString()); assertEquals("1", eps.getSeason().toString());
assertEquals("1", eps.getAbsolute().toString()); assertEquals("1", eps.getAbsolute().toString());
assertEquals("2005-03-27", eps.airdate().toString());
// check umlaut in title // check umlaut in title
eps = list.get(2); eps = list.get(2);
@ -50,6 +52,7 @@ public class SerienjunkiesClientTest {
assertEquals("1", eps.getSeason().toString()); assertEquals("1", eps.getSeason().toString());
assertEquals("3", eps.getEpisode().toString()); assertEquals("3", eps.getEpisode().toString());
assertEquals("3", eps.getAbsolute().toString()); assertEquals("3", eps.getAbsolute().toString());
assertEquals("2005-04-10", eps.airdate().toString());
} }