[MangaDex] Fix branching ##172

Koitharu 3 years ago
parent 6183f6edf3
commit 6b95a8c55e
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -134,35 +134,10 @@ internal class MangaDexParser(context: MangaLoaderContext) : MangaParser(context
val feedDeferred = async { loadChapters(mangaId) } val feedDeferred = async { loadChapters(mangaId) }
val mangaAttrs = attrsDeferred.await() val mangaAttrs = attrsDeferred.await()
val feed = feedDeferred.await() val feed = feedDeferred.await()
// 2022-01-02T00:27:11+00:00
val dateFormat = SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'+00:00'",
Locale.ROOT,
)
manga.copy( manga.copy(
description = mangaAttrs.optJSONObject("description")?.selectByLocale() description = mangaAttrs.optJSONObject("description")?.selectByLocale()
?: manga.description, ?: manga.description,
chapters = feed.mapChapters { _, jo -> chapters = mapChapters(feed),
val id = jo.getString("id")
val attrs = jo.getJSONObject("attributes")
if (!attrs.isNull("externalUrl")) {
return@mapChapters null
}
val locale = attrs.getStringOrNull("translatedLanguage")?.let { Locale.forLanguageTag(it) }
val relations = jo.getJSONArray("relationships").associateByKey("type")
val number = attrs.getFloatOrDefault("chapter", 0f)
MangaChapter(
id = generateUid(id),
name = attrs.getStringOrNull("title")?.takeUnless(String::isEmpty)
?: "Chapter #$number",
number = number.toInt(),
url = id,
scanlator = relations["scanlation_group"]?.getStringOrNull("name"),
uploadDate = dateFormat.tryParse(attrs.getString("publishAt")),
branch = locale?.getDisplayName(locale)?.toTitleCase(locale),
source = source,
)
},
) )
} }
@ -262,6 +237,53 @@ internal class MangaDexParser(context: MangaLoaderContext) : MangaParser(context
} }
} }
private fun mapChapters(list: List<JSONObject>): List<MangaChapter> {
// 2022-01-02T00:27:11+00:00
val dateFormat = SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'+00:00'",
Locale.ROOT,
)
val branches = list.associateGrouping { jo ->
jo.getJSONObject("attributes").getStringOrNull("translatedLanguage") to
jo.getJSONArray("relationships").associateByKey("type")["scanlation_group"]
?.getJSONObject("attributes")?.getStringOrNull("name")
}
val dc = list.groupBy { jo -> jo.getJSONObject("attributes").getFloatOrDefault("chapter", 0f) }
val chaptersBuilder = ChaptersListBuilder(list.size)
var index = 0
for ((number, value) in dc) {
for (jo in value) {
val id = jo.getString("id")
val attrs = jo.getJSONObject("attributes")
if (!attrs.isNull("externalUrl")) {
continue
}
val lang = attrs.getStringOrNull("translatedLanguage")
val locale = lang?.let { Locale.forLanguageTag(it) }
val relations = jo.getJSONArray("relationships").associateByKey("type")
val team = relations["scanlation_group"]?.getJSONObject("attributes")?.getStringOrNull("name")
?.takeUnless { it.isBlank() }
var branch = locale?.getDisplayName(locale)?.toTitleCase(locale)
if (branches[lang].orEmpty().size > 1 && team != null) {
branch += " ($team)"
}
chaptersBuilder += MangaChapter(
id = generateUid(id),
name = attrs.getStringOrNull("title")?.takeUnless(String::isEmpty)
?: "Chapter #$number",
number = if (number <= 0f) (index + 1) else number.toInt(),
url = id,
scanlator = team,
uploadDate = dateFormat.tryParse(attrs.getString("publishAt")),
branch = branch,
source = source,
)
}
index++
}
return chaptersBuilder.toList()
}
private class Chapters( private class Chapters(
val data: List<JSONObject>, val data: List<JSONObject>,
val total: Int, val total: Int,

@ -66,11 +66,11 @@ inline fun <T> List<T>.areItemsEquals(other: List<T>, equals: (T, T) -> Boolean)
fun <T> Iterator<T>.nextOrNull(): T? = if (hasNext()) next() else null fun <T> Iterator<T>.nextOrNull(): T? = if (hasNext()) next() else null
inline fun <T, K, V> Collection<T>.associateGrouping(transform: (T) -> Pair<K, V>): Map<K, List<V>> { inline fun <T, K, V> Collection<T>.associateGrouping(transform: (T) -> Pair<K, V>): Map<K, Set<V>> {
val result = LinkedHashMap<K, MutableList<V>>(size) val result = LinkedHashMap<K, MutableSet<V>>(size)
for (item in this) { for (item in this) {
val (k, v) = transform(item) val (k, v) = transform(item)
result.getOrPut(k) { LinkedList() }.add(v) result.getOrPut(k) { LinkedHashSet() }.add(v)
} }
return result return result
} }

@ -1,7 +1,7 @@
package org.koitharu.kotatsu.test_util package org.koitharu.kotatsu.test_util
import androidx.collection.ArraySet import androidx.collection.ArraySet
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.model.RATING_UNKNOWN import org.koitharu.kotatsu.parsers.model.RATING_UNKNOWN
@ -44,15 +44,15 @@ inline operator fun <T> List<T>.component6(): T = get(5)
inline operator fun <T> List<T>.component7(): T = get(6) inline operator fun <T> List<T>.component7(): T = get(6)
fun mangaOf(source: MangaSource, url: String): Manga { fun mangaOf(source: MangaSource, url: String): Manga {
val httpUrl = url.toHttpUrl() val httpUrl = url.toHttpUrlOrNull()
var id = 1125899906842597L var id = 1125899906842597L
source.name.forEach { c -> id = 31 * id + c.code } source.name.forEach { c -> id = 31 * id + c.code }
url.forEach { c -> id = 31 * id + c.code } url.forEach { c -> id = 31 * id + c.code }
return Manga( return Manga(
id = id, id = id,
title = httpUrl.pathSegments.last(), title = httpUrl?.pathSegments?.last() ?: url,
altTitle = null, altTitle = null,
url = url.toRelativeUrl(httpUrl.host), url = httpUrl?.let { url.toRelativeUrl(it.host) } ?: url,
publicUrl = url, publicUrl = url,
rating = RATING_UNKNOWN, rating = RATING_UNKNOWN,
isNsfw = false, isNsfw = false,

Loading…
Cancel
Save