Albirew/tachiyomi
Archivé
1
0
Bifurcation 0

Use network cache

Cette révision appartient à :
inorichi 2016-02-05 20:18:39 +01:00
Parent eec2dcd981
révision 6435eeb251
5 fichiers modifiés avec 37 ajouts et 18 suppressions

Voir le fichier

@ -84,7 +84,7 @@ public class DownloadManager {
if (finished) {
DownloadService.stop(context);
}
}, e -> Timber.e(e.getCause(), e.getMessage()));
}, e -> DownloadService.stop(context));
if (!isRunning) {
isRunning = true;

Voir le fichier

@ -83,7 +83,7 @@ public class MyAnimeList extends MangaSyncService {
public Observable<Boolean> login(String username, String password) {
createHeaders(username, password);
return networkService.getResponse(getLoginUrl(), headers, null)
return networkService.getResponse(getLoginUrl(), headers, false)
.map(response -> response.code() == 200);
}
@ -101,7 +101,7 @@ public class MyAnimeList extends MangaSyncService {
}
public Observable<List<MangaSync>> search(String query) {
return networkService.getStringResponse(getSearchUrl(query), headers, null)
return networkService.getStringResponse(getSearchUrl(query), headers, true)
.map(Jsoup::parse)
.flatMap(doc -> Observable.from(doc.select("entry")))
.filter(entry -> !entry.select("type").text().equals("Novel"))
@ -126,7 +126,7 @@ public class MyAnimeList extends MangaSyncService {
public Observable<List<MangaSync>> getList() {
// TODO cache this list for a few minutes
return networkService.getStringResponse(getListUrl(username), headers, null)
return networkService.getStringResponse(getListUrl(username), headers, true)
.map(Jsoup::parse)
.flatMap(doc -> Observable.from(doc.select("manga")))
.map(entry -> {

Voir le fichier

@ -7,11 +7,13 @@ import java.io.File;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@ -22,12 +24,23 @@ import rx.Observable;
public final class NetworkHelper {
private OkHttpClient client;
private OkHttpClient forceCacheClient;
private CookieManager cookieManager;
public final CacheControl NULL_CACHE_CONTROL = new CacheControl.Builder().noCache().build();
public final Headers NULL_HEADERS = new Headers.Builder().build();
public final RequestBody NULL_REQUEST_BODY = new FormBody.Builder().build();
public final CacheControl CACHE_CONTROL = new CacheControl.Builder()
.maxAge(10, TimeUnit.MINUTES)
.build();
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = chain -> {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "max-age=" + 600)
.build();
};
private static final int CACHE_SIZE = 5 * 1024 * 1024; // 5 MiB
private static final String CACHE_DIR_NAME = "network_cache";
@ -42,18 +55,24 @@ public final class NetworkHelper {
.cookieJar(new JavaNetCookieJar(cookieManager))
.cache(new Cache(cacheDir, CACHE_SIZE))
.build();
forceCacheClient = client.newBuilder()
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.build();
}
public Observable<Response> getResponse(final String url, final Headers headers, final CacheControl cacheControl) {
public Observable<Response> getResponse(final String url, final Headers headers, boolean forceCache) {
return Observable.defer(() -> {
try {
OkHttpClient c = forceCache ? forceCacheClient : client;
Request request = new Request.Builder()
.url(url)
.cacheControl(cacheControl != null ? cacheControl : NULL_CACHE_CONTROL)
.headers(headers != null ? headers : NULL_HEADERS)
.cacheControl(CACHE_CONTROL)
.build();
return Observable.just(client.newCall(request).execute());
return Observable.just(c.newCall(request).execute());
} catch (Throwable e) {
return Observable.error(e);
}
@ -70,8 +89,8 @@ public final class NetworkHelper {
});
}
public Observable<String> getStringResponse(final String url, final Headers headers, final CacheControl cacheControl) {
return getResponse(url, headers, cacheControl)
public Observable<String> getStringResponse(final String url, final Headers headers, boolean forceCache) {
return getResponse(url, headers, forceCache)
.flatMap(this::mapResponseToString);
}
@ -95,7 +114,7 @@ public final class NetworkHelper {
try {
Request request = new Request.Builder()
.url(url)
.cacheControl(NULL_CACHE_CONTROL)
.cacheControl(CacheControl.FORCE_NETWORK)
.headers(headers != null ? headers : NULL_HEADERS)
.build();

Voir le fichier

@ -53,7 +53,7 @@ public abstract class Source extends BaseSource {
page.url = getInitialPopularMangasUrl();
return networkService
.getStringResponse(page.url, requestHeaders, null)
.getStringResponse(page.url, requestHeaders, true)
.map(Jsoup::parse)
.doOnNext(doc -> page.mangas = parsePopularMangasFromHtml(doc))
.doOnNext(doc -> page.nextPageUrl = parseNextPopularMangasUrl(doc, page))
@ -66,7 +66,7 @@ public abstract class Source extends BaseSource {
page.url = getInitialSearchUrl(query);
return networkService
.getStringResponse(page.url, requestHeaders, null)
.getStringResponse(page.url, requestHeaders, true)
.map(Jsoup::parse)
.doOnNext(doc -> page.mangas = parseSearchFromHtml(doc))
.doOnNext(doc -> page.nextPageUrl = parseNextSearchUrl(doc, page, query))
@ -76,14 +76,14 @@ public abstract class Source extends BaseSource {
// Get manga details from the source
public Observable<Manga> pullMangaFromNetwork(final String mangaUrl) {
return networkService
.getStringResponse(getBaseUrl() + overrideMangaUrl(mangaUrl), requestHeaders, null)
.getStringResponse(getBaseUrl() + overrideMangaUrl(mangaUrl), requestHeaders, true)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToManga(mangaUrl, unparsedHtml)));
}
// Get chapter list of a manga from the source
public Observable<List<Chapter>> pullChaptersFromNetwork(final String mangaUrl) {
return networkService
.getStringResponse(getBaseUrl() + mangaUrl, requestHeaders, null)
.getStringResponse(getBaseUrl() + mangaUrl, requestHeaders, false)
.flatMap(unparsedHtml -> {
List<Chapter> chapters = parseHtmlToChapters(unparsedHtml);
return !chapters.isEmpty() ?
@ -102,7 +102,7 @@ public abstract class Source extends BaseSource {
public Observable<List<Page>> pullPageListFromNetwork(final String chapterUrl) {
return networkService
.getStringResponse(getBaseUrl() + overrideChapterUrl(chapterUrl), requestHeaders, null)
.getStringResponse(getBaseUrl() + overrideChapterUrl(chapterUrl), requestHeaders, false)
.flatMap(unparsedHtml -> {
List<Page> pages = convertToPages(parseHtmlToPageUrls(unparsedHtml));
return !pages.isEmpty() ?
@ -127,7 +127,7 @@ public abstract class Source extends BaseSource {
public Observable<Page> getImageUrlFromPage(final Page page) {
page.setStatus(Page.LOAD_PAGE);
return networkService
.getStringResponse(overridePageUrl(page.getUrl()), requestHeaders, null)
.getStringResponse(overridePageUrl(page.getUrl()), requestHeaders, false)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml)))
.onErrorResumeNext(e -> {
page.setStatus(Page.ERROR);

Voir le fichier

@ -309,7 +309,7 @@ public class Batoto extends LoginSource {
@Override
public Observable<Boolean> login(String username, String password) {
return networkService.getStringResponse(LOGIN_URL, requestHeaders, null)
return networkService.getStringResponse(LOGIN_URL, requestHeaders, false)
.flatMap(response -> doLogin(response, username, password))
.map(this::isAuthenticationSuccessful);
}