Yokan / rebuild
Course 1 8624561

AniSkipService.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/AniSkipService.kt

implementation49 lines
1package eu.friedcutlet.yokan.services.media23import eu.friedcutlet.yokan.services.catchingNonCancellation4import eu.friedcutlet.yokan.services.http.HttpRequest5import eu.friedcutlet.yokan.services.http.HttpService6import kotlinx.serialization.json.Json7import kotlinx.serialization.json.JsonArray8import kotlinx.serialization.json.JsonObject9import kotlinx.serialization.json.JsonPrimitive10import kotlinx.serialization.json.contentOrNull11import kotlinx.serialization.json.doubleOrNull1213data class AniSkipMarker(14    val type: String,15    val startSeconds: Double,16    val endSeconds: Double,17    val episodeLengthSeconds: Double,18)1920class AniSkipService(21    private val http: HttpService,22) {23    private val base = "https://api.aniskip.com/v2"24    private val json = Json { ignoreUnknownKeys = true; isLenient = true }2526    suspend fun markers(malId: Long, episode: Int, episodeLengthSeconds: Int): List<AniSkipMarker> {27        if (malId <= 0L || episode <= 0) return emptyList()28        val length = episodeLengthSeconds.coerceAtLeast(0)29        val url = "$base/skip-times/$malId/$episode?types[]=op&types[]=ed&types[]=mixed-op&types[]=mixed-ed&types[]=recap&episodeLength=$length"30        val response = catchingNonCancellation {31            http.request(HttpRequest(url, headers = mapOf("Accept" to "application/json")))32        }.getOrNull() ?: return emptyList()33        if (response.statusCode !in 200..299) return emptyList()34        val parsed = runCatching { json.parseToJsonElement(response.body) as? JsonObject }.getOrNull() ?: return emptyList()35        val found = (parsed["found"] as? JsonPrimitive)?.contentOrNull?.lowercase() == "true"36            || (parsed["found"] as? JsonPrimitive)?.contentOrNull == "true"37        if (!found) return emptyList()38        val results = parsed["results"] as? JsonArray ?: return emptyList()39        return results.mapNotNull { entry ->40            val obj = entry as? JsonObject ?: return@mapNotNull null41            val type = (obj["skipType"] as? JsonPrimitive)?.contentOrNull.orEmpty()42            val interval = obj["interval"] as? JsonObject ?: return@mapNotNull null43            val start = (interval["startTime"] as? JsonPrimitive)?.doubleOrNull ?: 0.044            val end = (interval["endTime"] as? JsonPrimitive)?.doubleOrNull ?: 0.045            val length = (obj["episodeLength"] as? JsonPrimitive)?.doubleOrNull ?: 0.046            AniSkipMarker(type = type, startSeconds = start, endSeconds = end, episodeLengthSeconds = length)47        }48    }49}50

Search chapters, reference and source filenames.