From dbf6996744215dd3562725715669b23f4e00f715 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Mon, 2 Aug 2021 17:57:55 -0400 Subject: [PATCH] BaseUrl and Nsfw support, and bugfixes (#2) * Better logging * Fix jq rounding errors * Add baseUrl and nsfw support * Add support for SourceFactory Nsfw annotation --- .../suwayomi/tachidesk/InspectorMain.kt | 21 ++++++++--- .../manga/impl/extension/Extension.kt | 37 ++++++++++++++++--- .../tachidesk/manga/impl/util/PackageTools.kt | 2 +- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/InspectorMain.kt b/server/src/main/kotlin/suwayomi/tachidesk/InspectorMain.kt index c29387a..d376398 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/InspectorMain.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/InspectorMain.kt @@ -7,14 +7,17 @@ package suwayomi.tachidesk * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import eu.kanade.tachiyomi.source.CatalogueSource import kotlinx.serialization.Serializable import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json +import mu.KotlinLogging +import suwayomi.tachidesk.manga.impl.extension.Extension import suwayomi.tachidesk.manga.impl.extension.Extension.installAPK import java.io.File object InspectorMain { + private val logger = KotlinLogging.logger {} + suspend fun inspectorMain(args: Array) { if (args.size < 3) { throw RuntimeException("Inspector must be given the path of apks directory, output json, and a tmp dir") @@ -27,7 +30,7 @@ object InspectorMain { val tmpDir = File(tmpDirPath, "tmp").also { it.mkdir() } val extensions = File(apksPath).listFiles().orEmpty().mapNotNull { if (it.extension == "apk") { - println("Installing ${it.absolutePath}") + logger.info("Installing ${it.absolutePath}") val (pkgName, sources) = installAPK(tmpDir) { it @@ -43,9 +46,17 @@ object InspectorMain { data class SourceJson( val name: String, val lang: String, - val id: Long + val id: String, + val baseUrl: String, + val nsfw: Boolean ) { - constructor(source: CatalogueSource) : - this(source.name, source.lang, source.id) + constructor(loadedSource: Extension.LoadedSource) : + this( + loadedSource.source.name, + loadedSource.source.lang, + loadedSource.source.id.toString(), + loadedSource.source.baseUrl, + loadedSource.isNsfw + ) } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/extension/Extension.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/extension/Extension.kt index f4b504c..959e77b 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/extension/Extension.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/extension/Extension.kt @@ -7,9 +7,10 @@ package suwayomi.tachidesk.manga.impl.extension * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import eu.kanade.tachiyomi.source.CatalogueSource +import eu.kanade.tachiyomi.annotations.Nsfw import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceFactory +import eu.kanade.tachiyomi.source.online.HttpSource import mu.KotlinLogging import suwayomi.tachidesk.manga.impl.util.PackageTools.EXTENSION_FEATURE import suwayomi.tachidesk.manga.impl.util.PackageTools.LIB_VERSION_MAX @@ -23,7 +24,22 @@ import java.io.File object Extension { private val logger = KotlinLogging.logger {} - suspend fun installAPK(tmpDir: File, fetcher: suspend () -> File): Pair> { + fun isNsfw(sourceInstance: Any): Boolean { + // Annotations are proxied, hence this janky way of checking for them + return sourceInstance.javaClass.annotations + .flatMap { it.javaClass.interfaces.map { it.simpleName } } + .firstOrNull { it == Nsfw::class.java.simpleName } != null + } + + data class LoadedSource( + val source: HttpSource, + val isNsfw: Boolean + ) { + constructor(source: HttpSource) : + this(source, isNsfw(source)) + } + + suspend fun installAPK(tmpDir: File, fetcher: suspend () -> File): Pair> { val apkFile = fetcher() val jarFile = File(tmpDir, "${apkFile.nameWithoutExtension}.jar") @@ -60,9 +76,20 @@ object Extension { // collect sources from the extension return packageInfo.packageName to when (val instance = loadExtensionSources(jarFile.absolutePath, className)) { - is Source -> listOf(instance) - is SourceFactory -> instance.createSources() + is Source -> listOf(instance).filterIsInstance() + .map { LoadedSource(it) } + is SourceFactory -> { + val isNsfw = isNsfw(instance) + instance.createSources().filterIsInstance() + .map { + if (isNsfw) { + LoadedSource(it, true) + } else { + LoadedSource(it) + } + } + } else -> throw RuntimeException("Unknown source class type! ${instance.javaClass}") - }.filterIsInstance() + } } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/PackageTools.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/PackageTools.kt index bec89e5..815a4fb 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/PackageTools.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/PackageTools.kt @@ -90,7 +90,7 @@ object PackageTools { dBuilder.parse(it) } - logger.debug(parsed.manifestXml) + logger.trace(parsed.manifestXml) applicationInfo.metaData = Bundle().apply { val appTag = doc.getElementsByTagName("application").item(0)