From d19e67ed2da7048386f281ee3498de241c7bd8eb Mon Sep 17 00:00:00 2001 From: nicki Date: Fri, 11 Aug 2023 12:59:21 -0500 Subject: [PATCH] MangaDex: Add filtering to list search (#17460) * increment extVersionCode closes https://github.com/tachiyomiorg/tachiyomi-extensions/issues/17456 * add filtering to `list:` * prefer using the constant instead * revert to fallback url if MD@H node fails * handle edge case in accessing list entries mostly just to ensure the Retry message doesn't happen * finish off TODO as manga redirects work fine nowadays * linting * add missing headers this helps add the necessary tachiyomi ua and other stuff * fix getMangaUrl --- src/all/mangadex/build.gradle | 2 +- .../extension/all/mangadex/MangaDex.kt | 30 +++++++++---------- .../all/mangadex/MdAtHomeReportInterceptor.kt | 12 +++++++- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/all/mangadex/build.gradle b/src/all/mangadex/build.gradle index bfedc6091..bc8627526 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 = 186 + extVersionCode = 187 isNsfw = 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 132694b73..ac0af605d 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 @@ -202,7 +202,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St ), ) .asObservableSuccess() - .map { searchMangaListParse(it, page) } + .map { searchMangaListParse(it, page, filters) } else -> super.fetchSearchManga(page, query.trim(), filters) } @@ -289,7 +289,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St return GET("${MDConstants.apiListUrl}/$list", headers, CacheControl.FORCE_NETWORK) } - private fun searchMangaListParse(response: Response, page: Int): MangasPage { + private fun searchMangaListParse(response: Response, page: Int, filters: FilterList): MangasPage { val listDto = response.parseAs() val listDtoFiltered = listDto.data!!.relationships.filterIsInstance() val amount = listDtoFiltered.count() @@ -300,7 +300,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St val minIndex = (page - 1) * MDConstants.mangaLimit - val url = MDConstants.apiMangaUrl.toHttpUrl().newBuilder() + val tempUrl = MDConstants.apiMangaUrl.toHttpUrl().newBuilder() .addQueryParameter("limit", MDConstants.mangaLimit.toString()) .addQueryParameter("offset", "0") .addQueryParameter("includes[]", MDConstants.coverArt) @@ -310,13 +310,20 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St .map(MangaDataDto::id) .toSet() - url.addQueryParameter("ids[]", ids) + tempUrl.addQueryParameter("ids[]", ids) - val mangaRequest = GET(url.build(), headers, CacheControl.FORCE_NETWORK) + val finalUrl = helper.mdFilters.addFiltersToUrl( + url = tempUrl, + filters = filters.ifEmpty { getFilterList() }, + dexLang = dexLang, + ) + + val mangaRequest = GET(finalUrl, headers, CacheControl.FORCE_NETWORK) val mangaResponse = client.newCall(mangaRequest).execute() val mangaList = searchMangaListParse(mangaResponse) - val hasNextPage = amount.toFloat() / MDConstants.mangaLimit - (page.toFloat() - 1) > 1 + val hasNextPage = amount.toFloat() / MDConstants.mangaLimit - (page.toFloat() - 1) > 1 && + ids.size == MDConstants.mangaLimit return MangasPage(mangaList, hasNextPage) } @@ -370,11 +377,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St // Manga Details section override fun getMangaUrl(manga: SManga): String { - // TODO: Remove once redirect for /manga is fixed. - val title = manga.title - val url = baseUrl + manga.url.replaceFirst("manga", "title") - - return "$url/" + helper.titleToSlug(title) + return baseUrl + manga.url + "/" + helper.titleToSlug(manga.title) } /** @@ -496,10 +499,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St */ private fun paginatedChapterListRequest(mangaId: String, offset: Int): Request { val url = helper.getChapterEndpoint(mangaId, offset, dexLang).toHttpUrl().newBuilder() - .addQueryParameter("contentRating[]", "safe") - .addQueryParameter("contentRating[]", "suggestive") - .addQueryParameter("contentRating[]", "erotica") - .addQueryParameter("contentRating[]", "pornographic") + .addQueryParameter("contentRating[]", MDConstants.allContentRatings) .addQueryParameter("excludedGroups[]", preferences.blockedGroups) .addQueryParameter("excludedUploaders[]", preferences.blockedUploaders) .build() diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt index 0864f3121..e7685f3af 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt @@ -2,12 +2,14 @@ package eu.kanade.tachiyomi.extension.all.mangadex import android.util.Log import eu.kanade.tachiyomi.extension.all.mangadex.dto.ImageReportDto +import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import okhttp3.Call import okhttp3.Callback import okhttp3.Headers +import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Interceptor import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient @@ -55,7 +57,15 @@ class MdAtHomeReportInterceptor( // gets stuck, as it tend to happens sometimes. client.newCall(reportRequest).enqueue(REPORT_CALLBACK) - return response + return if (!response.isSuccessful) { + Log.e("MangaDex", "Error connecting to MD@Home node, fallback to uploads server") + val fallbackUrl = MDConstants.cdnUrl.toHttpUrl().newBuilder() + .addPathSegments(originalRequest.url.pathSegments.joinToString("/")) + .build() + client.newCall(GET(fallbackUrl, headers)).execute() + } else { + response + } } companion object {