Introduce HomeParser

This commit is contained in:
FooIbar authored and tarsin committed 2023-02-11 07:54:11 +08:00
1 parent 4dc7a97322
commit 6413ed67c6
4 files changed
+28 -23

No files matched your search

@@ -41,6 +41,7 @@ import com.hippo.ehviewer.client.parser.GalleryNotAvailableParser
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.ProfileParser
import com.hippo.ehviewer.client.parser.RateGalleryParser
import com.hippo.ehviewer.client.parser.SignInParser
@@ -760,7 +761,7 @@ object EhEngine {
}
@Throws(Throwable::class)
suspend fun getFunds(): ArchiveParser.Funds? {
suspend fun getFunds(): HomeParser.Funds {
val url = EhUrl.URL_FUNDS
Log.d(TAG, url)
val request = EhRequestBuilder(url, null).build()
@@ -773,7 +774,7 @@ object EhEngine {
code = response.code
headers = response.headers
body = response.body.string()
return ArchiveParser.parseFunds(body!!)
return HomeParser.parseFunds(body!!)
}
} catch (e: Throwable) {
ExceptionUtils.throwIfFatal(e)
@@ -23,8 +23,6 @@ object ArchiveParser {
Regex("<strong>(.*)</strong>.*<a href=\"([^\"]*)\">Click Here To Start Downloading</a>")
private val PATTERN_CURRENT_FUNDS =
Regex("<p>([\\d,]+) GP \\[[^]]*] &nbsp; ([\\d,]+) Credits \\[[^]]*]</p>")
private val PATTERN_FUNDS =
Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL)
private val PATTERN_HATH_FORM =
Regex("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">")
private val PATTERN_HATH_ARCHIVE =
@@ -61,7 +59,7 @@ object ArchiveParser {
PATTERN_CURRENT_FUNDS.find(body)?.groupValues?.run {
val fundsGP = ParserUtils.parseInt(get(1), 0)
val fundsC = ParserUtils.parseInt(get(2), 0)
val funds = Funds(fundsGP, fundsC)
val funds = HomeParser.Funds(fundsGP, fundsC)
result.funds = funds
}
return result
@@ -75,14 +73,6 @@ object ArchiveParser {
// TODO: Check more errors
}
fun parseFunds(body: String): Funds? {
return PATTERN_FUNDS.find(body)?.groupValues?.run {
val fundsC = ParserUtils.parseInt(get(1), 0)
val fundsGP = ParserUtils.parseInt(get(2), 0) * 1000
return Funds(fundsGP, fundsC)
}
}
class Archive(
val res: String,
val name: String,
@@ -91,7 +81,5 @@ object ArchiveParser {
val isHAtH: Boolean
)
class Funds(var fundsGP: Int, var fundsC: Int)
class Result(val paramOr: String?, val archiveList: List<Archive>, var funds: Funds?)
class Result(val paramOr: String?, val archiveList: List<Archive>, var funds: HomeParser.Funds?)
}
@@ -0,0 +1,19 @@
package com.hippo.ehviewer.client.parser
import com.hippo.ehviewer.client.exception.ParseException
object HomeParser {
private val PATTERN_FUNDS =
Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL)
fun parseFunds(body: String): Funds {
PATTERN_FUNDS.find(body)?.groupValues?.run {
val fundsC = ParserUtils.parseInt(get(1), 0)
val fundsGP = ParserUtils.parseInt(get(2), 0) * 1000
return Funds(fundsGP, fundsC)
}
throw ParseException("Parse funds error", body)
}
data class Funds(val fundsGP: Int, val fundsC: Int)
}
@@ -78,6 +78,7 @@ import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.ListUrlBuilder
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.RateGalleryParser
import com.hippo.ehviewer.client.parser.TorrentParser
@@ -149,7 +150,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
}
private var mArchiveFormParamOr: String? = null
private var mArchiveList: List<ArchiveParser.Archive>? = null
private var mCurrentFunds: ArchiveParser.Funds? = null
private var mCurrentFunds: HomeParser.Funds? = null
@State
private var mState = STATE_INIT
@@ -1459,7 +1460,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
}
}
private fun bind(data: List<ArchiveParser.Archive>?, funds: ArchiveParser.Funds?) {
private fun bind(data: List<ArchiveParser.Archive>?, funds: HomeParser.Funds?) {
if (null == mDialog || null == mProgressView || null == mErrorText || null == mListView) {
return
}
@@ -1511,11 +1512,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
val cost = ParserUtils.parseInt(
mArchiveList!![position].cost.removeSuffix("GP").removeSuffix("Credits"), 0
)
if (cost <= mCurrentFunds!!.fundsGP) {
mCurrentFunds!!.fundsGP -= cost
} else if (cost <= mCurrentFunds!!.fundsC) {
mCurrentFunds!!.fundsC -= cost
} else {
if (cost > maxOf(mCurrentFunds!!.fundsGP, mCurrentFunds!!.fundsC)) {
showTip(R.string.insufficient_funds, LENGTH_SHORT)
return
}