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 389ddd888..954b46f8d 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt @@ -669,7 +669,14 @@ object EhEngine { code = response.code headers = response.headers body = response.body.string() - result = ArchiveParser.parse(body)!! + result = ArchiveParser.parse(body!!)!! + if (result.funds == null) { + try { + result.funds = getFunds() + } catch (e: Throwable) { + e.printStackTrace() + } + } } } catch (e: Throwable) { ExceptionUtils.throwIfFatal(e) @@ -711,6 +718,7 @@ object EhEngine { val call = okHttpClient.newCall(request) var body: String? = null var headers: Headers? = null + var result: String? var code = -1 try { call.executeAsync().use { response -> @@ -718,15 +726,16 @@ object EhEngine { headers = response.headers body = response.body.string() transformException(code, headers, body, null) + result = ArchiveParser.parseArchiveUrl(body!!) } } catch (e: Throwable) { ExceptionUtils.throwIfFatal(e) transformException(code, headers, body, e) throw e } - var result = ArchiveParser.parseArchiveUrl(body) if (!isHAtH) { if (result == null) { + // Wait for the server to prepare archives delay(1000) try { call.clone().executeAsync().use { response -> @@ -734,13 +743,13 @@ object EhEngine { headers = response.headers body = response.body.string() transformException(code, headers, body, null) + result = ArchiveParser.parseArchiveUrl(body!!) } } catch (e: Throwable) { ExceptionUtils.throwIfFatal(e) transformException(code, headers, body, e) throw e } - result = ArchiveParser.parseArchiveUrl(body) if (result == null) { throw EhException("Archive unavailable") } @@ -750,6 +759,29 @@ object EhEngine { return null } + @Throws(Throwable::class) + suspend fun getFunds(): ArchiveParser.Funds? { + val url = EhUrl.URL_FUNDS + Log.d(TAG, url) + val request = EhRequestBuilder(url, null).build() + val call = okHttpClient.newCall(request) + var body: String? = null + var headers: Headers? = null + var code = -1 + try { + call.executeAsync().use { response -> + code = response.code + headers = response.headers + body = response.body.string() + return ArchiveParser.parseFunds(body!!) + } + } catch (e: Throwable) { + ExceptionUtils.throwIfFatal(e) + transformException(code, headers, body, e) + throw e + } + } + @Throws(Throwable::class) private suspend fun getProfileInternal( url: String, referer: String diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhUrl.kt b/app/src/main/java/com/hippo/ehviewer/client/EhUrl.kt index d8079b231..1d5e07180 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhUrl.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhUrl.kt @@ -44,6 +44,7 @@ object EhUrl { const val URL_SIGN_IN = "https://forums.e-hentai.org/index.php?act=Login" const val URL_REGISTER = "https://forums.e-hentai.org/index.php?act=Reg&CODE=00" const val URL_FORUMS = "https://forums.e-hentai.org/" + const val URL_FUNDS = "https://e-hentai.org/exchange.php?t=gp" const val REFERER_EX = "https://$DOMAIN_EX" const val ORIGIN_EX = REFERER_EX const val REFERER_E = "https://$DOMAIN_E" 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 a07d095f1..924797737 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 @@ -26,6 +26,8 @@ object ArchiveParser { Pattern.compile("(.*).*Click Here To Start Downloading") private val PATTERN_CURRENT_FUNDS = Pattern.compile("

([\\d,]+) GP \\[[^]]*]   ([\\d,]+) Credits \\[[^]]*]

") + private val PATTERN_FUNDS = + Pattern.compile("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", Pattern.DOTALL) private val PATTERN_HATH_FORM = Pattern.compile("
") private val PATTERN_HATH_ARCHIVE = @@ -33,8 +35,7 @@ object ArchiveParser { private val PATTERN_NEED_HATH_CLIENT = Pattern.compile("You must have a H@H client assigned to your account to use this feature\\.") - fun parse(body: String?): Result? { - body ?: return null + fun parse(body: String): Result? { var m = PATTERN_HATH_FORM.matcher(body) if (!m.find()) { return null @@ -44,7 +45,7 @@ object ArchiveParser { val d = Jsoup.parse(body) val es = d.select("#db>div>div") for (e in es) { - if (!e.attr("style").contains("color:#CCCCCC")) { + if (e.childrenSize() > 0 && !e.attr("style").contains("color:#CCCCCC")) { try { val res = e.selectFirst("form>input")!!.attr("value") val name = e.selectFirst("form>div>input")!!.attr("value") @@ -77,8 +78,7 @@ object ArchiveParser { } @Throws(NoHAtHClientException::class) - fun parseArchiveUrl(body: String?): String? { - body ?: return null + fun parseArchiveUrl(body: String): String? { val m = PATTERN_NEED_HATH_CLIENT.matcher(body) if (m.find()) { throw NoHAtHClientException("No H@H client") @@ -92,6 +92,15 @@ object ArchiveParser { // TODO: Check more errors } + fun parseFunds(body: String): Funds? { + val m = PATTERN_FUNDS.matcher(body) + return if (m.find()) { + val fundsC = ParserUtils.parseInt(m.group(1), 0) + val fundsGP = ParserUtils.parseInt(m.group(2), 0) * 1000 + return Funds(fundsGP, fundsC) + } else null + } + class Archive( val res: String, val name: String, @@ -114,5 +123,5 @@ object ArchiveParser { class Funds(var fundsGP: Int, var fundsC: Int) - class Result(val paramOr: String?, val archiveList: List, val funds: Funds?) + class Result(val paramOr: String?, val archiveList: List, var funds: Funds?) } \ No newline at end of file diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index 18737fa0b..c50a87fa2 100755 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -1466,13 +1466,12 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl mListView!!.adapter = ArrayAdapter(mDialog!!.context, R.layout.item_select_dialog, nameArray) if (funds != null) { - mDialog!!.setTitle( - getString( - R.string.current_funds, - funds.fundsGP, - funds.fundsC - ) - ) + var fundsGP = funds.fundsGP.toString() + // Ex GP numbers are rounded down to the nearest thousand + if (Settings.getGallerySite() == EhUrl.SITE_EX) { + fundsGP += "+" + } + mDialog!!.setTitle(getString(R.string.current_funds, fundsGP, funds.fundsC)) } } } @@ -1481,7 +1480,10 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl val context = context val activity = mainActivity if (null != context && null != activity && null != mArchiveList && position < mArchiveList!!.size) { - if (mCurrentFunds != null) { + 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 ) diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index af663edfb..134185cdc 100755 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -531,7 +531,7 @@ 原始档案 重采样档案 下载压缩包需要 H@H 客户端 - 资金:%d GP,%d Credits + 资金:%s GP,%d Credits 余额不足 隐私 不允许屏幕抓取 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4da48ee79..7e486c529 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -598,7 +598,7 @@ Original archive Resample archive Need H@H client for archive download - Funds: %d GP, %d Credits + Funds: %s GP, %d Credits Insufficient Funds Privacy Prevent screenshots