From f92453cdcf7fc514a90d2b2f0f5409bab01895fc Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sun, 26 Sep 2021 19:23:14 +0700 Subject: [PATCH] MangaDex: Title fetching improvement (#9238) Some manga somehow has romaji title tagged as "jp" language. So also use altTitles list and use extension language as the base language when searching while keeping english as fallback. This reduces the possibility of this extension returning empty title. --- src/all/mangadex/build.gradle | 2 +- .../extension/all/mangadex/MangaDex.kt | 8 +++----- .../extension/all/mangadex/MangaDexHelper.kt | 20 ++++++++++++++++--- .../extension/all/mangadex/dto/MangaDto.kt | 1 + 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/all/mangadex/build.gradle b/src/all/mangadex/build.gradle index 28545fca4..f0f507062 100644 --- a/src/all/mangadex/build.gradle +++ b/src/all/mangadex/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'MangaDex' pkgNameSuffix = 'all.mangadex' extClass = '.MangaDexFactory' - extVersionCode = 138 + extVersionCode = 139 containsNsfw = true } diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt index d8221786d..79a2d3499 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt @@ -137,7 +137,7 @@ abstract class MangaDex(override val lang: String, val dexLang: String) : val fileName = mangaDataDto.relationships.firstOrNull { relationshipDto -> relationshipDto.type.equals(MDConstants.coverArt, true) }?.attributes?.fileName - helper.createBasicManga(mangaDataDto, fileName, coverSuffix) + helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang) } return MangasPage(mangaList, hasMoreResults) @@ -185,7 +185,7 @@ abstract class MangaDex(override val lang: String, val dexLang: String) : relationshipDto.type.equals(MDConstants.coverArt, true) relationshipDto.type.equals(MDConstants.coverArt, true) }?.attributes?.fileName - helper.createBasicManga(mangaDataDto, fileName, coverSuffix) + helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang) } return MangasPage(mangaList, hasMoreResults) @@ -316,10 +316,8 @@ abstract class MangaDex(override val lang: String, val dexLang: String) : override fun mangaDetailsParse(response: Response): SManga { val manga = helper.json.decodeFromString(response.body!!.string()) - val shortLang = lang.substringBefore("-") - val coverSuffix = preferences.getString(MDConstants.getCoverQualityPreferenceKey(dexLang), "") - return helper.createManga(manga.data, fetchSimpleChapterList(manga, shortLang), shortLang, coverSuffix) + return helper.createManga(manga.data, fetchSimpleChapterList(manga, dexLang), dexLang, coverSuffix) } /** diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt index bdf681737..fe850ea6e 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexHelper.kt @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +import kotlinx.serialization.json.jsonArray import okhttp3.CacheControl import okhttp3.Headers import okhttp3.OkHttpClient @@ -166,10 +167,23 @@ class MangaDexHelper() { /** * create an SManga from json element only basic elements */ - fun createBasicManga(mangaDataDto: MangaDataDto, coverFileName: String?, coverSuffix: String?): SManga { + fun createBasicManga( + mangaDataDto: MangaDataDto, + coverFileName: String?, + coverSuffix: String?, + lang: String + ): SManga { return SManga.create().apply { url = "/manga/${mangaDataDto.id}" - title = cleanString(mangaDataDto.attributes.title.asMdMap()["en"] ?: "") + val titleMap = mangaDataDto.attributes.title.asMdMap() + val dirtyTitle = titleMap[lang] + ?: titleMap["en"] + ?: mangaDataDto.attributes.altTitles.jsonArray + .find { + val altTitle = it.asMdMap() + altTitle[lang] ?: altTitle["en"] != null + }?.asMdMap()?.values?.singleOrNull() + title = cleanString(dirtyTitle ?: "") coverFileName?.let { thumbnail_url = when (coverSuffix != null && coverSuffix != "") { @@ -231,7 +245,7 @@ class MangaDexHelper() { .filter { it.isNullOrBlank().not() } val desc = attr.description.asMdMap() - return createBasicManga(mangaDataDto, coverFileName, coverSuffix).apply { + return createBasicManga(mangaDataDto, coverFileName, coverSuffix, lang).apply { description = cleanString(desc[lang] ?: desc["en"] ?: "") author = authors.joinToString(", ") artist = artists.joinToString(", ") diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/dto/MangaDto.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/dto/MangaDto.kt index 0eb7e4cc1..d2e997ba5 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/dto/MangaDto.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/dto/MangaDto.kt @@ -44,6 +44,7 @@ data class MangaDataDto( @Serializable data class MangaAttributesDto( val title: JsonElement, + val altTitles: JsonElement, val description: JsonElement, val originalLanguage: String, val lastVolume: String?,