-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8bd9ef
commit efe5f54
Showing
8 changed files
with
316 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/ani/dantotsu/notifications/MediaNameFetch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ani.dantotsu.notifications | ||
|
||
import ani.dantotsu.client | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class MediaNameFetch { | ||
companion object { | ||
private fun queryBuilder(mediaIds: List<Int>): String { | ||
var query = "{" | ||
mediaIds.forEachIndexed { index, mediaId -> | ||
query += """ | ||
media$index: Media(id: $mediaId) { | ||
id | ||
title { | ||
romaji | ||
} | ||
} | ||
""".trimIndent() | ||
} | ||
query += "}" | ||
return query | ||
} | ||
|
||
suspend fun fetchMediaTitles(ids: List<Int>): Map<Int, String> { | ||
return try { | ||
val url = "https://graphql.anilist.co/" | ||
val data = mapOf( | ||
"query" to queryBuilder(ids), | ||
) | ||
withContext(Dispatchers.IO) { | ||
val response = client.post( | ||
url, | ||
headers = mapOf( | ||
"Content-Type" to "application/json", | ||
"Accept" to "application/json" | ||
), | ||
data = data | ||
) | ||
val mediaResponse = parseMediaResponseWithGson(response.text) | ||
val mediaMap = mutableMapOf<Int, String>() | ||
mediaResponse.data.forEach { (_, mediaItem) -> | ||
mediaMap[mediaItem.id] = mediaItem.title.romaji | ||
} | ||
mediaMap | ||
} | ||
} catch (e: Exception) { | ||
val errorMap = mutableMapOf<Int, String>() | ||
ids.forEach { errorMap[it] = "Unknown" } | ||
errorMap | ||
} | ||
} | ||
|
||
private fun parseMediaResponseWithGson(response: String): MediaResponse { | ||
val gson = Gson() | ||
val type = object : TypeToken<MediaResponse>() {}.type | ||
return gson.fromJson(response, type) | ||
} | ||
|
||
data class MediaResponse(val data: Map<String, MediaItem>) | ||
data class MediaItem( | ||
val id: Int, | ||
val title: MediaTitle | ||
) | ||
|
||
data class MediaTitle(val romaji: String) | ||
|
||
} | ||
} |
Oops, something went wrong.