MAL: add way to search by list items' titles

This commit is contained in:
arkon 2021-01-04 14:30:04 -05:00
parent d8a3692d92
commit 8a792e6d76
2 changed files with 58 additions and 22 deletions

View file

@ -22,6 +22,7 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
const val REREADING = 7
private const val SEARCH_ID_PREFIX = "id:"
private const val SEARCH_LIST_PREFIX = "my:"
}
private val json: Json by injectLazy()
@ -88,6 +89,12 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
}
}
if (query.startsWith(SEARCH_LIST_PREFIX)) {
query.substringAfter(SEARCH_LIST_PREFIX).let { title ->
return api.findListItems(title)
}
}
return api.search(query)
}

View file

@ -164,28 +164,13 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
}
suspend fun findListItem(track: Track, offset: Int = 0): Track? {
return withContext(Dispatchers.IO) {
val urlBuilder = "$baseApiUrl/users/@me/mangalist".toUri().buildUpon()
.appendQueryParameter("fields", "list_status")
.appendQueryParameter("limit", listPaginationAmount.toString())
if (offset > 0) {
urlBuilder.appendQueryParameter("offset", offset.toString())
}
val request = Request.Builder()
.url(urlBuilder.build().toString())
.get()
.build()
authClient.newCall(request)
.await()
.parseAs<JsonObject>()
.let {
val obj = it.jsonObject
val json = getListPage(offset)
val obj = json.jsonObject
val trackedManga = obj["data"]!!.jsonArray.find { data ->
data.jsonObject["node"]!!.jsonObject["id"]!!.jsonPrimitive.int == track.media_id
}
when {
return when {
// Found the item in the list
trackedManga != null -> {
parseMangaItem(trackedManga.jsonObject["list_status"]!!.jsonObject, track)
@ -200,6 +185,50 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
}
}
}
suspend fun findListItems(query: String, offset: Int = 0): List<TrackSearch> {
return withContext(Dispatchers.IO) {
val json = getListPage(offset)
val obj = json.jsonObject
val matches = obj["data"]!!.jsonArray
.filter {
it.jsonObject["node"]!!.jsonObject["title"]!!.jsonPrimitive.content.contains(
query,
ignoreCase = true
)
}
.map {
val id = it.jsonObject["node"]!!.jsonObject["id"]!!.jsonPrimitive.int
async { getMangaDetails(id) }
}
.awaitAll()
// Check next page if there's more
if (!obj["paging"]!!.jsonObject["next"]?.jsonPrimitive?.contentOrNull.isNullOrBlank()) {
matches + findListItems(query, offset + listPaginationAmount)
} else {
matches
}
}
}
private suspend fun getListPage(offset: Int): JsonObject {
return withContext(Dispatchers.IO) {
val urlBuilder = "$baseApiUrl/users/@me/mangalist".toUri().buildUpon()
.appendQueryParameter("fields", "list_status")
.appendQueryParameter("limit", listPaginationAmount.toString())
if (offset > 0) {
urlBuilder.appendQueryParameter("offset", offset.toString())
}
val request = Request.Builder()
.url(urlBuilder.build().toString())
.get()
.build()
authClient.newCall(request)
.await()
.parseAs()
}
}