mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2025-12-08 14:05:46 +00:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bce508a8c2 | |||
| 224f0a6cc0 | |||
| 61937ed209 | |||
| 8e861020d8 | |||
| c231b956b4 | |||
| c14df69ae1 | |||
| f55744a038 | |||
| f27c493771 | |||
| 7bc1e822fc | |||
| eb9456a233 | |||
| 8c8a02054d | |||
| f52eb2ad6e | |||
| 4fcdb6f728 | |||
| 18cdf4ffbe | |||
| 33d143ea12 | |||
| e0215dcf8f | |||
| ca4348b5f6 | |||
| 249cbba1f7 | |||
| a94b7b8020 | |||
| 632b534da9 | |||
| b30af7c8f9 | |||
| dda2ca17d3 | |||
| 0591f26d0c | |||
| d24e75a452 | |||
| 6b8d06da70 | |||
| cbd6651c67 | |||
| 69bfa56692 | |||
| 270be95784 | |||
| 09b4b45b75 | |||
| b80293103e | |||
| fe32aaacb2 | |||
| 7cbaac8a3e |
82
CHANGELOG.md
82
CHANGELOG.md
@@ -1,26 +1,56 @@
|
||||
# SauceNaoAPI Changelog
|
||||
|
||||
## 0.7.1
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.4.20` -> `1.4.21`
|
||||
* `Klock`: `2.0.0` -> `2.0.2`
|
||||
* `Ktor`: `1.4.3` -> `1.5.0`
|
||||
|
||||
## 0.7.0
|
||||
|
||||
**BREAKING CHANGES: PACKAGE HAS BEEN CHANGED FROM `com.insanusmokrassar` to `dev.inmo`**
|
||||
|
||||
Migration:
|
||||
|
||||
* Packages in the whole project were changed `com.insanusmokrassar.SauceNaoAPI` -> `dev.inmo.saucenaoapi`
|
||||
* Change implementation in your gradle files: `implementation "com.insanusmokrassar:SauceNaoAPI:*"` ->
|
||||
`implementation "dev.inmo:saucenaoapi:*"`
|
||||
|
||||
## 0.6.2
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.4.10` -> `1.4.20`
|
||||
* `Kotlin Serialisation`: `1.0.0-RC2` -> `1.0.1`
|
||||
* `Kotlin Coroutines`: `1.3.9` -> `1.4.2`
|
||||
* `Klock`: `1.12.1` -> `2.0.0`
|
||||
* `Ktor`: `1.4.1` -> `1.4.3`
|
||||
|
||||
## 0.6.1
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.4.0` -> `1.4.10`
|
||||
* `Kotlin Serialisation`: `1.0.0-RC` -> `1.0.0-RC2`
|
||||
* `Klock`: `1.12.0` -> `1.12.1`
|
||||
* `Ktor`: `1.4.0` -> `1.4.1`
|
||||
|
||||
## 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
|
||||
|
||||
### 0.5.1
|
||||
|
||||
* All known fields were added to `ResultData`
|
||||
|
||||
## 0.4.0
|
||||
|
||||
* Update libraries versions
|
||||
* Kotlin `1.3.31` -> `1.3.50`
|
||||
* Coroutines `1.2.1` -> `1.3.2`
|
||||
* Serialization `0.11.0` -> `0.13.0`
|
||||
* Joda Time `2.10.1` -> `2.10.4`
|
||||
* Ktor `1.1.4` -> `1.2.5`
|
||||
* Now `SauceNaoAPI` is `Closeable`
|
||||
* Now `SauceNaoAPI` working with synchronous queue
|
||||
* `SauceNaoAPI` now will wait for some time when one of limits will be achieved
|
||||
|
||||
### 0.4.4
|
||||
## 0.4.4
|
||||
|
||||
* Uploading of file
|
||||
* Updates of versions
|
||||
@@ -28,15 +58,15 @@
|
||||
* `SauceNaoAPI` instances now can return `limitsState` object, which will contains `LimitsState` with currently known
|
||||
state of limits
|
||||
|
||||
### 0.4.3
|
||||
## 0.4.3
|
||||
|
||||
Hotfix for serializer of `SauceNaoAnswer`
|
||||
|
||||
### 0.4.2
|
||||
## 0.4.2
|
||||
|
||||
Hotfix for autostop for some time when there is no remaining quotas for requests
|
||||
|
||||
### 0.4.1 Managers experiments and row format in answer
|
||||
## 0.4.1 Managers experiments and row format in answer
|
||||
|
||||
* Add `TimeManager` - it will manage work with requests times
|
||||
* Add `RequestQuotaMagager` - it will manage quota for requests and call suspend
|
||||
@@ -45,6 +75,18 @@ if they will be over
|
||||
* Now `SauceNaoAnswer` have field `row` which contains `JsonObject` with
|
||||
all original answer fields
|
||||
|
||||
## 0.4.0
|
||||
|
||||
* Update libraries versions
|
||||
* Kotlin `1.3.31` -> `1.3.50`
|
||||
* Coroutines `1.2.1` -> `1.3.2`
|
||||
* Serialization `0.11.0` -> `0.13.0`
|
||||
* Joda Time `2.10.1` -> `2.10.4`
|
||||
* Ktor `1.1.4` -> `1.2.5`
|
||||
* Now `SauceNaoAPI` is `Closeable`
|
||||
* Now `SauceNaoAPI` working with synchronous queue
|
||||
* `SauceNaoAPI` now will wait for some time when one of limits will be achieved
|
||||
|
||||
## 0.3.0
|
||||
|
||||
* Now `results` field of `SauceNaoAnswer` is optional and is empty list by default
|
||||
|
||||
10
README.md
10
README.md
@@ -1,9 +1,19 @@
|
||||
# SauceNaoAPI
|
||||
|
||||
[  ](https://bintray.com/insanusmokrassar/SauceNaoAPI/saucenaoapi/_latestVersion)
|
||||
|
||||
It is wrapper for [SauceNAO](https://saucenao.com/) API. For now, library is
|
||||
in preview state. It can be fully used, but some of info can be unavailable from
|
||||
wrapper classes, but now you can access them via `SauceNaoAnswer#row` field.
|
||||
|
||||
## Including
|
||||
|
||||
### Gradle
|
||||
|
||||
```groovy
|
||||
implementation "com.insanusmokrassar:SauceNaoAPI:$saucenaoapi_version"
|
||||
```
|
||||
|
||||
## Requester
|
||||
|
||||
For the requests we are using `SauceNaoAPI` object. Unfortunately, for now it
|
||||
|
||||
61
build.gradle
61
build.gradle
@@ -1,6 +1,3 @@
|
||||
project.version = "0.5.1"
|
||||
project.group = "com.github.insanusmokrassar"
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
@@ -11,31 +8,63 @@ buildscript {
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
|
||||
classpath "com.github.breadmoirai:github-release:$gradle_github_release_plugin_version"
|
||||
}
|
||||
}
|
||||
|
||||
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 = "$library_version"
|
||||
project.group = "dev.inmo"
|
||||
|
||||
apply from: "publish.gradle"
|
||||
apply from: "github_release.gradle"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
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-json:$kotlin_serialisation_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')
|
||||
}
|
||||
}
|
||||
jvmTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-junit')
|
||||
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
|
||||
}
|
||||
}
|
||||
jsTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-js')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
changelog_parser.sh
Executable file
24
changelog_parser.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
|
||||
function parse() {
|
||||
version=$1
|
||||
|
||||
while IFS= read -r line && [ -z "`echo $line | grep -e "^#\+ $version"`" ]
|
||||
do
|
||||
: # do nothing
|
||||
done
|
||||
|
||||
while IFS= read -r line && [ -z "`echo $line | grep -e "^#\+"`" ]
|
||||
do
|
||||
echo "$line"
|
||||
done
|
||||
}
|
||||
|
||||
version=$1
|
||||
file=$2
|
||||
|
||||
if [ -n "$file" ]; then
|
||||
parse $version < "$file"
|
||||
else
|
||||
parse $version
|
||||
fi
|
||||
28
github_release.gradle
Normal file
28
github_release.gradle
Normal file
@@ -0,0 +1,28 @@
|
||||
private String getCurrentVersionChangelog() {
|
||||
OutputStream changelogDataOS = new ByteArrayOutputStream()
|
||||
exec {
|
||||
standardOutput = changelogDataOS
|
||||
commandLine 'chmod', "+x", './changelog_parser.sh'
|
||||
commandLine './changelog_parser.sh', "$library_version", 'CHANGELOG.md'
|
||||
}
|
||||
|
||||
return changelogDataOS.toString().trim()
|
||||
}
|
||||
|
||||
if (new File(projectDir, "secret.gradle").exists()) {
|
||||
apply from: './secret.gradle'
|
||||
apply plugin: "com.github.breadmoirai.github-release"
|
||||
|
||||
githubRelease {
|
||||
token "${project.property('GITHUB_RELEASE_TOKEN')}"
|
||||
|
||||
owner "InsanusMokrassar"
|
||||
repo "${rootProject.name}"
|
||||
|
||||
tagName "${project.version}"
|
||||
releaseName "${project.version}"
|
||||
targetCommitish "${project.version}"
|
||||
|
||||
body getCurrentVersionChangelog()
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
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.21
|
||||
kotlin_coroutines_version=1.4.2
|
||||
kotlin_serialisation_version=1.0.1
|
||||
klock_version=2.0.2
|
||||
ktor_version=1.5.0
|
||||
|
||||
project_public_name=SauceNao API
|
||||
project_public_description=SauceNao API library
|
||||
gradle_github_release_plugin_version=2.2.12
|
||||
|
||||
gradle_bintray_plugin_version=1.8.4
|
||||
library_version=0.7.1
|
||||
|
||||
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-6.5.1-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'signing'
|
||||
|
||||
task sourcesJar(type: Jar) {
|
||||
from sourceSets.main.allSource
|
||||
classifier = 'sources'
|
||||
}
|
||||
|
||||
task javadocJar(type: Jar) {
|
||||
from javadoc
|
||||
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'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signing {
|
||||
useGpgCmd()
|
||||
sign publishing.publications.maven
|
||||
}
|
||||
1
publication.kpsb
Normal file
1
publication.kpsb
Normal file
@@ -0,0 +1 @@
|
||||
{"bintrayConfig":{"repo":"SauceNaoAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/${project.name}","autoPublish":true,"overridePublish":true},"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"}]}}
|
||||
@@ -1,33 +1,69 @@
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
ext {
|
||||
projectBintrayDir = "${project.group}/".replace(".", "/") + "${project.name}/${project.version}"
|
||||
task javadocsJar(type: Jar) {
|
||||
classifier = 'javadoc'
|
||||
}
|
||||
task sourceJar (type : Jar) {
|
||||
classifier = 'sources'
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
pkg {
|
||||
repo = 'StandardRepository'
|
||||
name = "${project.name}"
|
||||
vcsUrl = "https://github.com/InsanusMokrassar/${project.name}"
|
||||
licenses = ['Apache-2.0']
|
||||
version {
|
||||
name = "${project.version}"
|
||||
released = new Date()
|
||||
vcsTag = name
|
||||
afterEvaluate {
|
||||
project.publishing.publications.all {
|
||||
// rename artifacts
|
||||
groupId "${project.group}"
|
||||
if (it.name.contains('kotlinMultiplatform')) {
|
||||
artifactId = "${project.name}"
|
||||
artifact sourceJar
|
||||
} else {
|
||||
artifactId = "${project.name}-$name"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
apply from: "maven.publish.gradle"
|
||||
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"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
name = "bintray"
|
||||
url = uri("https://api.bintray.com/maven/${project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')}/SauceNaoAPI/${project.name}/;publish=1;override=1")
|
||||
credentials {
|
||||
username = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')
|
||||
password = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1 @@
|
||||
/*
|
||||
* This settings file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* The settings file is used to specify which projects to include in your build.
|
||||
* In a single project build this file can be empty or even removed.
|
||||
*
|
||||
* Detailed information about configuring a multi-project build in Gradle can be found
|
||||
* in the user guide at https://docs.gradle.org/3.4.1/userguide/multi_project_builds.html
|
||||
*/
|
||||
|
||||
/*
|
||||
// To declare projects as part of a multi-project build use the 'include' method
|
||||
include 'shared'
|
||||
include 'api'
|
||||
include 'services:webservice'
|
||||
*/
|
||||
|
||||
rootProject.name = 'SauceNaoAPI'
|
||||
rootProject.name = 'saucenaoapi'
|
||||
|
||||
17
src/commonMain/kotlin/dev/inmo/saucenaoapi/OutputType.kt
Normal file
17
src/commonMain/kotlin/dev/inmo/saucenaoapi/OutputType.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package dev.inmo.saucenaoapi
|
||||
|
||||
sealed class OutputType {
|
||||
abstract val typeCode: Int
|
||||
}
|
||||
|
||||
object HtmlOutputType : dev.inmo.saucenaoapi.OutputType() {
|
||||
override val typeCode: Int = 0
|
||||
}
|
||||
|
||||
object XmlOutputType : dev.inmo.saucenaoapi.OutputType() {
|
||||
override val typeCode: Int = 1
|
||||
}
|
||||
|
||||
object JsonOutputType : dev.inmo.saucenaoapi.OutputType() {
|
||||
override val typeCode: Int = 2
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
package dev.inmo.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 dev.inmo.saucenaoapi.exceptions.TooManyRequestsException
|
||||
import dev.inmo.saucenaoapi.exceptions.sauceNaoAPIException
|
||||
import dev.inmo.saucenaoapi.models.*
|
||||
import dev.inmo.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 outputType: dev.inmo.saucenaoapi.OutputType = dev.inmo.saucenaoapi.JsonOutputType,
|
||||
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<Pair<Continuation<SauceNaoAnswer>, 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<HttpResponse>(builder)
|
||||
val answerText = call.readText()
|
||||
logger.info(answerText)
|
||||
timeManager.addTimeAndClear()
|
||||
Json.nonstrict.parse(
|
||||
parser.decodeFromString(
|
||||
SauceNaoAnswerSerializer,
|
||||
answerText
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
package dev.inmo.saucenaoapi
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.Input
|
||||
@@ -0,0 +1,23 @@
|
||||
package dev.inmo.saucenaoapi.additional
|
||||
|
||||
import dev.inmo.saucenaoapi.additional.header.ResultMetaInfo
|
||||
import dev.inmo.saucenaoapi.additional.header.adapted
|
||||
import dev.inmo.saucenaoapi.additional.results.AdaptedResult
|
||||
import dev.inmo.saucenaoapi.additional.results.adapted
|
||||
import dev.inmo.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<AdaptedResult>
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional
|
||||
package dev.inmo.saucenaoapi.additional
|
||||
|
||||
import com.soywiz.klock.TimeSpan
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
|
||||
package dev.inmo.saucenaoapi.additional.header
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.*
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import dev.inmo.saucenaoapi.additional.*
|
||||
import dev.inmo.saucenaoapi.models.Header
|
||||
|
||||
val Header.shortLimitStatus: LimitStatus
|
||||
get() = LimitStatus(
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
|
||||
package dev.inmo.saucenaoapi.additional.header
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import dev.inmo.saucenaoapi.models.Header
|
||||
|
||||
data class IndexInfo(
|
||||
val id: Int,
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
|
||||
package dev.inmo.saucenaoapi.additional.header
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import dev.inmo.saucenaoapi.models.Header
|
||||
|
||||
val Header.queryPreview
|
||||
get() = QueryResultPreview(
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
|
||||
package dev.inmo.saucenaoapi.additional.header
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import dev.inmo.saucenaoapi.models.Header
|
||||
|
||||
data class ResultMetaInfo(
|
||||
val accountInfo: AccountInfo = AccountInfo(),
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.additional.results
|
||||
package dev.inmo.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 dev.inmo.saucenaoapi.additional.header.IndexInfo
|
||||
import dev.inmo.saucenaoapi.additional.header.ResultMetaInfo
|
||||
import dev.inmo.saucenaoapi.models.Result
|
||||
import dev.inmo.saucenaoapi.models.ResultData
|
||||
|
||||
fun Result.adapted(
|
||||
resultMetaInfo: ResultMetaInfo
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.inmo.saucenaoapi.additional.results
|
||||
|
||||
import dev.inmo.saucenaoapi.additional.header.IndexInfo
|
||||
|
||||
data class ResultHeader(
|
||||
val similarity: Float,
|
||||
val thumbnail: String,
|
||||
val index: IndexInfo
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
package dev.inmo.saucenaoapi.exceptions
|
||||
|
||||
import dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.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 io.ktor.utils.io.errors.IOException
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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("Too many requests were sent in the short period") {
|
||||
override val waitTime: TimeSpan = SHORT_TIME_RECALCULATING_MILLIS
|
||||
}
|
||||
class TooManyRequestsLongException(override val answerContent: String) : TooManyRequestsException("Too many requests were sent in the long period") {
|
||||
override val waitTime: TimeSpan = LONG_TIME_RECALCULATING_MILLIS
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import dev.inmo.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<List<HeaderIndex?>> {
|
||||
override val descriptor: SerialDescriptor = StringDescriptor
|
||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): List<HeaderIndex?> {
|
||||
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<HeaderIndex?>(parsed.keys.max() ?: 0) {
|
||||
return Array<HeaderIndex?>(parsed.keys.maxOrNull() ?: 0) {
|
||||
parsed[it]
|
||||
}.toList()
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: List<HeaderIndex?>) {
|
||||
override fun serialize(encoder: Encoder, value: List<HeaderIndex?>) {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.CommonMultivariantStringSerializer
|
||||
import dev.inmo.saucenaoapi.utils.CommonMultivariantStringSerializer
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
@@ -0,0 +1,56 @@
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import dev.inmo.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<Result> = emptyList(),
|
||||
)
|
||||
|
||||
@Serializable(SauceNaoAnswerSerializer::class)
|
||||
data class SauceNaoAnswer internal constructor(
|
||||
val header: Header,
|
||||
val results: List<Result> = emptyList(),
|
||||
val raw: JsonObject = JsonObject(emptyMap())
|
||||
)
|
||||
|
||||
@Serializer(SauceNaoAnswer::class)
|
||||
object SauceNaoAnswerSerializer : KSerializer<SauceNaoAnswer> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
@Serializer(String::class)
|
||||
object CommonMultivariantStringSerializer : KSerializer<String> by 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,21 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
package dev.inmo.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 dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.saucenaoapi.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.saucenaoapi.exceptions.TooManyRequestsException
|
||||
import dev.inmo.saucenaoapi.exceptions.TooManyRequestsLongException
|
||||
import dev.inmo.saucenaoapi.models.Header
|
||||
import dev.inmo.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
|
||||
@@ -0,0 +1,21 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
|
||||
interface SauceCloseable {
|
||||
fun close()
|
||||
}
|
||||
|
||||
fun <T> SauceCloseable.use(block: (SauceCloseable) -> T): T = try {
|
||||
block(this)
|
||||
} finally {
|
||||
close()
|
||||
}
|
||||
|
||||
suspend fun <T> SauceCloseable.useSafe(block: suspend (SauceCloseable) -> T): T = try {
|
||||
supervisorScope {
|
||||
block(this@useSafe)
|
||||
}
|
||||
} finally {
|
||||
close()
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.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<DateTime>) {
|
||||
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<TimeManagerAction>(Channel.UNLIMITED)
|
||||
|
||||
private val timeUpdateJob = scope.launch {
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import dev.inmo.saucenaoapi.SauceNaoAPI
|
||||
import dev.inmo.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 {
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
sealed class OutputType {
|
||||
abstract val typeCode: Int
|
||||
}
|
||||
|
||||
object HtmlOutputType : OutputType() {
|
||||
override val typeCode: Int = 0
|
||||
}
|
||||
|
||||
object XmlOutputType : OutputType() {
|
||||
override val typeCode: Int = 1
|
||||
}
|
||||
|
||||
object JsonOutputType : OutputType() {
|
||||
override val typeCode: Int = 2
|
||||
}
|
||||
@@ -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<AdaptedResult>
|
||||
)
|
||||
@@ -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
|
||||
)
|
||||
@@ -1,34 +0,0 @@
|
||||
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 io.ktor.utils.io.errors.IOException
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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,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<Result> = emptyList(),
|
||||
val raw: JsonObject = JsonObject(emptyMap())
|
||||
)
|
||||
|
||||
@Serializer(SauceNaoAnswer::class)
|
||||
object SauceNaoAnswerSerializer : KSerializer<SauceNaoAnswer> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -1,19 +0,0 @@
|
||||
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 com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.TimeSpan
|
||||
|
||||
internal suspend fun calculateSleepTime(
|
||||
header: Header,
|
||||
mostOldestInShortPeriodGetter: suspend () -> DateTime?,
|
||||
mostOldestInLongPeriodGetter: suspend () -> DateTime?
|
||||
): DateTime? {
|
||||
return when {
|
||||
header.longRemaining < 1 -> mostOldestInLongPeriodGetter() ?.plus(LONG_TIME_RECALCULATING_MILLIS)
|
||||
header.shortRemaining < 1 -> mostOldestInShortPeriodGetter() ?.plus(SHORT_TIME_RECALCULATING_MILLIS)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user