mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2025-12-08 14:05:46 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b597312a0c | |||
| 541134d8a7 | |||
| 0c2c7e9e50 | |||
| 3ecfb4298b | |||
| 8c49e60dec | |||
| 51957eb369 | |||
| 1bab6417ed | |||
| ea9d76fa47 | |||
| d5d3de9559 |
16
CHANGELOG.md
16
CHANGELOG.md
@@ -1,5 +1,9 @@
|
||||
# SauceNaoAPI Changelog
|
||||
|
||||
## 0.5.0
|
||||
|
||||
* Versions updates
|
||||
|
||||
## 0.4.0
|
||||
|
||||
* Update libraries versions
|
||||
@@ -12,6 +16,18 @@
|
||||
* Now `SauceNaoAPI` working with synchronous queue
|
||||
* `SauceNaoAPI` now will wait for some time when one of limits will be achieved
|
||||
|
||||
### 0.4.4
|
||||
|
||||
* Uploading of file
|
||||
* Updates of versions
|
||||
* Now `SauceNaoAPI` do not require api key
|
||||
* `SauceNaoAPI` instances now can return `limitsState` object, which will contains `LimitsState` with currently known
|
||||
state of limits
|
||||
|
||||
### 0.4.3
|
||||
|
||||
Hotfix for serializer of `SauceNaoAnswer`
|
||||
|
||||
### 0.4.2
|
||||
|
||||
Hotfix for autostop for some time when there is no remaining quotas for requests
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
project.version = "0.4.2"
|
||||
project.version = "0.5.0"
|
||||
project.group = "com.github.insanusmokrassar"
|
||||
|
||||
buildscript {
|
||||
@@ -32,7 +32,7 @@ dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
|
||||
implementation "joda-time:joda-time:$joda_time_version"
|
||||
implementation "com.soywiz.korlibs.klock:klock:$klock_version"
|
||||
implementation "io.ktor:ktor-client-core:$ktor_version"
|
||||
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
kotlin.code.style=official
|
||||
kotlin_version=1.3.50
|
||||
kotlin_coroutines_version=1.3.2
|
||||
kotlin_serialisation_runtime_version=0.13.0
|
||||
joda_time_version=2.10.4
|
||||
ktor_version=1.2.5
|
||||
kotlin_version=1.3.72
|
||||
kotlin_coroutines_version=1.3.8
|
||||
kotlin_serialisation_runtime_version=0.20.0
|
||||
klock_version=1.11.14
|
||||
ktor_version=1.3.2
|
||||
|
||||
project_public_name=SauceNao API
|
||||
project_public_description=SauceNao API library
|
||||
|
||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.streams.asInput
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
|
||||
suspend fun main(vararg args: String) {
|
||||
val (key, requestUrl) = args
|
||||
val (key, requestSubject) = args
|
||||
|
||||
val api = SauceNaoAPI(key, scope = GlobalScope)
|
||||
api.use {
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
val api = SauceNaoAPI(key, scope = scope)
|
||||
api.use { _ ->
|
||||
println(
|
||||
it.request(requestUrl)
|
||||
when {
|
||||
requestSubject.startsWith("/") -> File(requestSubject).let {
|
||||
api.request(
|
||||
it.inputStream().asInput(),
|
||||
ContentType.parse(Files.probeContentType(it.toPath()))
|
||||
)
|
||||
}
|
||||
else -> api.request(requestSubject)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
scope.cancel()
|
||||
}
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.*
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.*
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.calculateSleepTime
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.call
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.client.features.ClientRequestException
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.response.readText
|
||||
import io.ktor.client.request.forms.*
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.readText
|
||||
import io.ktor.http.*
|
||||
import io.ktor.utils.io.core.Input
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.io.core.Closeable
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.joda.time.DateTime
|
||||
import java.io.Closeable
|
||||
import java.util.logging.Logger
|
||||
import kotlin.Result
|
||||
import kotlin.coroutines.*
|
||||
|
||||
private const val API_TOKEN_FIELD = "api_key"
|
||||
private const val OUTPUT_TYPE_FIELD = "output_type"
|
||||
private const val URL_FIELD = "url"
|
||||
private const val FILE_FIELD = "file"
|
||||
private const val FILENAME_FIELD = "filename"
|
||||
private const val DB_FIELD = "db"
|
||||
private const val DBMASK_FIELD = "dbmask"
|
||||
private const val DBMASKI_FIELD = "dbmaski"
|
||||
@@ -32,7 +36,7 @@ private const val MINIMAL_SIMILARITY_FIELD = "minsim"
|
||||
private const val SEARCH_URL = "https://saucenao.com/search.php"
|
||||
|
||||
data class SauceNaoAPI(
|
||||
private val apiToken: String,
|
||||
private val apiToken: String? = null,
|
||||
private val outputType: OutputType = JsonOutputType,
|
||||
private val client: HttpClient = HttpClient(OkHttp),
|
||||
private val searchUrl: String = SEARCH_URL,
|
||||
@@ -44,6 +48,9 @@ data class SauceNaoAPI(
|
||||
private val timeManager = TimeManager(scope)
|
||||
private val quotaManager = RequestQuotaManager(scope)
|
||||
|
||||
val limitsState: LimitsState
|
||||
get() = quotaManager.limitsState
|
||||
|
||||
private val requestsJob = scope.launch {
|
||||
for ((callback, requestBuilder) in requestsChannel) {
|
||||
quotaManager.getQuota()
|
||||
@@ -54,7 +61,8 @@ data class SauceNaoAPI(
|
||||
|
||||
quotaManager.updateQuota(answer.header, timeManager)
|
||||
} catch (e: TooManyRequestsException) {
|
||||
quotaManager.happenTooManyRequests(timeManager)
|
||||
logger.warning("Exceed time limit. Answer was:\n${e.answerContent}")
|
||||
quotaManager.happenTooManyRequests(timeManager, e)
|
||||
requestsChannel.send(callback to requestBuilder)
|
||||
} catch (e: Exception) {
|
||||
try {
|
||||
@@ -72,7 +80,18 @@ data class SauceNaoAPI(
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
url.asSauceRequestSubject,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun request(
|
||||
mediaInput: Input,
|
||||
mimeType: ContentType = mediaInput.mimeType,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
mediaInput.asSauceRequestSubject(mimeType),
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
@@ -83,7 +102,7 @@ data class SauceNaoAPI(
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
url.asSauceRequestSubject,
|
||||
db = db,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
@@ -95,7 +114,7 @@ data class SauceNaoAPI(
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
url.asSauceRequestSubject,
|
||||
dbmask = dbmask,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
@@ -107,7 +126,7 @@ data class SauceNaoAPI(
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
url.asSauceRequestSubject,
|
||||
dbmaski = dbmaski,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
@@ -117,21 +136,21 @@ data class SauceNaoAPI(
|
||||
builder: HttpRequestBuilder
|
||||
): SauceNaoAnswer {
|
||||
return try {
|
||||
val call = client.execute(builder)
|
||||
val answerText = call.response.readText()
|
||||
val call = client.request<HttpResponse>(builder)
|
||||
val answerText = call.readText()
|
||||
logger.info(answerText)
|
||||
timeManager.addTimeAndClear()
|
||||
Json.nonstrict.parse(
|
||||
SauceNaoAnswer.serializer(),
|
||||
SauceNaoAnswerSerializer,
|
||||
answerText
|
||||
)
|
||||
} catch (e: ClientRequestException) {
|
||||
throw e.sauceNaoAPIException
|
||||
throw e.sauceNaoAPIException()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun makeRequest(
|
||||
url: String,
|
||||
request: SauceRequestSubject,
|
||||
db: Int? = null,
|
||||
dbmask: Int? = null,
|
||||
dbmaski: Int? = null,
|
||||
@@ -142,14 +161,45 @@ data class SauceNaoAPI(
|
||||
requestsChannel.offer(
|
||||
it to HttpRequestBuilder().apply {
|
||||
url(searchUrl)
|
||||
parameter(URL_FIELD, url)
|
||||
parameter(API_TOKEN_FIELD, apiToken)
|
||||
|
||||
apiToken ?.also { parameter(API_TOKEN_FIELD, it) }
|
||||
parameter(OUTPUT_TYPE_FIELD, outputType.typeCode)
|
||||
db ?.also { parameter(DB_FIELD, it) }
|
||||
dbmask ?.also { parameter(DBMASK_FIELD, it) }
|
||||
dbmaski ?.also { parameter(DBMASKI_FIELD, it) }
|
||||
resultsCount ?.also { parameter(RESULTS_COUNT_FIELD, it) }
|
||||
minSimilarity ?.also { parameter(MINIMAL_SIMILARITY_FIELD, it) }
|
||||
|
||||
when (request) {
|
||||
is UrlSauceRequestSubject -> {
|
||||
parameter(URL_FIELD, request.url)
|
||||
}
|
||||
is InputRequestSubject -> {
|
||||
val mimeType = request.mimeType
|
||||
|
||||
method = HttpMethod.Post
|
||||
body = MultiPartFormDataContent(formData {
|
||||
appendInput(
|
||||
FILE_FIELD,
|
||||
Headers.build {
|
||||
append(HttpHeaders.ContentType, mimeType.toString())
|
||||
|
||||
val fakeFilename = "filename=file" + when (mimeType) {
|
||||
ContentType.Image.GIF -> ".gif"
|
||||
ContentType.Image.JPEG -> ".jpeg"
|
||||
ContentType.Image.PNG -> ".png"
|
||||
ContentType.Image.SVG -> ".svg"
|
||||
else -> throw IllegalArgumentException(
|
||||
"Currently supported formats for uploading in sauce: gif, jpeg, png, svg"
|
||||
)
|
||||
}
|
||||
append(HttpHeaders.ContentDisposition, "filename=$fakeFilename")
|
||||
},
|
||||
block = request::input
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.Input
|
||||
|
||||
internal sealed class SauceRequestSubject
|
||||
|
||||
internal data class UrlSauceRequestSubject(val url: String) : SauceRequestSubject()
|
||||
|
||||
internal data class InputRequestSubject(val input: Input, val mimeType: ContentType) : SauceRequestSubject()
|
||||
|
||||
internal val String.asSauceRequestSubject
|
||||
get() = UrlSauceRequestSubject(this)
|
||||
|
||||
internal fun Input.asSauceRequestSubject(mimeType: ContentType)
|
||||
= InputRequestSubject(this, mimeType)
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional
|
||||
|
||||
import com.soywiz.klock.TimeSpan
|
||||
|
||||
typealias AccountType = Int
|
||||
const val defaultAccountType: AccountType = 1 // "basic"
|
||||
|
||||
typealias UserId = Int
|
||||
|
||||
const val SHORT_TIME_RECALCULATING_MILLIS = 30 * 1000
|
||||
const val LONG_TIME_RECALCULATING_MILLIS = 24 * 60 * 60 * 1000
|
||||
val SHORT_TIME_RECALCULATING_MILLIS = TimeSpan(30.0 * 1000)
|
||||
val LONG_TIME_RECALCULATING_MILLIS = TimeSpan(24.0 * 60 * 60 * 1000)
|
||||
|
||||
@@ -1,13 +1,34 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.exceptions
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.soywiz.klock.TimeSpan
|
||||
import io.ktor.client.features.ClientRequestException
|
||||
import io.ktor.client.statement.readText
|
||||
import io.ktor.http.HttpStatusCode.Companion.TooManyRequests
|
||||
import kotlinx.io.IOException
|
||||
import io.ktor.utils.io.errors.IOException
|
||||
|
||||
val ClientRequestException.sauceNaoAPIException: Exception
|
||||
get() = when (response.status) {
|
||||
TooManyRequests -> TooManyRequestsException()
|
||||
else -> this
|
||||
internal suspend fun ClientRequestException.sauceNaoAPIException(): Exception {
|
||||
return when (response.status) {
|
||||
TooManyRequests -> {
|
||||
val answerContent = response.readText()
|
||||
when {
|
||||
answerContent.contains("daily limit") -> TooManyRequestsLongException(answerContent)
|
||||
else -> TooManyRequestsShortException(answerContent)
|
||||
}
|
||||
}
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
class TooManyRequestsException : IOException()
|
||||
sealed class TooManyRequestsException : IOException() {
|
||||
abstract val answerContent: String
|
||||
abstract val waitTime: TimeSpan
|
||||
}
|
||||
|
||||
class TooManyRequestsShortException(override val answerContent: String) : TooManyRequestsException() {
|
||||
override val waitTime: TimeSpan = SHORT_TIME_RECALCULATING_MILLIS
|
||||
}
|
||||
class TooManyRequestsLongException(override val answerContent: String) : TooManyRequestsException() {
|
||||
override val waitTime: TimeSpan = LONG_TIME_RECALCULATING_MILLIS
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.JsonObjectSerializer
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.StringDescriptor
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObjectSerializer
|
||||
|
||||
@Serializable
|
||||
data class Header(
|
||||
@@ -38,11 +38,11 @@ data class Header(
|
||||
val userId: Int? = null
|
||||
)
|
||||
|
||||
object IndexesSerializer : KSerializer<List<HeaderIndex?>> {
|
||||
internal object IndexesSerializer : KSerializer<List<HeaderIndex?>> {
|
||||
override val descriptor: SerialDescriptor = StringDescriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): List<HeaderIndex?> {
|
||||
val json = decoder.decodeSerializableValue(JsonObjectSerializer)
|
||||
val json = JsonObjectSerializer.deserialize(decoder)
|
||||
val parsed = json.keys.mapNotNull { it.toIntOrNull() }.sorted().mapNotNull {
|
||||
val jsonObject = json.getObjectOrNull(it.toString()) ?: return@mapNotNull null
|
||||
val index = Json.nonstrict.parse(HeaderIndex.serializer(), Json.stringify(JsonObjectSerializer, jsonObject))
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class LimitsState(
|
||||
val maxShortQuota: Int,
|
||||
val maxLongQuota: Int,
|
||||
val knownShortQuota: Int,
|
||||
val knownLongQuota: Int
|
||||
)
|
||||
@@ -1,29 +1,33 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.ArrayListSerializer
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
@Serializable(SauceNaoAnswerSerializer::class)
|
||||
data class SauceNaoAnswer(
|
||||
@Serializable
|
||||
data class SauceNaoAnswer internal constructor(
|
||||
val header: Header,
|
||||
val results: List<Result> = emptyList(),
|
||||
val raw: JsonObject
|
||||
val raw: JsonObject = JsonObject(emptyMap())
|
||||
)
|
||||
|
||||
@Serializer(SauceNaoAnswer::class)
|
||||
object SauceNaoAnswerSerializer : KSerializer<SauceNaoAnswer> {
|
||||
private val resultsSerializer = ArrayListSerializer(Result.serializer())
|
||||
private val resultsSerializer = ListSerializer(Result.serializer())
|
||||
private const val headerField = "header"
|
||||
private const val resultsField = "results"
|
||||
private val serializer = Json.nonstrict
|
||||
|
||||
override fun deserialize(decoder: Decoder): SauceNaoAnswer {
|
||||
val raw = JsonObjectSerializer.deserialize(decoder)
|
||||
val header = serializer.fromJson(Header.serializer(), raw.getObject(headerField))
|
||||
val results = serializer.fromJson(resultsSerializer, raw.getArray(resultsField))
|
||||
val stringRaw = serializer.stringify(JsonObjectSerializer, raw)
|
||||
|
||||
return SauceNaoAnswer(header, results, raw)
|
||||
return serializer.parse(
|
||||
SauceNaoAnswer.serializer(),
|
||||
stringRaw
|
||||
).copy(
|
||||
raw = raw
|
||||
)
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: SauceNaoAnswer) {
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.ArrayListSerializer
|
||||
import kotlinx.serialization.internal.StringSerializer
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
|
||||
|
||||
@Serializer(String::class)
|
||||
object CommonMultivariantStringSerializer : KSerializer<String> by StringSerializer {
|
||||
private val stringArraySerializer = ArrayListSerializer(StringSerializer)
|
||||
object CommonMultivariantStringSerializer : KSerializer<String> by String.serializer() {
|
||||
private val stringArraySerializer = ListSerializer(String.serializer())
|
||||
|
||||
override fun deserialize(decoder: Decoder): String {
|
||||
return try {
|
||||
decoder.decodeSerializableValue(StringSerializer)
|
||||
decoder.decodeSerializableValue(String.serializer())
|
||||
} catch (e: Exception) {
|
||||
decoder.decodeSerializableValue(stringArraySerializer).joinToString()
|
||||
}
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.*
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
|
||||
@Serializer(forClass = JsonElement::class)
|
||||
internal object JsonElementSerializer : KSerializer<JsonElement> {
|
||||
override val descriptor: SerialDescriptor = object : SerialClassDescImpl("JsonElementSerializer") {
|
||||
override val kind: SerialKind
|
||||
get() = UnionKind.SEALED
|
||||
|
||||
init {
|
||||
addElement("JsonElement")
|
||||
}
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonElement) {
|
||||
when (obj) {
|
||||
is JsonPrimitive -> JsonPrimitiveSerializer.serialize(encoder, obj)
|
||||
is JsonObject -> JsonObjectSerializer.serialize(encoder, obj)
|
||||
is JsonArray -> JsonArraySerializer.serialize(encoder, obj)
|
||||
}
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonElement {
|
||||
val input = decoder as? JsonInput ?: error("JsonElement is deserializable only when used by Json")
|
||||
return input.decodeJson()
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonPrimitive::class)
|
||||
internal object JsonPrimitiveSerializer : KSerializer<JsonPrimitive> {
|
||||
override val descriptor: SerialDescriptor =
|
||||
JsonPrimitiveDescriptor
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonPrimitive) {
|
||||
return if (obj is JsonNull) {
|
||||
JsonNullSerializer.serialize(encoder, JsonNull)
|
||||
} else {
|
||||
JsonLiteralSerializer.serialize(encoder, obj as JsonLiteral)
|
||||
}
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonPrimitive {
|
||||
return if (decoder.decodeNotNullMark()) JsonPrimitive(decoder.decodeString())
|
||||
else JsonNullSerializer.deserialize(decoder)
|
||||
}
|
||||
|
||||
private object JsonPrimitiveDescriptor : SerialClassDescImpl("JsonPrimitive") {
|
||||
override val kind: SerialKind
|
||||
get() = PrimitiveKind.STRING
|
||||
|
||||
override val isNullable: Boolean
|
||||
get() = true
|
||||
|
||||
init {
|
||||
JsonPrimitiveSerializer.JsonPrimitiveDescriptor.addElement("JsonPrimitive")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonNull::class)
|
||||
internal object JsonNullSerializer : KSerializer<JsonNull> {
|
||||
override val descriptor: SerialDescriptor =
|
||||
JsonNullDescriptor
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonNull) {
|
||||
encoder.encodeNull()
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonNull {
|
||||
decoder.decodeNull()
|
||||
return JsonNull
|
||||
}
|
||||
|
||||
private object JsonNullDescriptor : SerialClassDescImpl("JsonNull") {
|
||||
override val kind: SerialKind
|
||||
get() = UnionKind.OBJECT
|
||||
|
||||
override val isNullable: Boolean
|
||||
get() = true
|
||||
|
||||
init {
|
||||
JsonNullSerializer.JsonNullDescriptor.addElement("JsonNull")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonLiteral::class)
|
||||
internal object JsonLiteralSerializer : KSerializer<JsonLiteral> {
|
||||
|
||||
override val descriptor: SerialDescriptor =
|
||||
JsonLiteralDescriptor
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonLiteral) {
|
||||
if (obj.isString) {
|
||||
return encoder.encodeString(obj.content)
|
||||
}
|
||||
|
||||
val integer = obj.intOrNull
|
||||
if (integer != null) {
|
||||
return encoder.encodeInt(integer)
|
||||
}
|
||||
|
||||
val double = obj.doubleOrNull
|
||||
if (double != null) {
|
||||
return encoder.encodeDouble(double)
|
||||
}
|
||||
|
||||
val boolean = obj.booleanOrNull
|
||||
if (boolean != null) {
|
||||
return encoder.encodeBoolean(boolean)
|
||||
}
|
||||
|
||||
encoder.encodeString(obj.content)
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonLiteral {
|
||||
return JsonLiteral(decoder.decodeString())
|
||||
}
|
||||
|
||||
private object JsonLiteralDescriptor : SerialClassDescImpl("JsonLiteral") {
|
||||
override val kind: SerialKind
|
||||
get() = PrimitiveKind.STRING
|
||||
|
||||
init {
|
||||
JsonLiteralSerializer.JsonLiteralDescriptor.addElement("JsonLiteral")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonObject::class)
|
||||
internal object JsonObjectSerializer : KSerializer<JsonObject> {
|
||||
override val descriptor: SerialDescriptor =
|
||||
NamedMapClassDescriptor("JsonObject", StringSerializer.descriptor,
|
||||
JsonElementSerializer.descriptor)
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonObject) {
|
||||
LinkedHashMapSerializer(StringSerializer, JsonElementSerializer).serialize(encoder, obj.content)
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonObject {
|
||||
return JsonObject(LinkedHashMapSerializer(StringSerializer, JsonElementSerializer).deserialize(decoder))
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonArray::class)
|
||||
internal object JsonArraySerializer : KSerializer<JsonArray> {
|
||||
|
||||
override val descriptor: SerialDescriptor = NamedListClassDescriptor("JsonArray",
|
||||
JsonElementSerializer.descriptor)
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonArray) {
|
||||
ArrayListSerializer(JsonElementSerializer).serialize(encoder, obj)
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonArray {
|
||||
return JsonArray(ArrayListSerializer(JsonElementSerializer).deserialize(decoder))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.util.asStream
|
||||
import io.ktor.utils.io.core.Input
|
||||
import java.io.InputStream
|
||||
import java.net.URLConnection
|
||||
|
||||
val InputStream.mimeType: ContentType
|
||||
get() {
|
||||
val contentType = URLConnection.guessContentTypeFromStream(this)
|
||||
return ContentType.parse(contentType)
|
||||
}
|
||||
|
||||
val Input.mimeType: ContentType
|
||||
get() = asStream().mimeType
|
||||
@@ -2,23 +2,34 @@ package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsLongException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.LimitsState
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.io.core.Closeable
|
||||
import org.joda.time.DateTime
|
||||
import java.io.Closeable
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
class RequestQuotaManager (
|
||||
private val scope: CoroutineScope
|
||||
internal class RequestQuotaManager (
|
||||
scope: CoroutineScope
|
||||
) : Closeable {
|
||||
private var longQuota = 1
|
||||
private var shortQuota = 1
|
||||
private var longMaxQuota = 1
|
||||
private var shortMaxQuota = 1
|
||||
|
||||
val limitsState: LimitsState
|
||||
get() = LimitsState(
|
||||
shortMaxQuota,
|
||||
longMaxQuota,
|
||||
shortQuota,
|
||||
longQuota
|
||||
)
|
||||
|
||||
private val quotaActions = Channel<suspend () -> Unit>(Channel.UNLIMITED)
|
||||
|
||||
private val quotaJob = scope.launch {
|
||||
@@ -43,11 +54,11 @@ class RequestQuotaManager (
|
||||
shortQuota = min(newShortQuota, shortMaxQuota)
|
||||
|
||||
when {
|
||||
longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()).millis + LONG_TIME_RECALCULATING_MILLIS
|
||||
shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()).millis + SHORT_TIME_RECALCULATING_MILLIS
|
||||
longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()) + LONG_TIME_RECALCULATING_MILLIS
|
||||
shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()) + SHORT_TIME_RECALCULATING_MILLIS
|
||||
else -> null
|
||||
} ?.also {
|
||||
delay(it - DateTime.now().millis)
|
||||
delay((it - DateTime.now()).millisecondsLong)
|
||||
shortQuota = max(shortQuota, 1)
|
||||
longQuota = max(longQuota, 1)
|
||||
}
|
||||
@@ -64,8 +75,8 @@ class RequestQuotaManager (
|
||||
timeManager
|
||||
)
|
||||
|
||||
suspend fun happenTooManyRequests(timeManager: TimeManager) = updateQuota(
|
||||
1,
|
||||
suspend fun happenTooManyRequests(timeManager: TimeManager, e: TooManyRequestsException) = updateQuota(
|
||||
if (e is TooManyRequestsLongException) 0 else 1,
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
|
||||
@@ -3,7 +3,8 @@ package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import org.joda.time.DateTime
|
||||
import com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.TimeSpan
|
||||
|
||||
internal suspend fun calculateSleepTime(
|
||||
header: Header,
|
||||
@@ -11,8 +12,8 @@ internal suspend fun calculateSleepTime(
|
||||
mostOldestInLongPeriodGetter: suspend () -> DateTime?
|
||||
): DateTime? {
|
||||
return when {
|
||||
header.longRemaining < 1 -> mostOldestInLongPeriodGetter() ?.plusMillis(LONG_TIME_RECALCULATING_MILLIS)
|
||||
header.shortRemaining < 1 -> mostOldestInShortPeriodGetter() ?.plusMillis(SHORT_TIME_RECALCULATING_MILLIS)
|
||||
header.longRemaining < 1 -> mostOldestInLongPeriodGetter() ?.plus(LONG_TIME_RECALCULATING_MILLIS)
|
||||
header.shortRemaining < 1 -> mostOldestInShortPeriodGetter() ?.plus(SHORT_TIME_RECALCULATING_MILLIS)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,17 @@ package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.TimeSpan
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.io.core.Closeable
|
||||
import org.joda.time.DateTime
|
||||
import java.io.Closeable
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
private fun MutableList<DateTime>.clearTooOldTimes(relatedTo: DateTime = DateTime.now()) {
|
||||
val limitValue = relatedTo.minusMillis(LONG_TIME_RECALCULATING_MILLIS)
|
||||
val limitValue = relatedTo - LONG_TIME_RECALCULATING_MILLIS
|
||||
|
||||
removeAll {
|
||||
it < limitValue
|
||||
@@ -55,7 +56,7 @@ private data class TimeManagerMostOldestInShortGetter(
|
||||
|
||||
val now = DateTime.now()
|
||||
|
||||
val limitTime = now.minusMillis(SHORT_TIME_RECALCULATING_MILLIS)
|
||||
val limitTime = now - SHORT_TIME_RECALCULATING_MILLIS
|
||||
|
||||
continuation.resumeWith(
|
||||
Result.success(
|
||||
@@ -67,7 +68,7 @@ private data class TimeManagerMostOldestInShortGetter(
|
||||
}
|
||||
}
|
||||
|
||||
class TimeManager(
|
||||
internal class TimeManager(
|
||||
scope: CoroutineScope
|
||||
) : Closeable {
|
||||
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)
|
||||
|
||||
Reference in New Issue
Block a user