From f43c4825397243a9339c9820d9c27286bcc10887 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 28 Apr 2023 15:31:03 +0300 Subject: [PATCH] [Honeymanga] Refactoring --- .../kotatsu/parsers/site/HoneyMangaParser.kt | 122 +++++++++--------- .../koitharu/kotatsu/parsers/MangaSources.kt | 2 +- 2 files changed, 60 insertions(+), 64 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/HoneyMangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/HoneyMangaParser.kt index a7057cc1..eea9c81c 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/HoneyMangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/HoneyMangaParser.kt @@ -10,33 +10,22 @@ import org.koitharu.kotatsu.parsers.MangaLoaderContext import org.koitharu.kotatsu.parsers.MangaSourceParser import org.koitharu.kotatsu.parsers.PagedMangaParser import org.koitharu.kotatsu.parsers.config.ConfigKey -import org.koitharu.kotatsu.parsers.model.Manga -import org.koitharu.kotatsu.parsers.model.MangaChapter -import org.koitharu.kotatsu.parsers.model.MangaPage -import org.koitharu.kotatsu.parsers.model.MangaSource -import org.koitharu.kotatsu.parsers.model.MangaState -import org.koitharu.kotatsu.parsers.model.MangaTag -import org.koitharu.kotatsu.parsers.model.RATING_UNKNOWN -import org.koitharu.kotatsu.parsers.model.SortOrder -import org.koitharu.kotatsu.parsers.util.domain -import org.koitharu.kotatsu.parsers.util.generateUid +import org.koitharu.kotatsu.parsers.model.* +import org.koitharu.kotatsu.parsers.util.* import org.koitharu.kotatsu.parsers.util.json.getStringOrNull import org.koitharu.kotatsu.parsers.util.json.mapJSON import org.koitharu.kotatsu.parsers.util.json.mapJSONIndexed -import org.koitharu.kotatsu.parsers.util.parseJson -import org.koitharu.kotatsu.parsers.util.parseJsonArray -import org.koitharu.kotatsu.parsers.util.removeSuffix -import org.koitharu.kotatsu.parsers.util.tryParse -import org.koitharu.kotatsu.parsers.util.urlEncoded import java.text.SimpleDateFormat -import java.util.EnumSet -import java.util.Locale +import java.util.* private const val PAGE_SIZE = 20 +private const val INFINITE = 999999 +private const val HEADER_ENCODING = "Content-Encoding" @MangaSourceParser("HONEYMANGA", "Honey Manga", "uk") -class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, MangaSource.HONEYMANGA, PAGE_SIZE), Interceptor { +class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, MangaSource.HONEYMANGA, PAGE_SIZE), + Interceptor { private val urlApi get() = "https://data.api.$domain" private val mangaApi get() = "$urlApi/v2/manga/cursor-list" @@ -64,7 +53,7 @@ class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US) val body = JSONObject() body.put("mangaId", manga.url) - body.put("pageSize", 999999) // Hack lol (no) + body.put("pageSize", INFINITE) // Hack lol (no) body.put("page", 1) body.put("sortOrder", "ASC") val chapterRequest = webClient.httpPost(chapterApi, body).parseJson() @@ -88,9 +77,9 @@ class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, scanlator = null, uploadDate = dateFormat.tryParse(jo.getString("lastUpdated")), branch = null, - source = source + source = source, ) - } + }, ) } @@ -98,10 +87,9 @@ class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, page: Int, query: String?, tags: Set?, - sortOrder: SortOrder + sortOrder: SortOrder, ): List { val body = JSONObject() - var content: JSONArray = JSONArray() body.put("page", page) body.put("pageSize", PAGE_SIZE) val sort = JSONObject() @@ -109,32 +97,44 @@ class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, sort.put("sortOrder", "DESC") body.put("sort", sort) - // Tags - content = if (!tags.isNullOrEmpty()) { - val filters: JSONArray = JSONArray() - val tag_filter = JSONObject() - tag_filter.put("filterBy", "genres") - tag_filter.put("filterOperator", "ALL") - val tag: JSONArray = JSONArray() - tags.forEach { - tag.put(it.title) + val content = when { + !tags.isNullOrEmpty() -> { + // Tags + val filters = JSONArray() + val tagFilter = JSONObject() + tagFilter.put("filterBy", "genres") + tagFilter.put("filterOperator", "ALL") + val tag = JSONArray() + tags.forEach { + tag.put(it.title) + } + tagFilter.put("filterValue", tag) + filters.put(tagFilter) + body.put("filters", filters) + webClient.httpPost(mangaApi, body).parseJson().getJSONArray("data") + } - tag_filter.put("filterValue", tag) - filters.put(tag_filter) - body.put("filters", filters) - webClient.httpPost(mangaApi, body).parseJson().getJSONArray("data") - // Search - } else if(!query.isNullOrEmpty()) { - if((query.length < 3)) throw IllegalArgumentException("Запит має містити щонайменше 3 символи / The query must contain at least 3 characters") - if(page == 1) { - webClient.httpGet(searchApi + query.urlEncoded()).parseJsonArray() - } else { - JSONArray() + + !query.isNullOrEmpty() -> { + // Search + when { + query.length < 3 -> throw IllegalArgumentException( + "The query must contain at least 3 characters (Запит має містити щонайменше 3 символи)", + ) + + page == searchPaginator.firstPage -> webClient + .httpGet(searchApi + query.urlEncoded()) + .parseJsonArray() + + else -> JSONArray() + } + } + + else -> { + // Popular/Newest + body.put("filters", JSONArray()) + webClient.httpPost(mangaApi, body).parseJson().getJSONArray("data") } - // Popular/Newest - } else { - body.put("filters", JSONArray()) - webClient.httpPost(mangaApi, body).parseJson().getJSONArray("data") } return content.mapJSON { jo -> val id = jo.getString("id") @@ -166,32 +166,29 @@ class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, override suspend fun getPages(chapter: MangaChapter): List { val body = JSONObject() body.put("chapterId", chapter.url) - val content = webClient.httpPost(framesApi, body).parseJson().getJSONObject("resourceIds") return List(content.length()) { i -> - val item = content.get(i.toString()).toString() + val item = content.getString(i.toString()) MangaPage(id = generateUid(item), "$imageStorageUrl/$item", getCoverUrl(item, 256), source) } } override suspend fun getTags(): Set { // https://data.api.honey-manga.com.ua/genres-tags/genres-list - val tagsSet = mutableListOf() val content = webClient.httpGet(genresListApi).parseJsonArray() - (0 until content.length()).forEach { i -> - val item = content.get(i).toString() - + val tagsSet = ArraySet(content.length()) + repeat(content.length()) { i -> + val item = content.getString(i) tagsSet.add(MangaTag(item, item, source)) } - - return tagsSet.toSet() + return tagsSet } // Need for disable encoding (with encoding not working) override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() - val newRequest = if (request.header("Content-Encoding") != null) { - request.newBuilder().removeHeader("Content-Encoding").build() + val newRequest = if (request.header(HEADER_ENCODING) != null) { + request.newBuilder().removeHeader(HEADER_ENCODING).build() } else { request } @@ -214,14 +211,13 @@ class HoneyMangaParser(context: MangaLoaderContext) : PagedMangaParser(context, else -> "likes" } - private fun getTitleTags(jsonTags: JSONArray): Set { + private fun getTitleTags(jsonTags: JSONArray): Set { val tagsSet = ArraySet(jsonTags.length()) - (0 until jsonTags.length()).forEach { i -> - val item = jsonTags.get(i) + repeat(jsonTags.length()) { i -> + val item = jsonTags.getString(i) - tagsSet.add(MangaTag(item.toString(), item.toString(), source)) + tagsSet.add(MangaTag(title = item.toTitleCase(sourceLocale), key = item, source = source)) } - - return tagsSet.toSet() + return tagsSet } } diff --git a/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaSources.kt b/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaSources.kt index ef660faa..2ca95f83 100644 --- a/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaSources.kt +++ b/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaSources.kt @@ -3,5 +3,5 @@ package org.koitharu.kotatsu.parsers import org.junit.jupiter.params.provider.EnumSource import org.koitharu.kotatsu.parsers.model.MangaSource -@EnumSource(MangaSource::class, names = ["LOCAL", "DUMMY"], mode = EnumSource.Mode.EXCLUDE) +@EnumSource(MangaSource::class, names = ["HONEYMANGA"], mode = EnumSource.Mode.INCLUDE) internal annotation class MangaSources