diff --git a/CHANGELOG.md b/CHANGELOG.md index b4fbddb..8d30c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # SauceNaoAPI Changelog +### 0.6.0 + +**MAIN PACKAGE WAS CHANGED: `com.github.insanusmokrassar` -> `com.insanusmokrassar`** + +* All known fields were added to `ResultData` +* Versions updates: + * `Kotlin`: `1.3.72` -> `1.4.0` + * `Coroutines`: `1.3.8` -> `1.3.9` + * `Serialization`: `0.20.0` -> `1.0.0-RC` + * `Klock`: `1.11.14` -> `1.12.0` + * `Ktor`: `1.3.2` -> `1.4.0` + ## 0.5.0 * Versions updates diff --git a/build.gradle b/build.gradle index c7ffeb4..1966924 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,3 @@ -project.version = "0.5.0" -project.group = "com.github.insanusmokrassar" - buildscript { repositories { mavenLocal() @@ -15,8 +12,13 @@ buildscript { } } -apply plugin: 'kotlin' -apply plugin: 'kotlinx-serialization' +plugins { + id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" + id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" +} + +project.version = "0.6.0" +project.group = "com.insanusmokrassar" apply from: "publish.gradle" @@ -25,17 +27,48 @@ repositories { jcenter() mavenCentral() maven { url "https://kotlin.bintray.com/kotlinx" } - maven { url "https://dl.bintray.com/kotlin/ktor" } } -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 "com.soywiz.korlibs.klock:klock:$klock_version" - implementation "io.ktor:ktor-client-core:$ktor_version" - implementation "io.ktor:ktor-client-okhttp:$ktor_version" +kotlin { + jvm() + js(BOTH) { + browser() + nodejs() + } - // Use JUnit test framework - testImplementation 'junit:junit:4.13' + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" + api "org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlin_serialisation_runtime_version" + api "com.soywiz.korlibs.klock:klock:$klock_version" + api "io.ktor:ktor-client-core:$ktor_version" + } + } + commonTest { + dependencies { + implementation kotlin('test-common') + implementation kotlin('test-annotations-common') + } + } + jvmMain { + dependencies { + implementation kotlin('stdlib') + } + } + jvmTest { + dependencies { + implementation kotlin('test-junit') + implementation "io.ktor:ktor-client-okhttp:$ktor_version" + } + } + jsMain { + dependencies { + implementation kotlin('test-js') + } + } + } } diff --git a/gradle.properties b/gradle.properties index 2cf7165..ce49cf7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,8 @@ kotlin.code.style=official -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 +kotlin_version=1.4.0 +kotlin_coroutines_version=1.3.9 +kotlin_serialisation_runtime_version=1.0.0-RC +klock_version=1.12.0 +ktor_version=1.4.0 -project_public_name=SauceNao API -project_public_description=SauceNao API library - -gradle_bintray_plugin_version=1.8.4 +gradle_bintray_plugin_version=1.8.5 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3d7bd11..17b99ce 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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-6.5.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip diff --git a/maven.publish.gradle b/maven.publish.gradle index 042777a..2acca31 100644 --- a/maven.publish.gradle +++ b/maven.publish.gradle @@ -1,63 +1,53 @@ apply plugin: 'maven-publish' -apply plugin: 'signing' -task sourcesJar(type: Jar) { - from sourceSets.main.allSource - classifier = 'sources' -} - -task javadocJar(type: Jar) { - from javadoc +task javadocsJar(type: Jar) { classifier = 'javadoc' } -publishing { - publications { - maven(MavenPublication) { - from components.java - groupId "${project.group}" - artifactId "${project.name}" - version "${project.version}" - - artifact sourcesJar - artifact javadocJar - - pom.withXml { - asNode().children().last() + { - resolveStrategy = Closure.DELEGATE_FIRST - name "${project_public_name}" - description "${project_public_description}" - url "https://insanusmokrassar.github.io/${project.name}" - - scm { - connection "scm:git:git://github.com/insanusmokrassar/${project.name}.git" - developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/${project.name}.git[push=]ssh:git@github.com:insanusmokrassar/${project.name}.git" - url "https://github.com/insanusmokrassar/${project.name}" - - } - - developers { - developer { - id "InsanusMokrassar" - name "Ovsyannikov Alexey" - email "ovsyannikov.alexey95@gmail.com" - } - } - - licenses { - license { - name 'The Apache Software License, Version 2.0' - url 'https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE' - distribution 'repo' - } - } - } - } +afterEvaluate { + project.publishing.publications.all { + // rename artifacts + groupId "${project.group}" + if (it.name.contains('kotlinMultiplatform')) { + artifactId = "${project.name}" + } else { + artifactId = "${project.name}-$name" } } } -signing { - useGpgCmd() - sign publishing.publications.maven -} +publishing { + publications.all { + artifact javadocsJar + + pom { + description = "SauceNao API library" + name = "SauceNao API" + url = "https://insanusmokrassar.github.io/${project.name}" + + scm { + developerConnection = "scm:git:[fetch=]https://github.com/insanusmokrassar/${project.name}.git[push=]https://github.com/insanusmokrassar/${project.name}.git" + url = "https://github.com/insanusmokrassar/${project.name}.git" + } + + developers { + + developer { + id = "InsanusMokrassar" + name = "Ovsyannikov Alexey" + email = "ovsyannikov.alexey95@gmail.com" + } + + } + + licenses { + + license { + name = "Apache Software License 2.0" + url = "https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE" + } + + } + } + } +} \ No newline at end of file diff --git a/publication.kpsb b/publication.kpsb new file mode 100644 index 0000000..82d5284 --- /dev/null +++ b/publication.kpsb @@ -0,0 +1 @@ +{"bintrayConfig":{"repo":"InsanusMokrassar","packageName":"${project.name}-mpp","packageVcs":"https://github.com/InsanusMokrassar/${project.name}"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"SauceNao API","description":"SauceNao API library","url":"https://insanusmokrassar.github.io/${project.name}","vcsUrl":"https://github.com/insanusmokrassar/${project.name}.git","developers":[{"id":"InsanusMokrassar","name":"Ovsyannikov Alexey","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/publish.gradle b/publish.gradle index 6e8de12..65530e9 100644 --- a/publish.gradle +++ b/publish.gradle @@ -1,33 +1,55 @@ apply plugin: 'com.jfrog.bintray' -ext { - projectBintrayDir = "${project.group}/".replace(".", "/") + "${project.name}/${project.version}" -} +apply from: "maven.publish.gradle" bintray { user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER') key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY') - publications = ["maven"] filesSpec { - into "$projectBintrayDir" - from("build/libs") { - include "**/*.asc" - } - from("build/publications/maven") { - rename 'pom-default.xml(.*)', "${project.name}-${project.version}.pom\$1" + from "${buildDir}/publications/" + eachFile { + String directorySubname = it.getFile().parentFile.name + if (it.getName() == "module.json") { + if (directorySubname == "kotlinMultiplatform") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module") + } else { + it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module") + } + } else { + if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom") + } else { + it.exclude() + } + } } + into "${project.group}".replace(".", "/") } pkg { - repo = 'StandardRepository' - name = "${project.name}" + repo = "InsanusMokrassar" + name = "${project.name}-mpp" vcsUrl = "https://github.com/InsanusMokrassar/${project.name}" - licenses = ['Apache-2.0'] + licenses = ["Apache-2.0"] version { name = "${project.version}" released = new Date() - vcsTag = name + vcsTag = "${project.version}" + gpg { + sign = true + passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase') + } } } } -apply from: "maven.publish.gradle" +bintrayUpload.doFirst { + publications = publishing.publications.collect { + if (it.name.contains('kotlinMultiplatform')) { + null + } else { + it.name + } + } - null +} + +bintrayUpload.dependsOn publishToMavenLocal \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/OutputType.kt similarity index 85% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/OutputType.kt index 841c06f..77bb84d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/OutputType.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI +package com.insanusmokrassar.SauceNaoAPI sealed class OutputType { abstract val typeCode: Int diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt similarity index 87% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt index 2ff1e3b..3b776e8 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt @@ -1,15 +1,14 @@ -package com.github.insanusmokrassar.SauceNaoAPI +package com.insanusmokrassar.SauceNaoAPI -import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException -import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException -import com.github.insanusmokrassar.SauceNaoAPI.models.* -import com.github.insanusmokrassar.SauceNaoAPI.utils.* +import com.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException +import com.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException +import com.insanusmokrassar.SauceNaoAPI.models.* +import com.insanusmokrassar.SauceNaoAPI.utils.* 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.request.forms.* +import io.ktor.client.request.forms.MultiPartFormDataContent +import io.ktor.client.request.forms.formData import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.readText import io.ktor.http.* @@ -17,9 +16,6 @@ import io.ktor.utils.io.core.Input import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import kotlinx.serialization.json.Json -import java.io.Closeable -import java.util.logging.Logger -import kotlin.Result import kotlin.coroutines.* private const val API_TOKEN_FIELD = "api_key" @@ -35,15 +31,21 @@ private const val MINIMAL_SIMILARITY_FIELD = "minsim" private const val SEARCH_URL = "https://saucenao.com/search.php" +val defaultSauceNaoParser = Json { + allowSpecialFloatingPointValues = true + allowStructuredMapKeys = true + ignoreUnknownKeys = true + useArrayPolymorphism = true +} + data class SauceNaoAPI( private val apiToken: String? = null, private val outputType: OutputType = JsonOutputType, - private val client: HttpClient = HttpClient(OkHttp), + private val client: HttpClient = HttpClient(), private val searchUrl: String = SEARCH_URL, - private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : Closeable { - private val logger = Logger.getLogger("SauceNaoAPI") - + private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + private val parser: Json = defaultSauceNaoParser +) : SauceCloseable { private val requestsChannel = Channel, HttpRequestBuilder>>(Channel.UNLIMITED) private val timeManager = TimeManager(scope) private val quotaManager = RequestQuotaManager(scope) @@ -57,16 +59,15 @@ data class SauceNaoAPI( launch { try { val answer = makeRequest(requestBuilder) - callback.resumeWith(Result.success(answer)) + callback.resume(answer) quotaManager.updateQuota(answer.header, timeManager) } catch (e: TooManyRequestsException) { - logger.warning("Exceed time limit. Answer was:\n${e.answerContent}") quotaManager.happenTooManyRequests(timeManager, e) requestsChannel.send(callback to requestBuilder) } catch (e: Exception) { try { - callback.resumeWith(Result.failure(e)) + callback.resumeWithException(e) } catch (e: IllegalStateException) { // may happen when already resumed and api was closed // do nothing } @@ -87,7 +88,7 @@ data class SauceNaoAPI( suspend fun request( mediaInput: Input, - mimeType: ContentType = mediaInput.mimeType, + mimeType: ContentType, resultsCount: Int? = null, minSimilarity: Float? = null ): SauceNaoAnswer? = makeRequest( @@ -138,9 +139,8 @@ data class SauceNaoAPI( return try { val call = client.request(builder) val answerText = call.readText() - logger.info(answerText) timeManager.addTimeAndClear() - Json.nonstrict.parse( + parser.decodeFromString( SauceNaoAnswerSerializer, answerText ) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceRequestSubject.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/SauceRequestSubject.kt similarity index 91% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceRequestSubject.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/SauceRequestSubject.kt index 573473e..680e802 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceRequestSubject.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/SauceRequestSubject.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI +package com.insanusmokrassar.SauceNaoAPI import io.ktor.http.ContentType import io.ktor.utils.io.core.Input diff --git a/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/AdaptedAnswer.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/AdaptedAnswer.kt new file mode 100644 index 0000000..5a76ab0 --- /dev/null +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/AdaptedAnswer.kt @@ -0,0 +1,23 @@ +package com.insanusmokrassar.SauceNaoAPI.additional + +import com.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo +import com.insanusmokrassar.SauceNaoAPI.additional.header.adapted +import com.insanusmokrassar.SauceNaoAPI.additional.results.AdaptedResult +import com.insanusmokrassar.SauceNaoAPI.additional.results.adapted +import com.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer + +val SauceNaoAnswer.adapted: AdaptedAnswer + get() = header.adapted.let { resultMetainfo -> + val adaptedResults = results.map { + it.adapted(resultMetainfo) + } + AdaptedAnswer( + resultMetainfo, + adaptedResults + ) + } + +data class AdaptedAnswer( + val resultMetaInfo: ResultMetaInfo, + val results: List +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/Utils.kt similarity index 82% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/Utils.kt index 7873582..e30b422 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/Utils.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/Utils.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional +package com.insanusmokrassar.SauceNaoAPI.additional import com.soywiz.klock.TimeSpan diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/AccountInfo.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/AccountInfo.kt similarity index 80% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/AccountInfo.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/AccountInfo.kt index fce54ac..4dc30da 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/AccountInfo.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/AccountInfo.kt @@ -1,7 +1,7 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional.header +package com.insanusmokrassar.SauceNaoAPI.additional.header -import com.github.insanusmokrassar.SauceNaoAPI.additional.* -import com.github.insanusmokrassar.SauceNaoAPI.models.Header +import com.insanusmokrassar.SauceNaoAPI.additional.* +import com.insanusmokrassar.SauceNaoAPI.models.Header val Header.shortLimitStatus: LimitStatus get() = LimitStatus( diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/IndexInfo.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/IndexInfo.kt similarity index 79% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/IndexInfo.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/IndexInfo.kt index 109939f..574e43a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/IndexInfo.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/IndexInfo.kt @@ -1,6 +1,6 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional.header +package com.insanusmokrassar.SauceNaoAPI.additional.header -import com.github.insanusmokrassar.SauceNaoAPI.models.Header +import com.insanusmokrassar.SauceNaoAPI.models.Header data class IndexInfo( val id: Int, diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/QueryResultsMetainfo.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/QueryResultsMetainfo.kt similarity index 87% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/QueryResultsMetainfo.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/QueryResultsMetainfo.kt index 69e3c8f..14a4219 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/QueryResultsMetainfo.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/QueryResultsMetainfo.kt @@ -1,6 +1,6 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional.header +package com.insanusmokrassar.SauceNaoAPI.additional.header -import com.github.insanusmokrassar.SauceNaoAPI.models.Header +import com.insanusmokrassar.SauceNaoAPI.models.Header val Header.queryPreview get() = QueryResultPreview( diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/ResultMetaInfo.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/ResultMetaInfo.kt similarity index 65% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/ResultMetaInfo.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/ResultMetaInfo.kt index 1e77a32..519335d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/header/ResultMetaInfo.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/header/ResultMetaInfo.kt @@ -1,6 +1,6 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional.header +package com.insanusmokrassar.SauceNaoAPI.additional.header -import com.github.insanusmokrassar.SauceNaoAPI.models.Header +import com.insanusmokrassar.SauceNaoAPI.models.Header data class ResultMetaInfo( val accountInfo: AccountInfo = AccountInfo(), diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/results/AdaptedResult.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/results/AdaptedResult.kt similarity index 52% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/results/AdaptedResult.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/results/AdaptedResult.kt index 2c16a44..f4ac074 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/results/AdaptedResult.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/results/AdaptedResult.kt @@ -1,9 +1,9 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional.results +package com.insanusmokrassar.SauceNaoAPI.additional.results -import com.github.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo -import com.github.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo -import com.github.insanusmokrassar.SauceNaoAPI.models.Result -import com.github.insanusmokrassar.SauceNaoAPI.models.ResultData +import com.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo +import com.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo +import com.insanusmokrassar.SauceNaoAPI.models.Result +import com.insanusmokrassar.SauceNaoAPI.models.ResultData fun Result.adapted( resultMetaInfo: ResultMetaInfo diff --git a/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/results/ResultHeader.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/results/ResultHeader.kt new file mode 100644 index 0000000..68d4770 --- /dev/null +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/additional/results/ResultHeader.kt @@ -0,0 +1,9 @@ +package com.insanusmokrassar.SauceNaoAPI.additional.results + +import com.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo + +data class ResultHeader( + val similarity: Float, + val thumbnail: String, + val index: IndexInfo +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt similarity index 66% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt index 6e450d8..8356d2a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt @@ -1,7 +1,7 @@ -package com.github.insanusmokrassar.SauceNaoAPI.exceptions +package com.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.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS +import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS import com.soywiz.klock.TimeSpan import io.ktor.client.features.ClientRequestException import io.ktor.client.statement.readText @@ -9,6 +9,7 @@ import io.ktor.http.HttpStatusCode.Companion.TooManyRequests import io.ktor.utils.io.errors.IOException internal suspend fun ClientRequestException.sauceNaoAPIException(): Exception { + val response = response ?: return this return when (response.status) { TooManyRequests -> { val answerContent = response.readText() @@ -21,14 +22,14 @@ internal suspend fun ClientRequestException.sauceNaoAPIException(): Exception { } } -sealed class TooManyRequestsException : IOException() { +sealed class TooManyRequestsException(message: String, cause: Throwable? = null) : IOException(message, cause) { abstract val answerContent: String abstract val waitTime: TimeSpan } -class TooManyRequestsShortException(override val answerContent: String) : TooManyRequestsException() { +class TooManyRequestsShortException(override val answerContent: String) : TooManyRequestsException("Too many requests were sent in the short period") { override val waitTime: TimeSpan = SHORT_TIME_RECALCULATING_MILLIS } -class TooManyRequestsLongException(override val answerContent: String) : TooManyRequestsException() { +class TooManyRequestsLongException(override val answerContent: String) : TooManyRequestsException("Too many requests were sent in the long period") { override val waitTime: TimeSpan = LONG_TIME_RECALCULATING_MILLIS } diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Header.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/Header.kt similarity index 60% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Header.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/Header.kt index 5c20969..53ad52f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Header.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/Header.kt @@ -1,9 +1,13 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models +package com.insanusmokrassar.SauceNaoAPI.models +import com.insanusmokrassar.SauceNaoAPI.defaultSauceNaoParser import kotlinx.serialization.* -import kotlinx.serialization.internal.StringDescriptor -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObjectSerializer +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.jsonObject @Serializable data class Header( @@ -39,21 +43,24 @@ data class Header( ) internal object IndexesSerializer : KSerializer> { - override val descriptor: SerialDescriptor = StringDescriptor + override val descriptor: SerialDescriptor = String.serializer().descriptor override fun deserialize(decoder: Decoder): List { - val json = JsonObjectSerializer.deserialize(decoder) + val json = JsonObject.serializer().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)) + val jsonObject = json[it.toString()] ?.jsonObject ?: return@mapNotNull null + val index = defaultSauceNaoParser.decodeFromString( + HeaderIndex.serializer(), + defaultSauceNaoParser.encodeToString(JsonObject.serializer(), jsonObject) + ) it to index }.toMap() - return Array(parsed.keys.max() ?: 0) { + return Array(parsed.keys.maxOrNull() ?: 0) { parsed[it] }.toList() } - override fun serialize(encoder: Encoder, obj: List) { + override fun serialize(encoder: Encoder, value: List) { TODO() } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/HeaderIndex.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/HeaderIndex.kt similarity index 78% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/HeaderIndex.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/HeaderIndex.kt index c34b0b7..359dac9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/HeaderIndex.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/HeaderIndex.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models +package com.insanusmokrassar.SauceNaoAPI.models import kotlinx.serialization.Serializable diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/LimitsState.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/LimitsState.kt similarity index 78% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/LimitsState.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/LimitsState.kt index 6b27024..ffe6e7a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/LimitsState.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/LimitsState.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models +package com.insanusmokrassar.SauceNaoAPI.models import kotlinx.serialization.Serializable diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Result.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/Result.kt similarity index 70% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Result.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/Result.kt index b2a1c93..5444b55 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Result.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/Result.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models +package com.insanusmokrassar.SauceNaoAPI.models import kotlinx.serialization.Serializable diff --git a/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/ResultData.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/ResultData.kt new file mode 100644 index 0000000..c247923 --- /dev/null +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/ResultData.kt @@ -0,0 +1,161 @@ +package com.insanusmokrassar.SauceNaoAPI.models + +import com.insanusmokrassar.SauceNaoAPI.utils.CommonMultivariantStringSerializer +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ResultData( + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("danbooru_id") + val danbooruId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("gelbooru_id") + val gelbooruId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("drawr_id") + val drawrId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("pixiv_id") + val pixivId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("seiga_id") + val seigaId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("yandere_id") + val yandereId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("konachan_id") + val konachanId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("sankaku_id") + val sankakuId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("anime-pictures_id") + val animePicturesId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("e621_id") + val e621Id: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("idol_id") + val idolId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("imdb_id") + val imdbId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("anidb_aid") + val anidbAId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("bcy_id") + val bcyId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("ddb_id") + val ddbId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("nijie_id") + val nijieId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("getchu_id") + val getchuId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("shutterstock_id") + val shutterstockId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("contributor_id") + val contributorId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("est_time") + val estTime: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("bcy_type") + val bcyType: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("da_id") + val daId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("pg_id") + val pgId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("mal_id") + val malId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("md_id") + val mdId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("mu_id") + val muId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("pawoo_id") + val pawooId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("pawoo_user_acct") + val pawooUserAcct: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("pawoo_user_username") + val pawooUserUsername: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("pawoo_user_display_name") + val pawooUserDisplayname: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val title: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("jp_title") + val titleJp: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("eng_title") + val titleEng: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("alt_titles") + val titleAlt: List = emptyList(), + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("jp_name") + val nameJp: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("eng_name") + val nameEng: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val creator: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val material: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("member_name") + val memberName: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("member_id") + val memberId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val part: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("part_name") + val partName: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val date: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val company: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val file: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val year: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("member_link_id") + val memberLinkId: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("author_name") + val authorName: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("author_url") + val authorUrl: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val characters: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val source: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val url: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + val type: String? = null, + @Serializable(CommonMultivariantStringSerializer::class) + @SerialName("created_at") + val createdAt: String? = null, + @SerialName("ext_urls") + val extUrls: List = emptyList() +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/ResultHeader.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/ResultHeader.kt similarity index 83% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/ResultHeader.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/ResultHeader.kt index 68e1a75..85c664f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/ResultHeader.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/ResultHeader.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models +package com.insanusmokrassar.SauceNaoAPI.models import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable diff --git a/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt new file mode 100644 index 0000000..dd24d8d --- /dev/null +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt @@ -0,0 +1,56 @@ +package com.insanusmokrassar.SauceNaoAPI.models + +import com.insanusmokrassar.SauceNaoAPI.defaultSauceNaoParser +import kotlinx.serialization.* +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.buildJsonObject + +@Serializable +private data class TemporalSauceNaoAnswerRepresentation( + val header: Header, + val results: List = emptyList(), +) + +@Serializable(SauceNaoAnswerSerializer::class) +data class SauceNaoAnswer internal constructor( + val header: Header, + val results: List = emptyList(), + val raw: JsonObject = JsonObject(emptyMap()) +) + +@Serializer(SauceNaoAnswer::class) +object SauceNaoAnswerSerializer : KSerializer { + private val resultsSerializer = ListSerializer(Result.serializer()) + private const val headerField = "header" + private const val resultsField = "results" + private val serializer = defaultSauceNaoParser + + override fun deserialize(decoder: Decoder): SauceNaoAnswer { + val raw = JsonObject.serializer().deserialize(decoder) + + return serializer.decodeFromJsonElement( + TemporalSauceNaoAnswerRepresentation.serializer(), + raw + ).let { + SauceNaoAnswer( + it.header, + it.results, + raw + ) + } + } + + override fun serialize(encoder: Encoder, value: SauceNaoAnswer) { + val resultObject = buildJsonObject { + value.raw.forEach { + put(it.key, it.value) + } + put(headerField, serializer.encodeToJsonElement(Header.serializer(), value.header)) + put(resultsField, serializer.encodeToJsonElement(resultsSerializer, value.results)) + } + JsonObject.serializer().serialize(encoder, resultObject) + } +} diff --git a/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/CommonMultivariantStringSerializer.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/CommonMultivariantStringSerializer.kt new file mode 100644 index 0000000..a531c0d --- /dev/null +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/CommonMultivariantStringSerializer.kt @@ -0,0 +1,22 @@ +package com.insanusmokrassar.SauceNaoAPI.utils + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializer +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.json.* + + +@Serializer(String::class) +object CommonMultivariantStringSerializer : KSerializer by String.serializer() { + private val stringArraySerializer = ListSerializer(String.serializer()) + + override fun deserialize(decoder: Decoder): String { + return when (val parsed = JsonElement.serializer().deserialize(decoder)) { + is JsonPrimitive -> parsed.content + is JsonArray -> parsed.joinToString { it.jsonPrimitive.content } + else -> error("Unexpected answer object has been received: $parsed") + } + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt similarity index 83% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt index 5ce190b..4542f9b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt @@ -1,22 +1,21 @@ -package com.github.insanusmokrassar.SauceNaoAPI.utils +package com.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.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS +import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS +import com.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException +import com.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsLongException +import com.insanusmokrassar.SauceNaoAPI.models.Header +import com.insanusmokrassar.SauceNaoAPI.models.LimitsState import com.soywiz.klock.DateTime import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel -import java.io.Closeable import kotlin.coroutines.suspendCoroutine import kotlin.math.max import kotlin.math.min internal class RequestQuotaManager ( scope: CoroutineScope -) : Closeable { +) : SauceCloseable { private var longQuota = 1 private var shortQuota = 1 private var longMaxQuota = 1 diff --git a/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/SauceCloseable.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/SauceCloseable.kt new file mode 100644 index 0000000..87791fa --- /dev/null +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/SauceCloseable.kt @@ -0,0 +1,21 @@ +package com.insanusmokrassar.SauceNaoAPI.utils + +import kotlinx.coroutines.supervisorScope + +interface SauceCloseable { + fun close() +} + +fun SauceCloseable.use(block: (SauceCloseable) -> T): T = try { + block(this) +} finally { + close() +} + +suspend fun SauceCloseable.useSafe(block: suspend (SauceCloseable) -> T): T = try { + supervisorScope { + block(this@useSafe) + } +} finally { + close() +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt similarity index 59% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt index 88a1462..f279599 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/SleepCalculations.kt @@ -1,10 +1,9 @@ -package com.github.insanusmokrassar.SauceNaoAPI.utils +package com.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 com.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS +import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS +import com.insanusmokrassar.SauceNaoAPI.models.Header import com.soywiz.klock.DateTime -import com.soywiz.klock.TimeSpan internal suspend fun calculateSleepTime( header: Header, diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt similarity index 87% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt rename to src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt index 2b1d327..c6a7a48 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt @@ -1,13 +1,11 @@ -package com.github.insanusmokrassar.SauceNaoAPI.utils +package com.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.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS +import com.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 java.io.Closeable import kotlin.coroutines.Continuation import kotlin.coroutines.suspendCoroutine @@ -44,7 +42,7 @@ private data class TimeManagerMostOldestInLongGetter( ) : TimeManagerAction { override suspend fun makeChangeWith(times: MutableList) { times.clearTooOldTimes() - continuation.resumeWith(Result.success(times.min())) + continuation.resumeWith(Result.success(times.minOrNull())) } } @@ -62,7 +60,7 @@ private data class TimeManagerMostOldestInShortGetter( Result.success( times.asSequence().filter { limitTime < it - }.min() + }.minOrNull() ) ) } @@ -70,7 +68,7 @@ private data class TimeManagerMostOldestInShortGetter( internal class TimeManager( scope: CoroutineScope -) : Closeable { +) : SauceCloseable { private val actionsChannel = Channel(Channel.UNLIMITED) private val timeUpdateJob = scope.launch { diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt b/src/jvmTest/kotlin/Launcher.kt similarity index 85% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt rename to src/jvmTest/kotlin/Launcher.kt index 0de3855..5fef72f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt +++ b/src/jvmTest/kotlin/Launcher.kt @@ -1,5 +1,5 @@ -package com.github.insanusmokrassar.SauceNaoAPI - +import com.insanusmokrassar.SauceNaoAPI.SauceNaoAPI +import com.insanusmokrassar.SauceNaoAPI.utils.useSafe import io.ktor.http.ContentType import io.ktor.utils.io.streams.asInput import kotlinx.coroutines.* @@ -12,7 +12,7 @@ suspend fun main(vararg args: String) { val scope = CoroutineScope(Dispatchers.Default) val api = SauceNaoAPI(key, scope = scope) - api.use { _ -> + api.useSafe { _ -> println( when { requestSubject.startsWith("/") -> File(requestSubject).let { diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/AdaptedAnswer.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/AdaptedAnswer.kt deleted file mode 100644 index 08a0b83..0000000 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/AdaptedAnswer.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional - -import com.github.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo -import com.github.insanusmokrassar.SauceNaoAPI.additional.header.adapted -import com.github.insanusmokrassar.SauceNaoAPI.additional.results.AdaptedResult -import com.github.insanusmokrassar.SauceNaoAPI.additional.results.adapted -import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer - -val SauceNaoAnswer.adapted: AdaptedAnswer - get() = header.adapted.let { resultMetainfo -> - val adaptedResults = results.map { - it.adapted(resultMetainfo) - } - AdaptedAnswer( - resultMetainfo, - adaptedResults - ) - } - -data class AdaptedAnswer( - val resultMetaInfo: ResultMetaInfo, - val results: List -) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/results/ResultHeader.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/results/ResultHeader.kt deleted file mode 100644 index 96788d5..0000000 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/additional/results/ResultHeader.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.insanusmokrassar.SauceNaoAPI.additional.results - -import com.github.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo - -data class ResultHeader( - val similarity: Float, - val thumbnail: String, - val index: IndexInfo -) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/ResultData.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/ResultData.kt deleted file mode 100644 index 5e0342c..0000000 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/ResultData.kt +++ /dev/null @@ -1,37 +0,0 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models - -import com.github.insanusmokrassar.SauceNaoAPI.utils.CommonMultivariantStringSerializer -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class ResultData( - @Serializable(CommonMultivariantStringSerializer::class) - @SerialName("danbooru_id") - val danbooruId: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - @SerialName("gelbooru_id") - val gelbooruId: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - @SerialName("drawr_id") - val drawrId: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - @SerialName("pixiv_id") - val pixivId: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - val title: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - val creator: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - val material: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - @SerialName("member_name") - val memberName: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - @SerialName("member_id") - val memberId: String? = null, - @Serializable(CommonMultivariantStringSerializer::class) - val characters: String? = null, - @SerialName("ext_urls") - val extUrls: List = emptyList() -) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt deleted file mode 100644 index bb36673..0000000 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.github.insanusmokrassar.SauceNaoAPI.models - -import kotlinx.serialization.* -import kotlinx.serialization.builtins.ListSerializer -import kotlinx.serialization.json.* - -@Serializable -data class SauceNaoAnswer internal constructor( - val header: Header, - val results: List = emptyList(), - val raw: JsonObject = JsonObject(emptyMap()) -) - -@Serializer(SauceNaoAnswer::class) -object SauceNaoAnswerSerializer : KSerializer { - 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 stringRaw = serializer.stringify(JsonObjectSerializer, raw) - - return serializer.parse( - SauceNaoAnswer.serializer(), - stringRaw - ).copy( - raw = raw - ) - } - - override fun serialize(encoder: Encoder, obj: SauceNaoAnswer) { - val resultObject = JsonObject( - obj.raw.content.let { - it + mapOf( - headerField to serializer.toJson(Header.serializer(), obj.header), - resultsField to serializer.toJson(resultsSerializer, obj.results) - ) - } - ) - JsonObject.serializer().serialize(encoder, resultObject) - } -} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/CommonMultivariantStringSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/CommonMultivariantStringSerializer.kt deleted file mode 100644 index 2a48ce0..0000000 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/CommonMultivariantStringSerializer.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.insanusmokrassar.SauceNaoAPI.utils - -import kotlinx.serialization.* -import kotlinx.serialization.builtins.ListSerializer -import kotlinx.serialization.builtins.serializer - - -@Serializer(String::class) -object CommonMultivariantStringSerializer : KSerializer by String.serializer() { - private val stringArraySerializer = ListSerializer(String.serializer()) - - override fun deserialize(decoder: Decoder): String { - return try { - decoder.decodeSerializableValue(String.serializer()) - } catch (e: Exception) { - decoder.decodeSerializableValue(stringArraySerializer).joinToString() - } - } -} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/MimeTypeDecoder.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/MimeTypeDecoder.kt deleted file mode 100644 index 415909a..0000000 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/MimeTypeDecoder.kt +++ /dev/null @@ -1,16 +0,0 @@ -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