Use CachedResource API

This commit is contained in:
Reinhard Pointner 2017-02-28 16:48:02 +08:00
parent 6996e4bf72
commit 45bcfbe3f6
1 changed files with 15 additions and 16 deletions

View File

@ -3,17 +3,16 @@ package net.filebot.web;
import static java.nio.charset.StandardCharsets.*; import static java.nio.charset.StandardCharsets.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
import static net.filebot.CachedResource.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
import static net.filebot.util.XPathUtilities.*; import static net.filebot.util.XPathUtilities.*;
import static net.filebot.web.EpisodeUtilities.*; import static net.filebot.web.EpisodeUtilities.*;
import static net.filebot.web.WebRequest.*;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.AbstractMap.SimpleImmutableEntry; import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList; import java.util.ArrayList;
@ -92,14 +91,8 @@ public class AnidbClient extends AbstractEpisodeListProvider {
@Override @Override
protected SeriesData fetchSeriesData(SearchResult anime, SortOrder sortOrder, Locale locale) throws Exception { protected SeriesData fetchSeriesData(SearchResult anime, SortOrder sortOrder, Locale locale) throws Exception {
// e.g. http://api.anidb.net:9001/httpapi?request=anime&client=filebot&clientver=1&protover=1&aid=4521
URL url = new URL("http://api.anidb.net:9001/httpapi?request=anime&client=" + client + "&clientver=" + clientver + "&protover=1&aid=" + anime.getId());
// respect flood protection limits
REQUEST_LIMIT.acquirePermit();
// get anime page as xml // get anime page as xml
Document dom = getDocument(url); Document dom = getXmlResource(anime.getId());
// check for errors (e.g. <error>Banned</error>) // check for errors (e.g. <error>Banned</error>)
String error = selectString("/error", dom); String error = selectString("/error", dom);
@ -176,19 +169,25 @@ public class AnidbClient extends AbstractEpisodeListProvider {
// sanity check // sanity check
if (episodes.isEmpty()) { if (episodes.isEmpty()) {
debug.fine(format("No episode data: %s (%d) => %s", anime, anime.getId(), url)); debug.fine(format("No episode data: %s (%d) => %s", anime, anime.getId(), getResource(anime.getId())));
} }
return new SeriesData(seriesInfo, episodes); return new SeriesData(seriesInfo, episodes);
} }
private Document getXmlResource(int aid) throws Exception {
Cache cache = Cache.getCache(getName(), CacheType.Monthly);
return cache.xml(aid, this::getResource).fetch(withPermit(fetchIfModified(), r -> REQUEST_LIMIT.acquirePermit())).expire(Cache.ONE_WEEK).get();
}
private URL getResource(int aid) throws Exception {
// e.g. http://api.anidb.net:9001/httpapi?request=anime&client=filebot&clientver=1&protover=1&aid=4521
return new URL("http://api.anidb.net:9001/httpapi?request=anime&client=" + client + "&clientver=" + clientver + "&protover=1&aid=" + aid);
}
@Override @Override
public URI getEpisodeListLink(SearchResult searchResult) { public URI getEpisodeListLink(SearchResult searchResult) {
try { return URI.create("http://anidb.net/a" + searchResult.getId());
return new URI("http://anidb.net/a" + searchResult.getId());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
} }
/** /**
@ -214,7 +213,7 @@ public class AnidbClient extends AbstractEpisodeListProvider {
/** /**
* This method is overridden in {@link net.filebot.WebServices.AnidbClientWithLocalSearch} to fetch the Anime Index from our own host and not anidb.net * This method is overridden in {@link net.filebot.WebServices.AnidbClientWithLocalSearch} to fetch the Anime Index from our own host and not anidb.net
*/ */
public synchronized SearchResult[] getAnimeTitles() throws Exception { public SearchResult[] getAnimeTitles() throws Exception {
// get data file (unzip and cache) // get data file (unzip and cache)
byte[] bytes = getCache("root").bytes("anime-titles.dat.gz", n -> new URL("http://anidb.net/api/" + n)).get(); byte[] bytes = getCache("root").bytes("anime-titles.dat.gz", n -> new URL("http://anidb.net/api/" + n)).get();