Tweak archive download and image limit reset

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-14 18:18:45 +08:00
1 parent 46271fb0d2
commit e9a1bf22b1
10 files changed
+51 -44

No files matched your search

@@ -35,7 +35,6 @@ import com.hippo.ehviewer.client.parser.GalleryPageApiParser
import com.hippo.ehviewer.client.parser.GalleryPageParser
import com.hippo.ehviewer.client.parser.GalleryTokenApiParser
import com.hippo.ehviewer.client.parser.HomeParser
import com.hippo.ehviewer.client.parser.Limits
import com.hippo.ehviewer.client.parser.ProfileParser
import com.hippo.ehviewer.client.parser.RateGalleryResult
import com.hippo.ehviewer.client.parser.SignInParser
@@ -306,14 +305,11 @@ object EhEngine {
return null
}
suspend fun resetImageLimits(): Limits? {
return ehRequest(EhUrl.URL_HOME) {
formBody {
add("act", "limits")
add("reset", "Reset Limit")
}
}.executeAndParsingWith(HomeParser::parseResetLimits)
}
suspend fun resetImageLimits() = ehRequest(EhUrl.URL_HOME) {
formBody {
add("reset_imagelimit", "Reset Limit")
}
}.executeAndParsingWith(HomeParser::parseResetLimits)
suspend fun modifyFavorites(url: String, gidArray: LongArray, dstCat: Int): FavoritesParser.Result {
val catStr: String = when (dstCat) {
@@ -15,7 +15,11 @@
*/
package com.hippo.ehviewer.client.exception
import androidx.annotation.StringRes
import splitties.init.appCtx
open class EhException : Exception {
constructor(detailMessage: String?) : super(detailMessage)
constructor(detailMessage: String?, cause: Throwable?) : super(detailMessage, cause)
constructor(@StringRes message: Int) : super(appCtx.getString(message))
}
@@ -0,0 +1,5 @@
package com.hippo.ehviewer.client.exception
import com.hippo.ehviewer.R
class InsufficientFundsException : EhException(R.string.insufficient_funds)
@@ -15,6 +15,7 @@
*/
package com.hippo.ehviewer.client.parser
import com.hippo.ehviewer.client.exception.InsufficientFundsException
import com.hippo.ehviewer.client.exception.NoHAtHClientException
import org.jsoup.Jsoup
@@ -27,8 +28,10 @@ object ArchiveParser {
Regex("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">")
private val PATTERN_HATH_ARCHIVE =
Regex("<p><a href=\"[^\"]*\" onclick=\"return do_hathdl\\('([0-9]+|org)'\\)\">([^<]+)</a></p>\\s*<p>([\\w. ]+)</p>\\s*<p>([\\w. ]+)</p>")
private val PATTERN_NEED_HATH_CLIENT =
Regex("You must have a H@H client assigned to your account to use this feature\\.")
private const val ERROR_NEED_HATH_CLIENT =
"You must have a H@H client assigned to your account to use this feature."
private const val ERROR_INSUFFICIENT_FUNDS =
"You do not have enough funds to download this archive."
fun parse(body: String): Result {
val m = PATTERN_HATH_FORM.find(body)!!
@@ -66,7 +69,11 @@ object ArchiveParser {
}
fun parseArchiveUrl(body: String): String? {
if (PATTERN_NEED_HATH_CLIENT.containsMatchIn(body)) throw NoHAtHClientException("No H@H client")
if (body.contains(ERROR_NEED_HATH_CLIENT)) {
throw NoHAtHClientException("No H@H client")
} else if (body.contains(ERROR_INSUFFICIENT_FUNDS)) {
throw InsufficientFundsException()
}
return Jsoup.parse(body).selectFirst("#continue>a[href]")
?.let { it.attr("href") + "?start=1" }
// TODO: Check more errors
@@ -1,20 +1,19 @@
package com.hippo.ehviewer.client.parser
import android.os.Parcelable
import com.hippo.ehviewer.client.exception.InsufficientFundsException
import com.hippo.ehviewer.client.exception.ParseException
import kotlinx.parcelize.Parcelize
object HomeParser {
private val PATTERN_FUNDS = Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL)
private const val RESET_SUCCEED = "Image limit was successfully reset."
private const val INSUFFICIENT_FUNDS = "Insufficient funds."
fun parse(body: String) = parseLimit(body)
fun parseResetLimits(body: String): Limits? {
return if (body.contains(RESET_SUCCEED)) {
null
} else {
parse(body)
fun parseResetLimits(body: String) {
if (body.contains(INSUFFICIENT_FUNDS)) {
throw InsufficientFundsException()
}
}
@@ -116,10 +116,10 @@ import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.ListUrlBuilder
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.NoHAtHClientException
import com.hippo.ehviewer.client.parser.ArchiveParser
import com.hippo.ehviewer.client.parser.HomeParser
import com.hippo.ehviewer.client.parser.ParserUtils
import com.hippo.ehviewer.client.parser.TorrentResult
import com.hippo.ehviewer.coil.justDownload
import com.hippo.ehviewer.dao.DownloadInfo
@@ -1366,19 +1366,6 @@ class GalleryDetailScene : BaseScene() {
val context = context
val activity = mainActivity
if (null != context && null != activity && null != mArchiveList && position < mArchiveList!!.size) {
if (mArchiveList!![position].name == "Insufficient Funds") {
showTip(R.string.insufficient_funds, LENGTH_SHORT)
return
} else if (mCurrentFunds != null) {
val cost = ParserUtils.parseInt(
mArchiveList!![position].cost.removeSuffix("GP").removeSuffix("Credits"),
0,
)
if (cost > maxOf(mCurrentFunds!!.fundsGP, mCurrentFunds!!.fundsC)) {
showTip(R.string.insufficient_funds, LENGTH_SHORT)
return
}
}
val res = mArchiveList!![position].res
val isHAtH = mArchiveList!![position].isHAtH
composeBindingGD?.run {
@@ -1400,10 +1387,18 @@ class GalleryDetailScene : BaseScene() {
}
showTip(R.string.download_archive_started, LENGTH_SHORT)
}.onFailure {
if (it is NoHAtHClientException) {
showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG)
} else {
showTip(R.string.download_archive_failure, LENGTH_LONG)
when (it) {
is NoHAtHClientException -> {
showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG)
}
is EhException -> {
showTip(ExceptionUtils.getReadableString(it), LENGTH_LONG)
}
else -> {
showTip(R.string.download_archive_failure, LENGTH_LONG)
}
}
}
}
@@ -127,8 +127,10 @@ fun EhScreen() {
val placeholder = stringResource(id = R.string.please_wait)
val resetImageLimitSucceed = stringResource(id = R.string.reset_limits_succeed)
val noImageLimits = stringResource(id = R.string.image_limits_summary, 0, 0)
var result by remember { mutableStateOf<HomeParser.Result?>(null) }
var summary by rememberSaveable { mutableStateOf(noImageLimits) }
suspend fun getImageLimits() = EhEngine.getImageLimits().also {
result = it
summary = context.getString(R.string.image_limits_summary, it.limits.current, it.limits.maximum)
}
val deferredResult = remember { coroutineScope.async { runSuspendCatching { getImageLimits() } } }
@@ -144,15 +146,14 @@ fun EhScreen() {
fun bind(result: HomeParser.Result) {
val (current, maximum, resetCost) = result.limits
val (fundsGP, fundsC) = result.funds
val cost = if (fundsGP >= resetCost) "$resetCost GP" else "$resetCost Credits"
val message = context.getString(R.string.current_limits, "$current / $maximum", cost) + "\n" + context.getString(R.string.current_funds, "$fundsGP+", fundsC)
val message = context.getString(R.string.current_limits, "$current / $maximum", resetCost) + "\n" + context.getString(R.string.current_funds, "$fundsGP+", fundsC)
dialog.setMessage(message)
resetButton.isEnabled = resetCost in 1..maxOf(fundsGP, fundsC)
resetButton.isEnabled = resetCost != 0
}
coroutineScope.launch {
runSuspendCatching {
val result = deferredResult.await().getOrNull() ?: getImageLimits()
withUIContext { bind(result) }
result ?: deferredResult.await().getOrNull() ?: getImageLimits()
withUIContext { bind(result!!) }
}.onFailure {
withUIContext { dialog.setMessage(it.localizedMessage) }
}
+1 -1
View File
@@ -455,7 +455,7 @@
<string name="insufficient_funds">余额不足</string>
<string name="image_limits">图片配额</string>
<string name="image_limits_summary">%1$d / %2$d(点击刷新)</string>
<string name="current_limits">当前:%1$s\n重置花费:%2$s</string>
<string name="current_limits">当前:%1$s\n重置花费:%2$d GP</string>
<string name="reset_limits_succeed">成功重置图片配额</string>
<string name="reset">重置</string>
<string name="settings_privacy">隐私</string>
+1 -1
View File
@@ -446,7 +446,7 @@
<string name="insufficient_funds">餘額不足</string>
<string name="image_limits">圖片配額</string>
<string name="image_limits_summary">%1$d / %2$d(點擊以重新整理)</string>
<string name="current_limits">目前:%1$s\n重置花費:%2$s</string>
<string name="current_limits">目前:%1$s\n重置花費:%2$d GP</string>
<string name="reset_limits_succeed">成功重置圖片配額</string>
<string name="reset">重置</string>
<string name="settings_privacy">隱私</string>
+1 -1
View File
@@ -520,7 +520,7 @@
<string name="insufficient_funds">Insufficient Funds</string>
<string name="image_limits">Image Limits</string>
<string name="image_limits_summary">%1$d / %2$d (Click to refresh)</string>
<string name="current_limits">Current: %1$s\nReset Cost: %2$s</string>
<string name="current_limits">Current: %1$s\nReset Cost: %2$d GP</string>
<string name="reset_limits_succeed">Image limit was successfully reset</string>
<string name="reset">Reset</string>
<string name="settings_privacy">Privacy</string>