BaseUrl and Nsfw support, and bugfixes (#2)

* Better logging

* Fix jq rounding errors

* Add baseUrl and nsfw support

* Add support for SourceFactory Nsfw annotation
This commit is contained in:
Syer10 2021-08-02 17:57:55 -04:00 committed by GitHub
parent 737b74af2a
commit dbf6996744
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 11 deletions

View File

@ -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<String>) {
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
)
}
}

View File

@ -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<String, List<CatalogueSource>> {
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<String, List<LoadedSource>> {
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<HttpSource>()
.map { LoadedSource(it) }
is SourceFactory -> {
val isNsfw = isNsfw(instance)
instance.createSources().filterIsInstance<HttpSource>()
.map {
if (isNsfw) {
LoadedSource(it, true)
} else {
LoadedSource(it)
}
}
}
else -> throw RuntimeException("Unknown source class type! ${instance.javaClass}")
}.filterIsInstance<CatalogueSource>()
}
}
}

View File

@ -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)