From 2ce2f0ee9b6d0a858bb70cfbb9b377185edbd28a Mon Sep 17 00:00:00 2001 From: tarsin Date: Fri, 28 Apr 2023 09:51:08 +0800 Subject: [PATCH] Cleanup EhEngine --- .../com/hippo/ehviewer/client/EhEngine.kt | 26 ++++++------------- .../ehviewer/client/parser/ArchiveParser.kt | 4 +-- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt index 814c729dd..64b5e3ea1 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt @@ -16,6 +16,7 @@ package com.hippo.ehviewer.client import android.util.Log +import arrow.fx.coroutines.parZip import com.hippo.ehviewer.AppConfig import com.hippo.ehviewer.EhApplication import com.hippo.ehviewer.GetText @@ -48,8 +49,6 @@ import com.hippo.ehviewer.client.parser.VoteTagParser import com.hippo.ehviewer.dailycheck.showEventNotification import com.hippo.ehviewer.dailycheck.today import com.hippo.network.StatusCodeException -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import moe.tarsin.coroutines.runSuspendCatching import okhttp3.Headers @@ -409,9 +408,8 @@ object EhEngine { gid: Long, token: String?, ): ArchiveParser.Result { - val referer = EhUrl.getGalleryDetailUrl(gid, token) - return ehRequest(url, referer).executeAndParsingWith(ArchiveParser::parse)!! - .apply { funds = funds ?: getFunds() } + return ehRequest(url, EhUrl.getGalleryDetailUrl(gid, token)).executeAndParsingWith(ArchiveParser::parse) + .apply { funds = funds ?: ehRequest(EhUrl.URL_FUNDS).executeAndParsingWith(HomeParser::parseFunds) } } suspend fun downloadArchive( @@ -458,19 +456,11 @@ object EhEngine { return null } - private suspend fun getFunds(): HomeParser.Funds { - return ehRequest(EhUrl.URL_FUNDS).executeAndParsingWith(HomeParser::parseFunds) - } - - private suspend fun getImageLimitsInternal(): HomeParser.Limits { - return ehRequest(EhUrl.URL_HOME).executeAndParsingWith(HomeParser::parse) - } - - suspend fun getImageLimits(): HomeParser.Result = coroutineScope { - val limitsDeferred = async { getImageLimitsInternal() } - val fundsDeferred = async { getFunds() } - HomeParser.Result(limitsDeferred.await(), fundsDeferred.await()) - } + suspend fun getImageLimits() = parZip( + { ehRequest(EhUrl.URL_HOME).executeAndParsingWith(HomeParser::parse) }, + { ehRequest(EhUrl.URL_FUNDS).executeAndParsingWith(HomeParser::parseFunds) }, + { limits, funds -> HomeParser.Result(limits, funds) }, + ) suspend fun resetImageLimits(): HomeParser.Limits? { return ehRequest(EhUrl.URL_HOME) { diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt index 5cd635652..f78b228b1 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt @@ -30,8 +30,8 @@ object ArchiveParser { private val PATTERN_NEED_HATH_CLIENT = Regex("You must have a H@H client assigned to your account to use this feature\\.") - fun parse(body: String): Result? { - val m = PATTERN_HATH_FORM.find(body) ?: return null + fun parse(body: String): Result { + val m = PATTERN_HATH_FORM.find(body)!! val paramOr = m.groupValues[1] val archiveList = ArrayList() Jsoup.parse(body).select("#db>div>div").forEach { element ->