* code cleanup

This commit is contained in:
Reinhard Pointner 2011-08-05 06:37:30 +00:00
parent d499bb01d6
commit 40c64041a1
2 changed files with 34 additions and 38 deletions

View File

@ -133,7 +133,7 @@
/> />
<!-- <!--
Very long-lived cache (one month!) for AniDB and Serienjunkies anime list and episode information. Very long-lived cache (one month!) anime/series list and episode information.
--> -->
<cache name="web-persistent-datasource" <cache name="web-persistent-datasource"
maxElementsInMemory="20" maxElementsInMemory="20"

View File

@ -110,29 +110,24 @@ public class SerienjunkiesClient implements EpisodeListProvider {
return seriesList; return seriesList;
// fetch series data // fetch series data
Reader reader = getReader(createConnection("allseries.php?d=" + apikey));
seriesList = new ArrayList<SerienjunkiesSearchResult>(); seriesList = new ArrayList<SerienjunkiesSearchResult>();
try { JSONObject data = (JSONObject) request("allseries.php?d=" + apikey);
JSONObject data = (JSONObject) JSONValue.parse(reader); JSONArray list = (JSONArray) data.get("allseries");
JSONArray list = (JSONArray) data.get("allseries");
for (Object element : list) {
JSONObject obj = (JSONObject) element;
for (Object element : list) { String sid = (String) obj.get("id");
JSONObject obj = (JSONObject) element; String mainTitle = (String) obj.get("short");
String germanTitle = (String) obj.get("short_german");
String sid = (String) obj.get("id");
String mainTitle = (String) obj.get("short");
String germanTitle = (String) obj.get("short_german");
seriesList.add(new SerienjunkiesSearchResult(Integer.parseInt(sid), mainTitle, germanTitle != null && germanTitle.length() > 0 ? germanTitle : null));
}
// populate cache seriesList.add(new SerienjunkiesSearchResult(Integer.parseInt(sid), mainTitle, germanTitle != null && germanTitle.length() > 0 ? germanTitle : null));
cache.putSeriesList(seriesList);
} finally {
reader.close();
} }
// populate cache
cache.putSeriesList(seriesList);
return seriesList; return seriesList;
} }
@ -146,30 +141,25 @@ public class SerienjunkiesClient implements EpisodeListProvider {
if (episodes != null) if (episodes != null)
return episodes; return episodes;
// fetch series data // fetch episode data
Reader reader = getReader(createConnection("allepisodes.php?d=" + apikey + "&q=" + series.getSeriesId()));
episodes = new ArrayList<Episode>(25); episodes = new ArrayList<Episode>(25);
try { JSONObject data = (JSONObject) request("allepisodes.php?d=" + apikey + "&q=" + series.getSeriesId());
JSONObject data = (JSONObject) JSONValue.parse(reader); JSONArray list = (JSONArray) data.get("allepisodes");
JSONArray list = (JSONArray) data.get("allepisodes");
for (int i = 0; i < list.size(); i++) {
JSONObject obj = (JSONObject) list.get(i);
for (int i = 0; i < list.size(); i++) { String season = (String) obj.get("season");
JSONObject obj = (JSONObject) list.get(i); String episode = (String) obj.get("episode");
String title = (String) obj.get("german");
String season = (String) obj.get("season");
String episode = (String) obj.get("episode");
String title = (String) obj.get("german");
episodes.add(new Episode(series.getName(), new Integer(season), new Integer(episode), title, i + 1, null, null));
}
// populate cache episodes.add(new Episode(series.getName(), new Integer(season), new Integer(episode), title, i + 1, null, null));
cache.putEpisodeList(episodes, series.getSeriesId());
} finally {
reader.close();
} }
// populate cache
cache.putEpisodeList(episodes, series.getSeriesId());
// make sure episodes are in ordered correctly // make sure episodes are in ordered correctly
sortEpisodes(episodes); sortEpisodes(episodes);
@ -177,14 +167,20 @@ public class SerienjunkiesClient implements EpisodeListProvider {
} }
private HttpsURLConnection createConnection(String resource) throws IOException, GeneralSecurityException { private Object request(String resource) throws IOException, GeneralSecurityException {
URL url = new URL("https", host, resource); URL url = new URL("https", host, resource);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
// disable SSL certificate validation // disable SSL certificate validation
connection.setSSLSocketFactory(createIgnoreCertificateSocketFactory()); connection.setSSLSocketFactory(createIgnoreCertificateSocketFactory());
return connection; // fetch and parse json data
Reader reader = getReader(connection);
try {
return JSONValue.parse(reader);
} finally {
reader.close();
}
} }