Yokan / rebuild
Course 1 8624561

DatabaseFallbackRuntimeService.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/extensions/DatabaseFallbackRuntimeService.kt

implementation112 lines
1package eu.friedcutlet.yokan.services.extensions23import eu.friedcutlet.yokan.database.DbPageUrl4import eu.friedcutlet.yokan.database.DbSource5import eu.friedcutlet.yokan.database.YokanDatabase6import eu.friedcutlet.yokan.domain.Status7import eu.friedcutlet.yokan.util.nowMillis89/**10 * Offline stand-in used when a source's real runtime fails: serves what the database already11 * knows about the source (saved titles, chapters and pages) without running extension code.12 * It serves no videos: there is no database-backed video cache, and fabricating a stream would13 * hand the episode page URL to the player and hide the extension failure from the UI.14 */15class DatabaseFallbackRuntimeService(16    override val source: DbSource,17    private val database: YokanDatabase,18) : ExtensionRuntimeService {19    override suspend fun supportsLatest(): Boolean = source.supportLatest == true2021    override suspend fun headers(): Map<String, String> = mapOf("User-Agent" to database.settings().userAgent)2223    override suspend fun popular(page: Int): SourceListing = listing(page)2425    override suspend fun latest(page: Int): SourceListing = listing(page)2627    override suspend fun search(query: String, page: Int, filters: List<SourceFilter>): SourceListing {28        val normalized = query.trim().lowercase()29        val items = database.mangas(source.itemType)30            .filter { normalized.isBlank() || it.name.orEmpty().lowercase().contains(normalized) }31            .map { RemoteManga(it.name.orEmpty(), it.link.orEmpty(), it.imageUrl, it.description, initialized = true) }32        return SourceListing(items = items, hasNextPage = false)33    }3435    override suspend fun detail(url: String): RemoteDetail {36        val candidates = database.mangas(source.itemType).filter { it.link == url }37        val manga = candidates.firstOrNull { it.sourceId == source.id }38            ?: candidates.firstOrNull { it.sourceId == null && it.source == source.name }39        val chapters = manga?.let { database.chapters(it.id) }.orEmpty().map {40            RemoteChapter(41                title = it.name.orEmpty(),42                url = it.url,43                dateUpload = it.dateUpload,44                scanlator = it.scanlator,45            )46        }47        return RemoteDetail(48            title = manga?.name ?: url,49            url = url,50            description = manga?.description.orEmpty(),51            author = manga?.author,52            artist = manga?.artist,53            thumbnailUrl = manga?.imageUrl,54            chapters = chapters,55            cast = manga?.cast,56            seasons = manga?.takeIf { it.fetchType == eu.friedcutlet.yokan.database.DbFetchType.Seasons }?.let { parent ->57                database.mangas(source.itemType).filter { it.parentId == parent.id }.map {58                    RemoteManga(it.name.orEmpty(), it.link.orEmpty(), it.imageUrl, it.description, initialized = true,59                        seasonNumber = it.seasonNumber, fetchesSeasons = it.fetchType == eu.friedcutlet.yokan.database.DbFetchType.Seasons)60                }61            },62        )63    }6465    // Only a fresh saved list stands in for the source; a stale one would pass expired URLs off as live.66    override suspend fun pageList(url: String): List<DbPageUrl> {67        val now = nowMillis()68        return database.pageCache()69            .firstOrNull { (it.urls?.contains(url) == true || it.chapterUrl == url) && it.isFresh(source.id, source.version, now) }70            ?.urls71            ?.map { DbPageUrl(it) }72            ?: listOf(DbPageUrl(url))73    }7475    override suspend fun htmlContent(name: String, url: String): String =76        "<html><head><title>$name</title></head><body>$url</body></html>"7778    override suspend fun filterList(): List<SourceFilter> = listOf(79        SourceFilter(80            id = "genre",81            name = "Genre",82            type = "SelectFilter",83            kind = SourceFilterKind.SingleSelect,84            entries = listOf("Action", "Drama", "Fantasy").map { FilterEntry(it, it) },85        ),86        SourceFilter(87            id = "status",88            name = "Status",89            type = "SelectFilter",90            kind = SourceFilterKind.SingleSelect,91            entries = listOf("Ongoing", "Completed").map { FilterEntry(it, it) },92        ),93    )9495    override suspend fun sourcePreferences(): List<SourcePreferenceValue> = listOf(96        SourcePreferenceValue("baseUrl", "Base URL", source.baseUrl),97        SourcePreferenceValue("lang", "Language", source.lang),98    )99100    private fun listing(page: Int): SourceListing {101        val items = database.mangas(source.itemType).map {102            RemoteManga(103                title = it.name.orEmpty(),104                url = it.link.orEmpty(),105                thumbnailUrl = it.imageUrl,106                description = it.description,107                initialized = true,108            )109        }110        return SourceListing(items = items, hasNextPage = page < 2)111    }112}113

Search chapters, reference and source filenames.