[Komga] change language to `all`, add more logs (#9360)

* change lang to all, but keep existing id

* properly close response bodies
add more logs
This commit is contained in:
Gauthier 2021-10-04 19:31:52 +08:00 committed by GitHub
parent ea64267f83
commit 1c53f50265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 25 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'Komga' extName = 'Komga'
pkgNameSuffix = 'all.komga' pkgNameSuffix = 'all.komga'
extClass = '.KomgaFactory' extClass = '.KomgaFactory'
extVersionCode = 31 extVersionCode = 32
} }
dependencies { dependencies {

View File

@ -32,12 +32,14 @@ import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okhttp3.ResponseBody
import rx.Single import rx.Single
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.security.MessageDigest
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
@ -176,20 +178,29 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
override fun mangaDetailsRequest(manga: SManga): Request = override fun mangaDetailsRequest(manga: SManga): Request =
GET(manga.url, headers) GET(manga.url, headers)
override fun mangaDetailsParse(response: Response): SManga = override fun mangaDetailsParse(response: Response): SManga {
if (response.fromReadList()) { val responseBody = response.body
val readList = json.decodeFromString<ReadListDto>(response.body?.string()!!) ?: throw IllegalStateException("Response code ${response.code}")
readList.toSManga()
} else { return responseBody.use { body ->
val series = json.decodeFromString<SeriesDto>(response.body?.string()!!) if (response.fromReadList()) {
series.toSManga() val readList = json.decodeFromString<ReadListDto>(body.string())
readList.toSManga()
} else {
val series = json.decodeFromString<SeriesDto>(body.string())
series.toSManga()
}
} }
}
override fun chapterListRequest(manga: SManga): Request = override fun chapterListRequest(manga: SManga): Request =
GET("${manga.url}/books?unpaged=true&media_status=READY&deleted=false", headers) GET("${manga.url}/books?unpaged=true&media_status=READY&deleted=false", headers)
override fun chapterListParse(response: Response): List<SChapter> { override fun chapterListParse(response: Response): List<SChapter> {
val page = json.decodeFromString<PageWrapperDto<BookDto>>(response.body?.string()!!).content val responseBody = response.body
?: throw IllegalStateException("Response code ${response.code}")
val page = responseBody.use { json.decodeFromString<PageWrapperDto<BookDto>>(it.string()).content }
val r = page.mapIndexed { index, book -> val r = page.mapIndexed { index, book ->
SChapter.create().apply { SChapter.create().apply {
@ -207,7 +218,10 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
GET("${chapter.url}/pages") GET("${chapter.url}/pages")
override fun pageListParse(response: Response): List<Page> { override fun pageListParse(response: Response): List<Page> {
val pages = json.decodeFromString<List<PageDto>>(response.body?.string()!!) val responseBody = response.body
?: throw IllegalStateException("Response code ${response.code}")
val pages = responseBody.use { json.decodeFromString<List<PageDto>>(it.string()) }
return pages.map { return pages.map {
val url = "${response.request.url}/${it.number}" + val url = "${response.request.url}/${it.number}" +
if (!supportedImageTypes.contains(it.mediaType)) { if (!supportedImageTypes.contains(it.mediaType)) {
@ -223,13 +237,18 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
} }
private fun processSeriesPage(response: Response): MangasPage { private fun processSeriesPage(response: Response): MangasPage {
if (response.fromReadList()) { val responseBody = response.body
with(json.decodeFromString<PageWrapperDto<ReadListDto>>(response.body?.string()!!)) { ?: throw IllegalStateException("Response code ${response.code}")
return MangasPage(content.map { it.toSManga() }, !last)
} return responseBody.use { body ->
} else { if (response.fromReadList()) {
with(json.decodeFromString<PageWrapperDto<SeriesDto>>(response.body?.string()!!)) { with(json.decodeFromString<PageWrapperDto<ReadListDto>>(body.string())) {
return MangasPage(content.map { it.toSManga() }, !last) MangasPage(content.map { it.toSManga() }, !last)
}
} else {
with(json.decodeFromString<PageWrapperDto<SeriesDto>>(body.string())) {
MangasPage(content.map { it.toSManga() }, !last)
}
} }
} }
} }
@ -350,10 +369,17 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
private var authors = emptyMap<String, List<AuthorDto>>() // roles to list of authors private var authors = emptyMap<String, List<AuthorDto>>() // roles to list of authors
override val name = "Komga${if (suffix.isNotBlank()) " ($suffix)" else ""}" override val name = "Komga${if (suffix.isNotBlank()) " ($suffix)" else ""}"
override val lang = "en" override val lang = "all"
override val supportsLatest = true override val supportsLatest = true
private val LOG_TAG = "extension.all.komga${if (suffix.isNotBlank()) ".$suffix" else ""}" private val LOG_TAG = "extension.all.komga${if (suffix.isNotBlank()) ".$suffix" else ""}"
// keep the previous ID when lang was "en", so that preferences and manga bindings are not lost
override val id by lazy {
val key = "${name.toLowerCase()}/en/$versionId"
val bytes = MessageDigest.getInstance("MD5").digest(key.toByteArray())
(0..7).map { bytes[it].toLong() and 0xff shl 8 * (7 - it) }.reduce(Long::or) and Long.MAX_VALUE
}
override val baseUrl by lazy { getPrefBaseUrl() } override val baseUrl by lazy { getPrefBaseUrl() }
private val username by lazy { getPrefUsername() } private val username by lazy { getPrefUsername() }
private val password by lazy { getPrefPassword() } private val password by lazy { getPrefPassword() }
@ -428,8 +454,15 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
.subscribe( .subscribe(
{ response -> { response ->
libraries = try { libraries = try {
json.decodeFromString(response.body?.string()!!) val responseBody = response.body
if (responseBody != null) {
responseBody.use { json.decodeFromString(it.string()) }
} else {
Log.e(LOG_TAG, "error while decoding JSON for libraries filter: response body is null. Response code: ${response.code}")
emptyList()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "error while decoding JSON for libraries filter", e)
emptyList() emptyList()
} }
}, },
@ -446,8 +479,15 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
.subscribe( .subscribe(
{ response -> { response ->
collections = try { collections = try {
json.decodeFromString<PageWrapperDto<CollectionDto>>(response.body?.string()!!).content val responseBody = response.body
if (responseBody != null) {
responseBody.use { json.decodeFromString<PageWrapperDto<CollectionDto>>(it.string()).content }
} else {
Log.e(LOG_TAG, "error while decoding JSON for collections filter: response body is null. Response code: ${response.code}")
emptyList()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "error while decoding JSON for collections filter", e)
emptyList() emptyList()
} }
}, },
@ -464,8 +504,15 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
.subscribe( .subscribe(
{ response -> { response ->
genres = try { genres = try {
json.decodeFromString(response.body?.string()!!) val responseBody = response.body
if (responseBody != null) {
responseBody.use { json.decodeFromString(it.string()) }
} else {
Log.e(LOG_TAG, "error while decoding JSON for genres filter: response body is null. Response code: ${response.code}")
emptySet()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "error while decoding JSON for genres filter", e)
emptySet() emptySet()
} }
}, },
@ -482,8 +529,15 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
.subscribe( .subscribe(
{ response -> { response ->
tags = try { tags = try {
json.decodeFromString(response.body?.string()!!) val responseBody = response.body
if (responseBody != null) {
responseBody.use { json.decodeFromString(it.string()) }
} else {
Log.e(LOG_TAG, "error while decoding JSON for tags filter: response body is null. Response code: ${response.code}")
emptySet()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "error while decoding JSON for tags filter", e)
emptySet() emptySet()
} }
}, },
@ -500,8 +554,15 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
.subscribe( .subscribe(
{ response -> { response ->
publishers = try { publishers = try {
json.decodeFromString(response.body?.string()!!) val responseBody = response.body
if (responseBody != null) {
responseBody.use { json.decodeFromString(it.string()) }
} else {
Log.e(LOG_TAG, "error while decoding JSON for publishers filter: response body is null. Response code: ${response.code}")
emptySet()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "error while decoding JSON for publishers filter", e)
emptySet() emptySet()
} }
}, },
@ -518,9 +579,16 @@ open class Komga(suffix: String = "") : ConfigurableSource, HttpSource() {
.subscribe( .subscribe(
{ response -> { response ->
authors = try { authors = try {
val list: List<AuthorDto> = json.decodeFromString(response.body?.string()!!) val responseBody = response.body
list.groupBy { it.role } if (responseBody != null) {
val list = responseBody.use<ResponseBody, List<AuthorDto>> { json.decodeFromString(it.string()) }
list.groupBy { it.role }
} else {
Log.e(LOG_TAG, "error while decoding JSON for authors filter: response body is null. Response code: ${response.code}")
emptyMap()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "error while decoding JSON for authors filter", e)
emptyMap() emptyMap()
} }
}, },