From 423b9659050bf764e8cb99350fad7dea5cc2ad4e Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 15 Jun 2022 11:42:11 +0300 Subject: [PATCH] Add utility functions --- build.gradle | 2 +- .../kotlin/org/koitharu/kotatsu/parsers/model/Manga.kt | 4 ++++ .../org/koitharu/kotatsu/parsers/util/json/JsonExt.kt | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9b9107ed..7e6298a0 100644 --- a/build.gradle +++ b/build.gradle @@ -51,7 +51,7 @@ afterEvaluate { } dependencies { - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2' implementation 'com.squareup.okhttp3:okhttp:4.9.3' implementation 'com.squareup.okio:okio:3.1.0' implementation 'org.jsoup:jsoup:1.15.1' diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/model/Manga.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/model/Manga.kt index c346518e..7e55306c 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/model/Manga.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/model/Manga.kt @@ -76,6 +76,10 @@ class Manga( val hasRating: Boolean get() = rating in 0f..1f + fun getChapters(branch: String?): List? { + return chapters?.filter { x -> x.branch == branch } + } + @InternalParsersApi fun copy( title: String = this.title, diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/json/JsonExt.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/json/JsonExt.kt index 3fccf101..04df05bf 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/json/JsonExt.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/json/JsonExt.kt @@ -94,6 +94,16 @@ fun JSONObject.getDoubleOrDefault(name: String, defaultValue: Double): Double { } } +fun JSONObject.getFloatOrDefault(name: String, defaultValue: Float): Float { + return when (val rawValue = opt(name)) { + null, JSONObject.NULL -> defaultValue + is Float -> rawValue + is Number -> rawValue.toFloat() + is String -> rawValue.toFloatOrNull() ?: defaultValue + else -> defaultValue + } +} + fun JSONArray.JSONIterator(): Iterator = JSONIterator(this) fun JSONArray.stringIterator(): Iterator = JSONStringIterator(this)