Terra Historicus: improve parsing and upload date logic (#12167)

This commit is contained in:
kasperskier 2022-06-12 22:17:30 +08:00 committed by GitHub
parent c5cd8463b9
commit 4ed2c3d31e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 39 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'Terra Historicus' extName = 'Terra Historicus'
pkgNameSuffix = 'zh.terrahistoricus' pkgNameSuffix = 'zh.terrahistoricus'
extClass = '.TerraHistoricus' extClass = '.TerraHistoricus'
extVersionCode = 1 extVersionCode = 2
} }
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"

View File

@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -21,43 +22,45 @@ class TerraHistoricus : HttpSource() {
private val json: Json by injectLazy() private val json: Json by injectLazy()
override fun popularMangaRequest(page: Int) = GET("$baseUrl/api/comic") override fun popularMangaRequest(page: Int) = GET("$baseUrl/api/comic", headers)
override fun popularMangaParse(response: Response) =
MangasPage(response.parseAs<List<THComic>>().map { it.toSManga() }, false)
override fun popularMangaParse(response: Response) = MangasPage( override fun latestUpdatesRequest(page: Int) = GET("$baseUrl/api/recentUpdate", headers)
json.decodeFromString<THResult<List<THComic>>>(response.body!!.string()).data.map(THComic::toSManga), override fun latestUpdatesParse(response: Response) =
false MangasPage(response.parseAs<List<THRecentUpdate>>().map { it.toSManga() }, false)
)
override fun latestUpdatesRequest(page: Int) = GET("$baseUrl/api/recentUpdate") override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> =
fetchPopularManga(page).map { mangasPage ->
val mangas = mangasPage.mangas.filter { it.title.contains(query) }
MangasPage(mangas, false)
}
override fun latestUpdatesParse(response: Response) = MangasPage( override fun searchMangaRequest(page: Int, query: String, filters: FilterList) = throw UnsupportedOperationException("Not used.")
json.decodeFromString<THResult<List<THRecentUpdate>>>(response.body!!.string()).data.map(THRecentUpdate::toSManga), override fun searchMangaParse(response: Response) = throw UnsupportedOperationException("Not used.")
false
)
override fun searchMangaRequest(page: Int, query: String, filters: FilterList) = // navigate webview to webpage
throw UnsupportedOperationException("没有搜索功能") override fun mangaDetailsRequest(manga: SManga) = GET(baseUrl + manga.url.removePrefix("/api"), headers)
override fun searchMangaParse(response: Response) = throw UnsupportedOperationException("没有搜索功能") override fun fetchMangaDetails(manga: SManga): Observable<SManga> =
client.newCall(chapterListRequest(manga)).asObservableSuccess()
.map { response -> mangaDetailsParse(response).apply { initialized = true } }
override fun mangaDetailsParse(response: Response) = override fun mangaDetailsParse(response: Response) = response.parseAs<THComic>().toSManga()
json.decodeFromString<THResult<THComic>>(response.body!!.string()).data.toSManga()
override fun chapterListParse(response: Response) = override fun chapterListParse(response: Response) = response.parseAs<THComic>().toSChapterList()
json.decodeFromString<THResult<THComic>>(response.body!!.string()).data.toSChapterList().orEmpty()
override fun fetchPageList(chapter: SChapter): Observable<List<Page>> { override fun fetchPageList(chapter: SChapter): Observable<List<Page>> =
return client.newCall(pageListRequest(chapter)) client.newCall(pageListRequest(chapter)).asObservableSuccess().map { response ->
.asObservableSuccess() (0 until response.parseAs<THEpisode>().pageInfos!!.size).map {
.map { response -> Page(it, "$baseUrl${chapter.url}/page?pageNum=${it + 1}")
(0 until json.decodeFromString<THResult<THEpisode>>(response.body!!.string()).data.pageInfos!!.size).map {
Page(it, "$baseUrl${chapter.url}/page?pageNum=${it + 1}")
}
} }
} }
override fun pageListParse(response: Response) = throw UnsupportedOperationException("Not used.") override fun pageListParse(response: Response) = throw UnsupportedOperationException("Not used.")
override fun imageUrlParse(response: Response) = override fun imageUrlParse(response: Response) = response.parseAs<THPage>().url
json.decodeFromString<THResult<THPage>>(response.body!!.string()).data.url
private inline fun <reified T> Response.parseAs() =
json.decodeFromString<THResult<T>>(this.body!!.string()).data
} }

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.extension.zh.terrahistoricus package eu.kanade.tachiyomi.extension.zh.terrahistoricus
import eu.kanade.tachiyomi.AppInfo
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -7,10 +8,11 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
data class THResult<T>(val code: Int, val msg: String, val data: T) data class THResult<T>(val code: Int, val msg: String, val data: T)
// https://web.hycdn.cn/comic/site/umi.dee612f8.js
@Serializable @Serializable
data class THComic( data class THComic(
val cid: String, val cid: String,
// val type: Int, val type: Int,
val cover: String, val cover: String,
val title: String, val title: String,
val subtitle: String, val subtitle: String,
@ -23,30 +25,51 @@ data class THComic(
) { ) {
private fun getDescription(): String? { private fun getDescription(): String? {
var result = "" var result = ""
if (subtitle.isNotEmpty()) result += "$subtitle" if (subtitle.isNotEmpty()) result += "$subtitle\n"
if (introduction != null) result += introduction if (introduction != null) result += introduction
return result.ifEmpty { null } return result.ifEmpty { null }
} }
private fun getType() = when (type) {
2 -> listOf("相簿")
3 -> listOf("四格")
else -> emptyList() // 1 = Normal
}
fun toSManga() = SManga.create().apply { fun toSManga() = SManga.create().apply {
url = "/api/comic/$cid" url = "/api/comic/$cid"
title = this@THComic.title title = this@THComic.title
author = authors.joinToString("") author = authors.joinToString("")
thumbnail_url = cover thumbnail_url = cover
description = getDescription() description = getDescription()
genre = keywords?.joinToString("")?.replace("", ", ") genre = (getType() + keywords.orEmpty()).joinToString(", ").replace("", ", ")
} }
fun toSChapterList() = episodes?.map { episode -> fun toSChapterList() = episodes!!.map { episode ->
SChapter.create().apply { SChapter.create().apply {
url = "/api/comic/$cid/episode/${episode.cid!!}" url = "/api/comic/$cid/episode/${episode.cid!!}"
try { name = if (episode.type != 1) {
chapter_number = episode.shortTitle?.toFloat() ?: chapter_number "${EPISODE_TYPES[episode.type]} ${episode.title}"
name = episode.title } else if (episode.shortTitle.isNullOrBlank()) {
} catch (e: NumberFormatException) { episode.title
name = "${episode.shortTitle} ${episode.title}" } else {
"${episode.shortTitle} ${episode.title}"
} }
date_upload = (updateTime ?: 0L) * 1000 }
}.apply {
if (isNewDateLogic) {
this[0].date_upload = (updateTime ?: 0L) * 1000
}
}
companion object {
private val EPISODE_TYPES = arrayOf("", "正篇", "番外", "贺图", "公告")
private val isNewDateLogic = run {
val commitCount = AppInfo.getVersionName().substringAfter('-', "")
if (commitCount.isNotEmpty()) // Preview
commitCount.toInt() >= 4442
else // Stable
AppInfo.getVersionCode() >= 81
} }
} }
} }
@ -72,7 +95,7 @@ data class THRecentUpdate(
@Serializable @Serializable
data class THEpisode( data class THEpisode(
val cid: String? = null, val cid: String? = null,
// val type: Int, val type: Int,
val shortTitle: String?, val shortTitle: String?,
val title: String, // 作品信息中 val title: String, // 作品信息中
// val likes: Int? = null, // val likes: Int? = null,