Tweak original image download

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-14 18:18:45 +08:00
1 parent e9a1bf22b1
commit c867a74aea
8 files changed
+52 -7

No files matched your search

@@ -25,6 +25,7 @@ import androidx.lifecycle.coroutineScope
import coil.ImageLoaderFactory
import coil.decode.ImageDecoderDecoder
import coil.util.DebugLogger
import com.google.net.cronet.okhttptransport.RedirectStrategy.withoutRedirects
import com.hippo.ehviewer.client.EhCookieStore
import com.hippo.ehviewer.client.EhTagDatabase
import com.hippo.ehviewer.client.data.GalleryDetail
@@ -154,7 +155,7 @@ class EhApplication : Application(), ImageLoaderFactory {
}
companion object {
val nonCacheOkHttpClient by lazy {
private val baseOkHttpClient by lazy {
httpClient {
cookieJar(EhCookieStore)
if (isAtLeastQ) {
@@ -164,6 +165,11 @@ class EhApplication : Application(), ImageLoaderFactory {
if (Settings.bypassCloudflare) {
addInterceptor(CloudflareInterceptor(appCtx))
}
}
}
val nonCacheOkHttpClient by lazy {
httpClient(baseOkHttpClient) {
// TODO: Rewrite CronetInterceptor to use android.net.http.HttpEngine and make it Android 14 only when released
if (isCronetSupported) {
addInterceptor(EhCookieStore)
@@ -172,6 +178,18 @@ class EhApplication : Application(), ImageLoaderFactory {
}
}
val noRedirectOkHttpClient by lazy {
httpClient(baseOkHttpClient) {
followRedirects(false)
if (isCronetSupported) {
addInterceptor(EhCookieStore)
cronet(cronetHttpClient) {
setRedirectStrategy(withoutRedirects())
}
}
}
}
// Never use this okhttp client to download large blobs!!!
val okHttpClient by lazy {
httpClient(nonCacheOkHttpClient) {
@@ -22,6 +22,8 @@ import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.InsufficientFundsException
import com.hippo.ehviewer.client.exception.NotLoggedInException
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.client.parser.ArchiveParser
import com.hippo.ehviewer.client.parser.EventPaneParser
@@ -134,6 +136,17 @@ private suspend inline fun <T> Request.executeAndParsingWith(block: suspend Stri
}
object EhEngine {
suspend fun getOriginalImageUrl(url: String, referer: String?): String {
Log.d(TAG, url)
return ehRequest(url, referer).executeNoRedirect {
header("Location")?.apply {
if (contains("bounce_login")) {
throw NotLoggedInException()
}
} ?: throw InsufficientFundsException()
}
}
suspend fun getTorrentList(url: String, gid: Long, token: String?): TorrentResult {
val referer = EhUrl.getGalleryDetailUrl(gid, token)
return ehRequest(url, referer).executeAndParsingWith(TorrentParser::parse)
@@ -16,6 +16,7 @@
package com.hippo.ehviewer.client
import android.util.Log
import com.hippo.ehviewer.EhApplication.Companion.noRedirectOkHttpClient
import com.hippo.ehviewer.EhApplication.Companion.nonCacheOkHttpClient
import com.hippo.ehviewer.EhApplication.Companion.okHttpClient
import kotlinx.coroutines.coroutineScope
@@ -105,6 +106,7 @@ suspend inline fun <R> Call.usingCancellable(crossinline block: suspend Response
suspend inline fun <R> Request.execute(block: Response.() -> R) = okHttpClient.newCall(this).executeAsync().use(block)
suspend inline fun <R> Request.executeNonCache(crossinline block: suspend Response.() -> R) = nonCacheOkHttpClient.newCall(this).usingCancellable(block)
suspend inline fun <R> Request.executeNoRedirect(block: Response.() -> R) = noRedirectOkHttpClient.newCall(this).executeAsync().use(block)
suspend inline fun <reified T> Request.executeAndParseAs() = execute { parseAs<T>() }
@@ -0,0 +1,5 @@
package com.hippo.ehviewer.client.exception
import com.hippo.ehviewer.R
class NotLoggedInException : EhException(R.string.sign_in_first)
@@ -16,7 +16,7 @@
package com.hippo.ehviewer.client.parser
import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.NotLoggedInException
import com.hippo.ehviewer.util.ExceptionUtils
import org.jsoup.Jsoup
@@ -29,7 +29,7 @@ object ForumsParser {
child.attr("href")
}.getOrElse {
ExceptionUtils.throwIfFatal(it)
throw EhException("Not logged in")
throw NotLoggedInException()
}
}
}
@@ -29,6 +29,7 @@ import com.hippo.ehviewer.client.EhUrl.getGalleryMultiPageViewerUrl
import com.hippo.ehviewer.client.EhUrl.referer
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.ehRequest
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.client.execute
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePages
@@ -722,8 +723,14 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
val referer: String?
if ((Settings.downloadOriginImage || orgImg) && !originImageUrl.isNullOrBlank()) {
targetImageUrl = originImageUrl
referer = EhUrl.getPageUrl(mSpiderInfo.gid, index, pToken)
val pageUrl = EhUrl.getPageUrl(mSpiderInfo.gid, index, pToken)
targetImageUrl = runSuspendCatching {
EhEngine.getOriginalImageUrl(originImageUrl!!, pageUrl)
}.getOrElse {
error = ExceptionUtils.getReadableString(it)
if (it is EhException) break else continue
}
referer = EhUrl.referer
} else {
targetImageUrl = imageUrl
referer = null
+1 -1
View File
@@ -346,7 +346,7 @@
<string name="settings_download_preload_image">预载图片</string>
<string name="settings_download_preload_image_summary">向后预载 %s 张图片</string>
<string name="settings_download_download_origin_image">下载原图</string>
<string name="settings_download_download_origin_image_summary">这很危险!勾选此项会导致下载配额迅速流失</string>
<string name="settings_download_download_origin_image_summary">警告!可能需要 GP</string>
<string name="settings_download_media_scan">允许媒体扫描</string>
<string name="settings_download_media_scan_summary_on">请避免他人翻看你的图库应用</string>
<string name="settings_download_media_scan_summary_off">大多数图库应用将不会显示下载目录中的图片</string>
+1 -1
View File
@@ -393,7 +393,7 @@
<string name="settings_download_preload_image">Preload image</string>
<string name="settings_download_preload_image_summary">Preload next %s image</string>
<string name="settings_download_download_origin_image">Download original image</string>
<string name="settings_download_download_origin_image_summary">It is dangerous! You may get 509 error</string>
<string name="settings_download_download_origin_image_summary">Caution! May require GP</string>
<string name="settings_download_restore_download_items">Restore download items</string>
<string name="settings_download_restore_download_items_summary">Restore all download items in download location</string>
<string name="settings_download_restore_not_found">Not found download items to restore</string>