Chunk the statuses endpoint

This commit is contained in:
Jobobby04 2021-05-10 22:01:42 -04:00
parent 2ec5581e8c
commit 273d61e69b

View File

@ -59,7 +59,7 @@ class FollowsHandler(
val hasMoreResults = mangaListResponse.limit + mangaListResponse.offset under mangaListResponse.total val hasMoreResults = mangaListResponse.limit + mangaListResponse.offset under mangaListResponse.total
val statusListResponse = client.newCall(mangaStatusListRequest(mangaListResponse.results)).await().parseAs<MangaStatusListResponse>() val statusListResponse = client.newCall(mangaStatusListRequest(mangaListResponse.results)).await().parseAs<MangaStatusListResponse>()
val results = followsParseMangaPage(mangaListResponse.results, statusListResponse) val results = followsParseMangaPage(mangaListResponse.results, statusListResponse.statuses)
MetadataMangasPage(results.map { it.first }, hasMoreResults, results.map { it.second }) MetadataMangasPage(results.map { it.first }, hasMoreResults, results.map { it.second })
} }
@ -69,7 +69,7 @@ class FollowsHandler(
* Parse follows api to manga page * Parse follows api to manga page
* used when multiple follows * used when multiple follows
*/ */
private fun followsParseMangaPage(response: List<MangaResponse>, statusListResponse: MangaStatusListResponse): List<Pair<SManga, MangaDexSearchMetadata>> { private fun followsParseMangaPage(response: List<MangaResponse>, statuses: Map<String, String?>): List<Pair<SManga, MangaDexSearchMetadata>> {
val comparator = compareBy<Pair<SManga, MangaDexSearchMetadata>> { it.second.followStatus } val comparator = compareBy<Pair<SManga, MangaDexSearchMetadata>> { it.second.followStatus }
.thenBy { it.first.title } .thenBy { it.first.title }
@ -79,7 +79,7 @@ class FollowsHandler(
lang, lang,
useLowQualityCovers useLowQualityCovers
).toSManga() to MangaDexSearchMetadata().apply { ).toSManga() to MangaDexSearchMetadata().apply {
followStatus = FollowStatus.fromDex(statusListResponse.statuses[it.data.id]).int followStatus = FollowStatus.fromDex(statuses[it.data.id]).int
} }
}.sortedWith(comparator) }.sortedWith(comparator)
} }
@ -218,15 +218,22 @@ class FollowsHandler(
val newResponse = client.newCall(followsListRequest(offset)).await() val newResponse = client.newCall(followsListRequest(offset)).await()
if (newResponse.code != 204) { if (newResponse.code != 204) {
val newMangaListResponse = newResponse.parseAs<MangaListResponse>(MdUtil.jsonParser) val newMangaListResponse = newResponse.parseAs<MangaListResponse>(MdUtil.jsonParser)
results.addAll(newMangaListResponse.results) results += newMangaListResponse.results
hasMoreResults = newMangaListResponse.limit + newMangaListResponse.offset under newMangaListResponse.total hasMoreResults = newMangaListResponse.limit + newMangaListResponse.offset under newMangaListResponse.total
lastOffset = newMangaListResponse.offset lastOffset = newMangaListResponse.offset
} else { } else {
hasMoreResults = false hasMoreResults = false
} }
} }
val statusListResponse = client.newCall(mangaStatusListRequest(results)).await().parseAs<MangaStatusListResponse>() val statuses = results.chunked(100)
followsParseMangaPage(results, statusListResponse) .map {
client.newCall(mangaStatusListRequest(results)).await().parseAs<MangaStatusListResponse>().statuses
}.fold(mutableMapOf<String, String?>()) { acc, curr ->
acc.putAll(curr)
acc
}
followsParseMangaPage(results, statuses)
} }
} }