[MangaLib] Add branching support

pull/346/head
Koitharu 3 years ago
parent ec11b79b18
commit d089fad6ce
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -1,5 +1,6 @@
package org.koitharu.kotatsu.parsers.site.ru.rulib
import androidx.collection.ArrayMap
import androidx.collection.ArraySet
import kotlinx.coroutines.withTimeoutOrNull
import okhttp3.Response
@ -17,10 +18,7 @@ import org.koitharu.kotatsu.parsers.exception.NotFoundException
import org.koitharu.kotatsu.parsers.exception.ParseException
import org.koitharu.kotatsu.parsers.model.*
import org.koitharu.kotatsu.parsers.util.*
import org.koitharu.kotatsu.parsers.util.json.JSONIterator
import org.koitharu.kotatsu.parsers.util.json.getStringOrNull
import org.koitharu.kotatsu.parsers.util.json.mapJSON
import org.koitharu.kotatsu.parsers.util.json.values
import org.koitharu.kotatsu.parsers.util.json.*
import java.text.SimpleDateFormat
import java.util.*
@ -102,10 +100,15 @@ internal open class MangaLibParser(
if (line.startsWith("window.__DATA__")) {
val json = JSONObject(line.substringAfter('=').substringBeforeLast(';'))
val list = json.getJSONObject("chapters").getJSONArray("list")
val branches = json.getJSONObject("chapters").getJSONArray("branches").toJSONList()
.associate { x ->
x.getInt("id") to x.getJSONArray("teams").toJSONList().joinToString { it.getString("name") }
}
val id = json.optJSONObject("user")?.getLong("id")?.toString() ?: "not"
val total = list.length()
chapters = ChaptersListBuilder(total)
for (i in 0 until total) {
val counters = ArrayMap<Int, Int>(branches.size)
for (i in (0 until total).reversed()) {
val item = list.getJSONObject(i)
val chapterId = item.getLong("chapter_id")
val scanlator = item.getStringOrNull("username")
@ -130,22 +133,22 @@ internal open class MangaLibParser(
val volume = item.getInt("chapter_volume")
val number = item.getString("chapter_number")
val fullNameChapter = "Том $volume. Глава $number"
val branchId = item.getIntOrDefault("branch_id", 0)
chapters.add(
MangaChapter(
id = generateUid(chapterId),
url = url,
source = source,
number = total - i,
number = counters.incrementAndGet(branchId),
uploadDate = dateFormat.tryParse(
item.getString("chapter_created_at").substringBefore(" "),
),
scanlator = scanlator,
branch = null,
branch = branches[branchId],
name = if (nameChapter.isNullOrBlank()) fullNameChapter else "$fullNameChapter - $nameChapter",
),
)
}
chapters.reverse()
break@scripts
}
}
@ -343,6 +346,13 @@ internal open class MangaLibParser(
return isSuccessful && mimeType?.startsWith("image/") == true && headersContentLength() >= 1024L
}
private fun MutableMap<Int, Int>.incrementAndGet(key: Int): Int {
var v = getOrDefault(key, 0)
v++
put(key, v)
return v
}
@MangaSourceParser("MANGALIB", "MangaLib", "ru")
class Impl(context: MangaLoaderContext) : MangaLibParser(context, MangaSource.MANGALIB)

Loading…
Cancel
Save