Suspend parser

This commit is contained in:
tarsin committed 2023-07-12 09:22:24 +08:00
1 parent 4694245b51
commit 37ec49ed69
5 files changed
+9 -11

No files matched your search

+2 -5
View File
@@ -33,7 +33,6 @@ import com.hippo.ehviewer.dao.LocalFavoriteInfo
import com.hippo.ehviewer.dao.QuickSearch
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.util.sendTo
import kotlinx.coroutines.runBlocking
import splitties.arch.room.roomDb
object EhDB {
@@ -162,11 +161,9 @@ object EhDB {
}
}
@Synchronized
fun containLocalFavorites(gid: Long): Boolean {
suspend fun containLocalFavorites(gid: Long): Boolean {
val dao = db.localFavoritesDao()
// TODO: remove runBlocking
return runBlocking { dao.contains(gid) }
return dao.contains(gid)
}
suspend fun putLocalFavorites(galleryInfo: GalleryInfo) {
@@ -121,7 +121,8 @@ private fun rethrowExactly(code: Int, headers: Headers, body: String, e: Throwab
throw e
}
private suspend inline fun <T> Request.executeAndParsingWith(block: String.() -> T): T {
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
private suspend inline fun <T> Request.executeAndParsingWith(block: suspend String.() -> T): T {
Log.d(TAG, url.toString())
return execute {
val body = body.string()
@@ -21,7 +21,7 @@ import com.hippo.ehviewer.client.exception.ParseException
import splitties.init.appCtx
object FavoritesParser {
fun parse(body: String): Result {
suspend fun parse(body: String): Result {
if (body.contains("This page requires you to log on.</p>")) {
throw EhException(appCtx.getString(R.string.need_sign_in))
}
@@ -83,7 +83,7 @@ object GalleryDetailParser {
"<p>(And if you choose to ignore this warning, you lose all rights to complain about it in the future.)</p>"
private const val PINING_STRING = "<p>This gallery is pining for the fjords.</p>"
fun parse(body: String): GalleryDetail {
suspend fun parse(body: String): GalleryDetail {
if (body.contains(OFFENSIVE_STRING)) {
throw OffensiveException()
}
@@ -108,7 +108,7 @@ object GalleryDetailParser {
return galleryDetail
}
private fun parseDetail(gd: GalleryDetail, d: Document, body: String) {
private suspend fun parseDetail(gd: GalleryDetail, d: Document, body: String) {
PATTERN_DETAIL.find(body)?.apply {
gd.gid = groupValues[1].toLongOrNull() ?: -1L
gd.token = groupValues[2]
@@ -35,13 +35,13 @@ object GalleryListParser {
private val PATTERN_PREV = Pattern.compile("prev=(\\d+(-\\d+)?)")
private val PATTERN_NEXT = Pattern.compile("next=(\\d+(-\\d+)?)")
fun parse(body: String): Result {
suspend fun parse(body: String): Result {
val d = Jsoup.parse(body)
d.outputSettings().prettyPrint(false)
return parse(d, body)
}
fun parse(d: Document, body: String): Result {
suspend fun parse(d: Document, body: String): Result {
val result = Result()
try {
val prev = d.getElementById("uprev")