Make internal and delegated sources able to use getMangaDetails, getChapterList, and getPageList properly

This commit is contained in:
Jobobby04
2021-01-02 03:11:20 -05:00
parent 23ac4b271c
commit 28fca8c839
26 changed files with 576 additions and 7 deletions
@@ -14,6 +14,7 @@ import exh.metadata.metadata.MangaDexSearchMetadata
import exh.metadata.metadata.base.RaisedTag
import exh.metadata.metadata.base.getFlatMetadataForManga
import exh.metadata.metadata.base.insertFlatMetadata
import exh.util.await
import exh.util.floor
import exh.util.nullIfZero
import kotlinx.serialization.decodeFromString
@@ -24,6 +25,7 @@ import kotlinx.serialization.json.jsonPrimitive
import okhttp3.Response
import rx.Completable
import rx.Single
import tachiyomi.source.model.MangaInfo
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@@ -70,6 +72,24 @@ class ApiMangaParser(private val langs: List<String>) {
}
}
suspend fun parseToManga(manga: MangaInfo, input: Response, forceLatestCover: Boolean, sourceId: Long): MangaInfo {
val mangaId = db.getManga(manga.key, sourceId).await()?.id
val metadata = if (mangaId != null) {
val flatMetadata = db.getFlatMetadataForManga(mangaId).await()
flatMetadata?.raise(metaClass) ?: newMetaInstance()
} else newMetaInstance()
parseInfoIntoMetadata(metadata, input, forceLatestCover)
if (mangaId != null) {
metadata.mangaId = mangaId
db.insertFlatMetadata(metadata.flatten()).await()
}
return metadata.createMangaInfo(manga)
}
fun parseInfoIntoMetadata(metadata: MangaDexSearchMetadata, input: Response, forceLatestCover: Boolean) = parseIntoMetadata(metadata, input, forceLatestCover)
fun parseIntoMetadata(metadata: MangaDexSearchMetadata, input: Response, forceLatestCover: Boolean) {
with(metadata) {
try {
@@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.network.asObservableSuccess
import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.toSManga
import exh.md.utils.MdUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -14,6 +15,7 @@ import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.Request
import rx.Observable
import tachiyomi.source.model.MangaInfo
class MangaHandler(val client: OkHttpClient, val headers: Headers, val langs: List<String>, val forceLatestCovers: Boolean = false) {
@@ -56,6 +58,13 @@ class MangaHandler(val client: OkHttpClient, val headers: Headers, val langs: Li
}
}
suspend fun getMangaDetails(manga: MangaInfo, sourceId: Long): MangaInfo {
return withContext(Dispatchers.IO) {
val response = client.newCall(apiRequest(manga.toSManga())).await()
ApiMangaParser(langs).parseToManga(manga, response, forceLatestCovers, sourceId)
}
}
fun fetchMangaDetailsObservable(manga: SManga): Observable<SManga> {
return client.newCall(apiRequest(manga))
.asObservableSuccess()
@@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.MetadataUtil
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Date
@@ -42,6 +43,53 @@ class EHentaiSearchMetadata : RaisedSearchMetadata() {
var aged: Boolean = false
var lastUpdateCheck: Long = 0
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = gId?.let { gId ->
gToken?.let { gToken ->
idAndTokenToUrl(gId, gToken)
}
}
val cover = thumbnailUrl
// No title bug?
val title = if (Injekt.get<PreferencesHelper>().useJapaneseTitle().get()) {
altTitle ?: title
} else {
title
}
// Set artist (if we can find one)
val artist = tags.filter { it.namespace == EH_ARTIST_NAMESPACE }.let { tags ->
if (tags.isNotEmpty()) tags.joinToString(transform = { it.name }) else null
}
// Copy tags -> genres
val genres = tagsToGenreList()
// Try to automatically identify if it is ongoing, we try not to be too lenient here to avoid making mistakes
// We default to completed
var status = MangaInfo.COMPLETED
title?.let { t ->
MetadataUtil.ONGOING_SUFFIX.find {
t.endsWith(it, ignoreCase = true)
}?.let {
status = MangaInfo.ONGOING
}
}
val description = "meta"
return manga.copy(
key = key ?: manga.key,
title = title ?: manga.title,
artist = artist ?: manga.artist,
description = description,
genres = genres,
status = status,
cover = cover ?: manga.cover
)
}
override fun copyTo(manga: SManga) {
gId?.let { gId ->
gToken?.let { gToken ->
@@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
@Serializable
class EightMusesSearchMetadata : RaisedSearchMetadata() {
@@ -14,6 +15,29 @@ class EightMusesSearchMetadata : RaisedSearchMetadata() {
var thumbnailUrl: String? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = path.joinToString("/", prefix = "/")
val title = title
val cover = thumbnailUrl
val artist = tags.ofNamespace(ARTIST_NAMESPACE).joinToString { it.name }
val genres = tagsToGenreList()
val description = "meta"
return manga.copy(
key = key,
title = title ?: manga.title,
cover = cover ?: manga.cover,
artist = artist,
genres = genres,
description = description
)
}
override fun copyTo(manga: SManga) {
manga.url = path.joinToString("/", prefix = "/")
@@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
@Serializable
class HBrowseSearchMetadata : RaisedSearchMetadata() {
@@ -19,6 +20,32 @@ class HBrowseSearchMetadata : RaisedSearchMetadata() {
// Length in pages
var length: Int? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = hbUrl
val title = title
// Guess thumbnail URL if manga does not have thumbnail URL
val cover = if (manga.cover.isBlank()) {
guessThumbnailUrl(hbId.toString())
} else null
val artist = tags.ofNamespace(ARTIST_NAMESPACE).joinToString { it.name }
val genres = tagsToGenreList()
val description = "meta"
return manga.copy(
key = key ?: manga.key,
title = title ?: manga.title,
cover = cover ?: manga.cover,
artist = artist,
genres = genres,
description = description
)
}
override fun copyTo(manga: SManga) {
hbUrl?.let {
manga.url = it
@@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
@Serializable
class HentaiCafeSearchMetadata : RaisedSearchMetadata() {
@@ -24,6 +25,31 @@ class HentaiCafeSearchMetadata : RaisedSearchMetadata() {
var artist: String? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val cover = thumbnailUrl
val title = title
val artist = artist
val author = artist
// Not available
val status = MangaInfo.UNKNOWN
val genres = tagsToGenreList()
val description = "meta"
return manga.copy(
cover = cover ?: manga.cover,
title = title ?: manga.title,
artist = artist ?: manga.artist,
author = author ?: manga.author,
status = status,
genres = genres,
description = description
)
}
override fun copyTo(manga: SManga) {
thumbnailUrl?.let { manga.thumbnail_url = it }
@@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.MetadataUtil
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
import java.util.Date
@Serializable
@@ -37,6 +38,30 @@ class HitomiSearchMetadata : RaisedSearchMetadata() {
var uploadDate: Long? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val cover = thumbnailUrl
val title = title
// Copy tags -> genres
val genres = tagsToGenreList()
val artist = artists.joinToString()
val status = MangaInfo.UNKNOWN
val description = "meta"
return manga.copy(
cover = cover ?: manga.cover,
title = title ?: manga.title,
genres = genres,
artist = artist,
status = status,
description = description
)
}
override fun copyTo(manga: SManga) {
thumbnailUrl?.let { manga.thumbnail_url = it }
@@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
@Serializable
class MangaDexSearchMetadata : RaisedSearchMetadata() {
@@ -40,6 +41,43 @@ class MangaDexSearchMetadata : RaisedSearchMetadata() {
var follow_status: Int? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = mdUrl?.let {
try {
val uri = it.toUri()
val out = uri.path!!.removePrefix("/api")
out + if (out.endsWith("/")) "" else "/"
} catch (e: Exception) {
it
}
}
val title = title
val cover = thumbnail_url
val author = author
val artist = artist
val status = status
val genres = tagsToGenreList()
val description = description
return manga.copy(
key = key ?: manga.key,
title = title ?: manga.title,
cover = cover ?: manga.cover,
author = author ?: manga.author,
artist = artist ?: manga.artist,
status = status ?: manga.status,
genres = genres,
description = description ?: manga.description
)
}
override fun copyTo(manga: SManga) {
mdUrl?.let {
manga.url = try {
@@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.MetadataUtil
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
import java.util.Date
@Serializable
@@ -37,6 +38,53 @@ class NHentaiSearchMetadata : RaisedSearchMetadata() {
var preferredTitle: Int? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = nhId?.let { nhIdToPath(it) }
val cover = if (mediaId != null) {
typeToExtension(coverImageType)?.let {
"https://t.nhentai.net/galleries/$mediaId/cover.$it"
}
} else null
val title = when (preferredTitle) {
TITLE_TYPE_SHORT -> shortTitle ?: englishTitle ?: japaneseTitle ?: manga.title
0, TITLE_TYPE_ENGLISH -> englishTitle ?: japaneseTitle ?: shortTitle ?: manga.title
else -> englishTitle ?: japaneseTitle ?: shortTitle ?: manga.title
}
// Set artist (if we can find one)
val artist = tags.filter { it.namespace == NHENTAI_ARTIST_NAMESPACE }.let { tags ->
if (tags.isNotEmpty()) tags.joinToString(transform = { it.name }) else null
}
// Copy tags -> genres
val genres = tagsToGenreList()
// Try to automatically identify if it is ongoing, we try not to be too lenient here to avoid making mistakes
// We default to completed
var status = SManga.COMPLETED
englishTitle?.let { t ->
MetadataUtil.ONGOING_SUFFIX.find {
t.endsWith(it, ignoreCase = true)
}?.let {
status = SManga.ONGOING
}
}
val description = "meta"
return manga.copy(
key = key ?: manga.key,
cover = cover ?: manga.cover,
title = title,
artist = artist ?: manga.artist,
genres = genres,
status = status,
description = description
)
}
override fun copyTo(manga: SManga) {
nhId?.let { manga.url = nhIdToPath(it) }
@@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.metadata.base.RaisedSearchMetadata
import exh.metadata.metadata.base.RaisedTitle
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
@Serializable
class PervEdenSearchMetadata : RaisedSearchMetadata() {
@@ -33,6 +34,36 @@ class PervEdenSearchMetadata : RaisedSearchMetadata() {
var lang: String? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = url
val cover = thumbnailUrl
val title = title
val artist = artist
val status = when (status) {
"Ongoing" -> MangaInfo.ONGOING
"Completed", "Suspended" -> MangaInfo.COMPLETED
else -> MangaInfo.UNKNOWN
}
// Copy tags -> genres
val genres = tagsToGenreList()
val description = "meta"
return manga.copy(
key = key ?: manga.key,
cover = cover ?: manga.cover,
title = title ?: manga.title,
artist = artist ?: manga.artist,
status = status,
genres = genres,
description = description
)
}
override fun copyTo(manga: SManga) {
url?.let { manga.url = it }
thumbnailUrl?.let { manga.thumbnail_url = it }
@@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
@Serializable
class PururinSearchMetadata : RaisedSearchMetadata() {
@@ -26,6 +27,33 @@ class PururinSearchMetadata : RaisedSearchMetadata() {
var ratingCount: Int? = null
var averageRating: Double? = null
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val key = prId?.let { prId ->
prShortLink?.let { prShortLink ->
"/gallery/$prId/$prShortLink"
}
}
val title = title ?: altTitle
val cover = thumbnailUrl
val artist = tags.ofNamespace(TAG_NAMESPACE_ARTIST).joinToString { it.name }
val genres = tagsToGenreList()
val description = "meta"
return manga.copy(
key = key ?: manga.key,
title = title ?: manga.title,
cover = cover ?: manga.cover,
artist = artist,
genres = genres,
description = description
)
}
override fun copyTo(manga: SManga) {
prId?.let { prId ->
prShortLink?.let { prShortLink ->
@@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.source.model.SManga
import exh.metadata.MetadataUtil
import exh.metadata.metadata.base.RaisedSearchMetadata
import kotlinx.serialization.Serializable
import tachiyomi.source.model.MangaInfo
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
@@ -41,6 +42,29 @@ class TsuminoSearchMetadata : RaisedSearchMetadata() {
var character: List<String> = emptyList()
override fun createMangaInfo(manga: MangaInfo): MangaInfo {
val title = title
val cover = tmId?.let { BASE_URL.replace("www", "content") + thumbUrlFromId(it.toString()) }
val artist = artist
val status = SManga.UNKNOWN
// Copy tags -> genres
val genres = tagsToGenreList()
val description = "meta"
return manga.copy(
title = title ?: manga.title,
cover = cover ?: manga.cover,
artist = artist ?: manga.artist,
status = status,
genres = genres,
description = description
)
}
override fun copyTo(manga: SManga) {
title?.let { manga.title = it }
manga.thumbnail_url = BASE_URL.replace("www", "content") + thumbUrlFromId(tmId.toString())
@@ -25,6 +25,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass
import tachiyomi.source.model.MangaInfo
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@@ -57,8 +58,12 @@ abstract class RaisedSearchMetadata {
abstract fun copyTo(manga: SManga)
abstract fun createMangaInfo(manga: MangaInfo): MangaInfo
fun tagsToGenreString() = tags.toGenreString()
fun tagsToGenreList() = tags.toGenreList()
fun tagsToDescription() =
StringBuilder("Tags:\n").apply {
// BiConsumer only available in Java 8, don't bother calling forEach directly on 'tags'
@@ -142,6 +147,10 @@ abstract class RaisedSearchMetadata {
(this).filter { it.type != TAG_TYPE_VIRTUAL }
.joinToString { (if (it.namespace != null) "${it.namespace}: " else "") + it.name }
fun MutableList<RaisedTag>.toGenreList() =
(this).filter { it.type != TAG_TYPE_VIRTUAL }
.map { (if (it.namespace != null) "${it.namespace}: " else "") + it.name }
private val module = SerializersModule {
polymorphic(RaisedSearchMetadata::class) {
subclass(EHentaiSearchMetadata::class)
@@ -10,6 +10,8 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import rx.Observable
import tachiyomi.source.model.ChapterInfo
import tachiyomi.source.model.MangaInfo
abstract class DelegatedHttpSource(val delegate: HttpSource) : HttpSource() {
/**
@@ -185,6 +187,14 @@ abstract class DelegatedHttpSource(val delegate: HttpSource) : HttpSource() {
return delegate.fetchMangaDetails(manga)
}
/**
* [1.x API] Get the updated details for a manga.
*/
override suspend fun getMangaDetails(manga: MangaInfo): MangaInfo {
ensureDelegateCompatible()
return delegate.getMangaDetails(manga)
}
/**
* Returns the request for the details of a manga. Override only if it's needed to change the
* url, send different headers or request method like POST.
@@ -207,6 +217,14 @@ abstract class DelegatedHttpSource(val delegate: HttpSource) : HttpSource() {
return delegate.fetchChapterList(manga)
}
/**
* [1.x API] Get all the available chapters for a manga.
*/
override suspend fun getChapterList(manga: MangaInfo): List<ChapterInfo> {
ensureDelegateCompatible()
return delegate.getChapterList(manga)
}
/**
* Returns an observable with the page list for a chapter.
*
@@ -217,6 +235,14 @@ abstract class DelegatedHttpSource(val delegate: HttpSource) : HttpSource() {
return delegate.fetchPageList(chapter)
}
/**
* [1.x API] Get the list of pages a chapter has.
*/
override suspend fun getPageList(chapter: ChapterInfo): List<tachiyomi.source.model.Page> {
ensureDelegateCompatible()
return delegate.getPageList(chapter)
}
/**
* Returns an observable with the page containing the source url of the image. If there's any
* error, it will return null instead of throwing an exception.
@@ -9,6 +9,8 @@ import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource
import okhttp3.Response
import tachiyomi.source.model.ChapterInfo
import tachiyomi.source.model.MangaInfo
import uy.kohesive.injekt.injectLazy
class EnhancedHttpSource(
@@ -177,6 +179,11 @@ class EnhancedHttpSource(
*/
override fun fetchMangaDetails(manga: SManga) = source().fetchMangaDetails(manga)
/**
* [1.x API] Get the updated details for a manga.
*/
override suspend fun getMangaDetails(manga: MangaInfo): MangaInfo = source().getMangaDetails(manga)
/**
* Returns the request for the details of a manga. Override only if it's needed to change the
* url, send different headers or request method like POST.
@@ -193,6 +200,11 @@ class EnhancedHttpSource(
*/
override fun fetchChapterList(manga: SManga) = source().fetchChapterList(manga)
/**
* [1.x API] Get all the available chapters for a manga.
*/
override suspend fun getChapterList(manga: MangaInfo): List<ChapterInfo> = source().getChapterList(manga)
/**
* Returns an observable with the page list for a chapter.
*
@@ -200,6 +212,11 @@ class EnhancedHttpSource(
*/
override fun fetchPageList(chapter: SChapter) = source().fetchPageList(chapter)
/**
* [1.x API] Get the list of pages a chapter has.
*/
override suspend fun getPageList(chapter: ChapterInfo): List<tachiyomi.source.model.Page> = source().getPageList(chapter)
/**
* Returns an observable with the page containing the source url of the image. If there's any
* error, it will return null instead of throwing an exception.