mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2025-12-08 14:05:46 +00:00
Compare commits
104 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 96f520700f | |||
| 2a4296ff7d | |||
| 2f34ab7ad1 | |||
| 798c4097ed | |||
| c1df04fd09 | |||
| 8cc0426dc5 | |||
| 9cc929adf6 | |||
| 85567956ed | |||
| db154f4202 | |||
| bfdbee356a | |||
| 62580f6649 | |||
| 0b79f27b38 | |||
| 8543917da7 | |||
| be858d3e60 | |||
| 90e090ca78 | |||
| 66414ff8df | |||
| d0c143dcd6 | |||
| 3ee0d0de7d | |||
| d04ee03043 | |||
| e173bd71e0 | |||
| 11aac1b581 | |||
| 4319c41e44 | |||
| dfd1ba807f | |||
| e926a5422d | |||
| d631d63490 | |||
| 60ae527dd0 | |||
| 05b8d7738f | |||
| b5ceff32bd | |||
| 64109735f8 | |||
| d4f902aefd | |||
| 2de15a54a1 | |||
| 036deeb318 | |||
| 4cc0223fbc | |||
| be292102aa | |||
| 9d473857be | |||
| 6ff3f76295 | |||
| 0ed1a8702b | |||
| c800a81a41 | |||
| 62ceae0066 | |||
| 8619d020f6 | |||
| affa06905e | |||
| cbe76b3d95 | |||
| 9f4feeccfc | |||
| 037074cef6 | |||
| dfe29296ab | |||
| ce1d185eff | |||
| a78611a27c | |||
| cba4b2ccdb | |||
| 6aabcca9d2 | |||
| 503c4226d7 | |||
| d82bff2563 | |||
| 8391e20dce | |||
| 67244683dd | |||
| 948dede0c8 | |||
| 6f171c5f6d | |||
| 6d85ccfffa | |||
| 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 | |||
| 6ab2646a03 | |||
| a54ac4d6a6 | |||
| 03e201ae56 | |||
| 315d78db11 | |||
| b597312a0c | |||
|
|
46ad249067 | ||
| 1348abd482 | |||
|
|
c7dc76c8df | ||
| 541134d8a7 | |||
| 0c2c7e9e50 | |||
| 3ecfb4298b | |||
| 8c49e60dec | |||
| 51957eb369 | |||
| 1bab6417ed | |||
| ea9d76fa47 | |||
| d5d3de9559 |
24
.github/workflows/build_and_publish.yml
vendored
Normal file
24
.github/workflows/build_and_publish.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
name: Publish package to GitHub Packages
|
||||
on: [push]
|
||||
jobs:
|
||||
publishing:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 11
|
||||
- name: Rewrite version
|
||||
run: |
|
||||
branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`"
|
||||
cat gradle.properties | sed -e "s/^library_version=\([0-9\.]*\)/library_version=\1-branch_$branch-build${{ github.run_number }}/" > gradle.properties.tmp
|
||||
rm gradle.properties
|
||||
mv gradle.properties.tmp gradle.properties
|
||||
- name: Build
|
||||
run: ./gradlew build
|
||||
- name: Publish
|
||||
run: ./gradlew publishAllPublicationsToGithubPackagesRepository --no-parallel -x signJsPublication -x signJvmPublication -x signKotlinMultiplatformPublication
|
||||
continue-on-error: true
|
||||
env:
|
||||
GITHUBPACKAGES_USER: ${{ github.actor }}
|
||||
GITHUBPACKAGES_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
|
||||
158
CHANGELOG.md
158
CHANGELOG.md
@@ -1,22 +1,144 @@
|
||||
# SauceNaoAPI Changelog
|
||||
|
||||
## 0.4.0
|
||||
## 0.11.1
|
||||
|
||||
* 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
|
||||
* Versions updates:
|
||||
* `Ktor`: `2.0.1` -> `2.0.3`
|
||||
* `Coroutines`: `1.6.1` -> `1.6.4`
|
||||
|
||||
### 0.4.2
|
||||
## 0.11.0
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.6.10` -> `1.6.21`
|
||||
* `Serialization`: `1.3.2` -> `1.3.3`
|
||||
* `Klock`: `2.6.3` -> `2.7.0`
|
||||
* `Ktor`: `1.6.8` -> `2.0.1`
|
||||
|
||||
## 0.10.1
|
||||
|
||||
* Versions updates:
|
||||
* `Klock`: `2.6.2` -> `2.6.3`
|
||||
* `Ktor`: `1.6.7` -> `1.6.8`
|
||||
|
||||
## 0.10.0
|
||||
|
||||
Migration onto libs versions toml
|
||||
|
||||
## 0.9.1
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.5.30` -> `1.5.31`
|
||||
* `Klock`: `2.4.0` -> `2.4.2`
|
||||
* `Coroutines`: `1.5.1` -> `1.5.2`
|
||||
* Add several extensions to `ResultData`: `authors`, `froms`, `charactersList`, `titles`, `urls`
|
||||
|
||||
## 0.9.0
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.5.10` -> `1.5.30`
|
||||
* `Klock`: `2.1.2` -> `2.4.0`
|
||||
* `Ktor`: `1.5.4` -> `1.6.3`
|
||||
* `Serialization`: `1.2.1` -> `1.2.2`
|
||||
* `Coroutines`: `1.5.0` -> `1.5.1`
|
||||
|
||||
## 0.8.2
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.4.32` -> `1.5.10`
|
||||
* `Klock`: `2.0.7` -> `2.1.2`
|
||||
* `Ktor`: `1.5.3` -> `1.5.4`
|
||||
* `Serialization`: `1.1.0` -> `1.2.1`
|
||||
* `Coroutines`: `1.4.3` -> `1.5.0`
|
||||
|
||||
## 0.8.1
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.4.31` -> `1.4.32`
|
||||
* `Klock`: `2.0.6` -> `2.0.7`
|
||||
* `Ktor`: `1.5.2` -> `1.5.3`
|
||||
|
||||
## 0.8.0
|
||||
|
||||
* Versions updates:
|
||||
* `Kotlin`: `1.4.21` -> `1.4.31`
|
||||
* `Klock`: `2.0.4` -> `2.0.6`
|
||||
* `Ktor`: `1.5.1` -> `1.5.2`
|
||||
* `Kotlin Serialisation`: `1.0.1` -> `1.1.0`
|
||||
* `Kotlin Coroutines`: `1.4.2` -> `1.4.3`
|
||||
|
||||
## 0.7.2
|
||||
|
||||
* Versions updates:
|
||||
* `Klock`: `2.0.2` -> `2.0.4`
|
||||
* `Ktor`: `1.5.0` -> `1.5.1`
|
||||
|
||||
## 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.4.4
|
||||
|
||||
* Uploading of file
|
||||
* Updates of versions
|
||||
* Now `SauceNaoAPI` do not require api key
|
||||
* `SauceNaoAPI` instances now can return `limitsState` object, which will contains `LimitsState` with currently known
|
||||
state of limits
|
||||
|
||||
## 0.4.3
|
||||
|
||||
Hotfix for serializer of `SauceNaoAnswer`
|
||||
|
||||
## 0.4.2
|
||||
|
||||
Hotfix for autostop for some time when there is no remaining quotas for requests
|
||||
|
||||
### 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
|
||||
@@ -25,6 +147,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
|
||||
|
||||
11
README.md
11
README.md
@@ -1,9 +1,20 @@
|
||||
# SauceNaoAPI
|
||||
|
||||
[](https://maven-badges.herokuapp.com/maven-central/dev.inmo/saucenaoapi)
|
||||
|
||||
|
||||
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 "dev.inmo:saucenaoapi:$saucenaoapi_version"
|
||||
```
|
||||
|
||||
## Requester
|
||||
|
||||
For the requests we are using `SauceNaoAPI` object. Unfortunately, for now it
|
||||
|
||||
68
build.gradle
68
build.gradle
@@ -1,41 +1,67 @@
|
||||
project.version = "0.4.2"
|
||||
project.group = "com.github.insanusmokrassar"
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
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 libs.buildscript.kt.gradle
|
||||
classpath libs.buildscript.kt.serialization
|
||||
classpath libs.buildscript.gh.release
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'kotlinx-serialization'
|
||||
plugins {
|
||||
alias(libs.plugins.multiplatform)
|
||||
alias(libs.plugins.serialization)
|
||||
}
|
||||
|
||||
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 "joda-time:joda-time:$joda_time_version"
|
||||
implementation "io.ktor:ktor-client-core:$ktor_version"
|
||||
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
|
||||
kotlin {
|
||||
jvm()
|
||||
js(IR) {
|
||||
browser()
|
||||
nodejs()
|
||||
}
|
||||
|
||||
// Use JUnit test framework
|
||||
testImplementation 'junit:junit:4.12'
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
|
||||
api libs.kt.coroutines
|
||||
api libs.kt.serialization
|
||||
api libs.klock
|
||||
api libs.ktor.client
|
||||
}
|
||||
}
|
||||
commonTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-common')
|
||||
implementation kotlin('test-annotations-common')
|
||||
}
|
||||
}
|
||||
jvmTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-junit')
|
||||
implementation libs.ktor.client.okhttp
|
||||
}
|
||||
}
|
||||
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 "v${project.version}"
|
||||
releaseName "${project.version}"
|
||||
targetCommitish "${project.version}"
|
||||
|
||||
body getCurrentVersionChangelog()
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,3 @@
|
||||
kotlin.code.style=official
|
||||
kotlin_version=1.3.50
|
||||
kotlin_coroutines_version=1.3.2
|
||||
kotlin_serialisation_runtime_version=0.13.0
|
||||
joda_time_version=2.10.4
|
||||
ktor_version=1.2.5
|
||||
|
||||
project_public_name=SauceNao API
|
||||
project_public_description=SauceNao API library
|
||||
|
||||
gradle_bintray_plugin_version=1.8.4
|
||||
library_version=0.12.0
|
||||
|
||||
30
gradle/libs.versions.toml
Normal file
30
gradle/libs.versions.toml
Normal file
@@ -0,0 +1,30 @@
|
||||
[versions]
|
||||
|
||||
kt = "1.7.10"
|
||||
kt-serialization = "1.4.0-RC"
|
||||
kt-coroutines = "1.6.4"
|
||||
|
||||
klock = "3.0.0"
|
||||
ktor = "2.0.3"
|
||||
|
||||
gh-release = "2.4.1"
|
||||
|
||||
[libraries]
|
||||
|
||||
kt-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kt" }
|
||||
kt-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kt-serialization" }
|
||||
kt-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kt-coroutines" }
|
||||
|
||||
klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" }
|
||||
|
||||
ktor-client = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
|
||||
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
|
||||
|
||||
buildscript-kt-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kt" }
|
||||
buildscript-kt-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kt" }
|
||||
buildscript-gh-release = { module = "com.github.breadmoirai:github-release", version.ref = "gh-release" }
|
||||
|
||||
[plugins]
|
||||
|
||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kt" }
|
||||
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kt" }
|
||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
|
||||
|
||||
2058
kotlin-js-store/yarn.lock
Normal file
2058
kotlin-js-store/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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,33 +1,79 @@
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
ext {
|
||||
projectBintrayDir = "${project.group}/".replace(".", "/") + "${project.name}/${project.version}"
|
||||
task javadocsJar(type: Jar) {
|
||||
classifier = 'javadoc'
|
||||
}
|
||||
|
||||
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"
|
||||
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/SauceNaoAPI/blob/master/LICENSE"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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
|
||||
repositories {
|
||||
if ((project.hasProperty('GITHUBPACKAGES_USER') || System.getenv('GITHUBPACKAGES_USER') != null) && (project.hasProperty('GITHUBPACKAGES_PASSWORD') || System.getenv('GITHUBPACKAGES_PASSWORD') != null)) {
|
||||
maven {
|
||||
name = "GithubPackages"
|
||||
url = uri("https://maven.pkg.github.com/InsanusMokrassar/SauceNaoAPI")
|
||||
credentials {
|
||||
username = project.hasProperty('GITHUBPACKAGES_USER') ? project.property('GITHUBPACKAGES_USER') : System.getenv('GITHUBPACKAGES_USER')
|
||||
password = project.hasProperty('GITHUBPACKAGES_PASSWORD') ? project.property('GITHUBPACKAGES_PASSWORD') : System.getenv('GITHUBPACKAGES_PASSWORD')
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) {
|
||||
maven {
|
||||
name = "sonatype"
|
||||
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
|
||||
credentials {
|
||||
username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
|
||||
password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (project.hasProperty("signing.gnupg.keyName")) {
|
||||
apply plugin: 'signing'
|
||||
|
||||
signing {
|
||||
useGpgCmd()
|
||||
|
||||
sign publishing.publications
|
||||
}
|
||||
|
||||
task signAll {
|
||||
tasks.withType(Sign).forEach {
|
||||
dependsOn(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
apply from: "maven.publish.gradle"
|
||||
|
||||
1
publish.kpsb
Normal file
1
publish.kpsb
Normal file
@@ -0,0 +1 @@
|
||||
{"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/SauceNaoAPI/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"}],"repositories":[{"name":"GithubPackages","url":"https://maven.pkg.github.com/InsanusMokrassar/SauceNaoAPI"},{"name":"sonatype","url":"https://oss.sonatype.org/service/local/staging/deploy/maven2/"}],"gpgSigning":{"type":"dev.inmo.kmppscriptbuilder.core.models.GpgSigning.Optional"}}}
|
||||
5
renovate.json
Normal file
5
renovate.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
]
|
||||
}
|
||||
@@ -1,18 +1,3 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
rootProject.name = 'saucenaoapi'
|
||||
|
||||
/*
|
||||
// To declare projects as part of a multi-project build use the 'include' method
|
||||
include 'shared'
|
||||
include 'api'
|
||||
include 'services:webservice'
|
||||
*/
|
||||
|
||||
rootProject.name = 'SauceNaoAPI'
|
||||
enableFeaturePreview("VERSION_CATALOGS")
|
||||
|
||||
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
|
||||
}
|
||||
230
src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt
Normal file
230
src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt
Normal file
@@ -0,0 +1,230 @@
|
||||
package dev.inmo.saucenaoapi
|
||||
|
||||
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.plugins.ClientRequestException
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.request.forms.MultiPartFormDataContent
|
||||
import io.ktor.client.request.forms.formData
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.*
|
||||
import io.ktor.utils.io.core.Input
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
private const val API_TOKEN_FIELD = "api_key"
|
||||
private const val OUTPUT_TYPE_FIELD = "output_type"
|
||||
private const val URL_FIELD = "url"
|
||||
private const val FILE_FIELD = "file"
|
||||
private const val FILENAME_FIELD = "filename"
|
||||
private const val DB_FIELD = "db"
|
||||
private const val DBMASK_FIELD = "dbmask"
|
||||
private const val DBMASKI_FIELD = "dbmaski"
|
||||
private const val RESULTS_COUNT_FIELD = "numres"
|
||||
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 client: HttpClient = HttpClient(),
|
||||
private val searchUrl: String = SEARCH_URL,
|
||||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||
private val parser: Json = defaultSauceNaoParser
|
||||
) : SauceCloseable {
|
||||
private val requestsChannel = Channel<Pair<CompletableDeferred<SauceNaoAnswer>, HttpRequestBuilder>>(Channel.UNLIMITED)
|
||||
private val subscope = CoroutineScope(scope.coroutineContext + SupervisorJob(scope.coroutineContext.job)).also {
|
||||
it.coroutineContext.job.invokeOnCompletion {
|
||||
requestsChannel.close(it)
|
||||
}
|
||||
}
|
||||
private val timeManager = TimeManager(subscope)
|
||||
private val quotaManager = RequestQuotaManager(subscope)
|
||||
|
||||
val limitsState: LimitsState
|
||||
get() = quotaManager.limitsState
|
||||
|
||||
private val requestsJob = subscope.launch {
|
||||
for ((callback, requestBuilder) in requestsChannel) {
|
||||
quotaManager.getQuota()
|
||||
launch {
|
||||
try {
|
||||
val answer = makeRequest(requestBuilder)
|
||||
callback.complete(answer)
|
||||
|
||||
quotaManager.updateQuota(answer.header, timeManager)
|
||||
} catch (e: TooManyRequestsException) {
|
||||
quotaManager.happenTooManyRequests(timeManager, e)
|
||||
requestsChannel.send(callback to requestBuilder)
|
||||
} catch (e: Exception) {
|
||||
try {
|
||||
callback.completeExceptionally(e)
|
||||
} catch (e: IllegalStateException) { // may happen when already resumed and api was closed
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun request(
|
||||
url: String,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer = makeRequest(
|
||||
url.asSauceRequestSubject,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun request(
|
||||
mediaInput: Input,
|
||||
mimeType: ContentType,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer = makeRequest(
|
||||
mediaInput.asSauceRequestSubject(mimeType),
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun request(
|
||||
file: MPPFile,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer = request(
|
||||
file.input,
|
||||
file.contentType,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun requestByDb(
|
||||
url: String,
|
||||
db: Int,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer = makeRequest(
|
||||
url.asSauceRequestSubject,
|
||||
db = db,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun requestByMask(
|
||||
url: String,
|
||||
dbmask: Int,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer = makeRequest(
|
||||
url.asSauceRequestSubject,
|
||||
dbmask = dbmask,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun requestByMaskI(
|
||||
url: String,
|
||||
dbmaski: Int,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer = makeRequest(
|
||||
url.asSauceRequestSubject,
|
||||
dbmaski = dbmaski,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
private suspend fun makeRequest(
|
||||
builder: HttpRequestBuilder
|
||||
): SauceNaoAnswer {
|
||||
return try {
|
||||
val call = client.request(builder)
|
||||
val answerText = call.bodyAsText()
|
||||
timeManager.addTimeAndClear()
|
||||
parser.decodeFromString(
|
||||
SauceNaoAnswerSerializer,
|
||||
answerText
|
||||
)
|
||||
} catch (e: ClientRequestException) {
|
||||
throw e.sauceNaoAPIException()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun makeRequest(
|
||||
request: SauceRequestSubject,
|
||||
db: Int? = null,
|
||||
dbmask: Int? = null,
|
||||
dbmaski: Int? = null,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer {
|
||||
val deferred = CompletableDeferred<SauceNaoAnswer>()
|
||||
|
||||
requestsChannel.trySend(
|
||||
deferred to HttpRequestBuilder().apply {
|
||||
url(searchUrl)
|
||||
|
||||
apiToken ?.also { parameter(API_TOKEN_FIELD, it) }
|
||||
parameter(OUTPUT_TYPE_FIELD, JsonOutputType.typeCode)
|
||||
db ?.also { parameter(DB_FIELD, it) }
|
||||
dbmask ?.also { parameter(DBMASK_FIELD, it) }
|
||||
dbmaski ?.also { parameter(DBMASKI_FIELD, it) }
|
||||
resultsCount ?.also { parameter(RESULTS_COUNT_FIELD, it) }
|
||||
minSimilarity ?.also { parameter(MINIMAL_SIMILARITY_FIELD, it) }
|
||||
|
||||
when (request) {
|
||||
is UrlSauceRequestSubject -> {
|
||||
parameter(URL_FIELD, request.url)
|
||||
}
|
||||
is InputRequestSubject -> {
|
||||
val mimeType = request.mimeType
|
||||
|
||||
method = HttpMethod.Post
|
||||
setBody(
|
||||
MultiPartFormDataContent(
|
||||
formData {
|
||||
appendInput(
|
||||
FILE_FIELD,
|
||||
Headers.build {
|
||||
append(HttpHeaders.ContentType, mimeType.toString())
|
||||
|
||||
val fakeFilename = "filename=file" + when (mimeType) {
|
||||
ContentType.Image.GIF -> ".gif"
|
||||
ContentType.Image.JPEG -> ".jpeg"
|
||||
ContentType.Image.PNG -> ".png"
|
||||
ContentType.Image.SVG -> ".svg"
|
||||
else -> throw IllegalArgumentException(
|
||||
"Currently supported formats for uploading in sauce: gif, jpeg, png, svg"
|
||||
)
|
||||
}
|
||||
append(HttpHeaders.ContentDisposition, "filename=$fakeFilename")
|
||||
},
|
||||
block = request::input
|
||||
)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return deferred.await()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
subscope.cancel()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package dev.inmo.saucenaoapi
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.Input
|
||||
|
||||
internal sealed class SauceRequestSubject
|
||||
|
||||
internal data class UrlSauceRequestSubject(val url: String) : SauceRequestSubject()
|
||||
|
||||
internal data class InputRequestSubject(val input: Input, val mimeType: ContentType) : SauceRequestSubject()
|
||||
|
||||
internal val String.asSauceRequestSubject
|
||||
get() = UrlSauceRequestSubject(this)
|
||||
|
||||
internal fun Input.asSauceRequestSubject(mimeType: ContentType)
|
||||
= InputRequestSubject(this, mimeType)
|
||||
@@ -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>
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.inmo.saucenaoapi.additional
|
||||
|
||||
import com.soywiz.klock.TimeSpan
|
||||
|
||||
typealias AccountType = Int
|
||||
const val defaultAccountType: AccountType = 1 // "basic"
|
||||
|
||||
typealias UserId = Int
|
||||
|
||||
val SHORT_TIME_RECALCULATING_MILLIS = TimeSpan(30.0 * 1000)
|
||||
val LONG_TIME_RECALCULATING_MILLIS = TimeSpan(24.0 * 60 * 60 * 1000)
|
||||
@@ -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 com.soywiz.klock.TimeSpan
|
||||
import dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.saucenaoapi.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import io.ktor.client.plugins.ClientRequestException
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
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.bodyAsText()
|
||||
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 com.github.insanusmokrassar.SauceNaoAPI.utils.JsonObjectSerializer
|
||||
import dev.inmo.saucenaoapi.defaultSauceNaoParser
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.StringDescriptor
|
||||
import kotlinx.serialization.json.Json
|
||||
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(
|
||||
@@ -38,22 +42,25 @@ data class Header(
|
||||
val userId: Int? = null
|
||||
)
|
||||
|
||||
object IndexesSerializer : KSerializer<List<HeaderIndex?>> {
|
||||
override val descriptor: SerialDescriptor = StringDescriptor
|
||||
internal object IndexesSerializer : KSerializer<List<HeaderIndex?>> {
|
||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): List<HeaderIndex?> {
|
||||
val json = decoder.decodeSerializableValue(JsonObjectSerializer)
|
||||
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
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class LimitsState(
|
||||
val maxShortQuota: Int,
|
||||
val maxLongQuota: Int,
|
||||
val knownShortQuota: Int,
|
||||
val knownLongQuota: Int
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
176
src/commonMain/kotlin/dev/inmo/saucenaoapi/models/ResultData.kt
Normal file
176
src/commonMain/kotlin/dev/inmo/saucenaoapi/models/ResultData.kt
Normal file
@@ -0,0 +1,176 @@
|
||||
package dev.inmo.saucenaoapi.models
|
||||
|
||||
import dev.inmo.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<String> = 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<String> = emptyList()
|
||||
)
|
||||
|
||||
val ResultData.froms: List<String>
|
||||
get() = material ?.split(", ") ?: emptyList()
|
||||
|
||||
val ResultData.authors: List<String>
|
||||
get() = (creator ?.split(", ") ?: emptyList()) + (memberName ?.split(", ") ?: emptyList())
|
||||
|
||||
val ResultData.charactersList: List<String>
|
||||
get() = characters ?.split(", ") ?: emptyList()
|
||||
|
||||
val ResultData.titles: List<String>
|
||||
get() = title ?.split(", ") ?: emptyList()
|
||||
|
||||
val ResultData.urls: List<String>
|
||||
get() = extUrls + (url ?.split(", ") ?: emptyList())
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.Input
|
||||
|
||||
expect class MPPFile
|
||||
|
||||
expect val MPPFile.input: Input
|
||||
expect val MPPFile.contentType: ContentType
|
||||
@@ -1,30 +1,43 @@
|
||||
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.models.Header
|
||||
import com.soywiz.klock.DateTime
|
||||
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 kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.io.core.Closeable
|
||||
import org.joda.time.DateTime
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
class RequestQuotaManager (
|
||||
private val scope: CoroutineScope
|
||||
) : Closeable {
|
||||
internal class RequestQuotaManager (
|
||||
scope: CoroutineScope
|
||||
) {
|
||||
private var longQuota = 1
|
||||
private var shortQuota = 1
|
||||
private var longMaxQuota = 1
|
||||
private var shortMaxQuota = 1
|
||||
|
||||
val limitsState: LimitsState
|
||||
get() = LimitsState(
|
||||
shortMaxQuota,
|
||||
longMaxQuota,
|
||||
shortQuota,
|
||||
longQuota
|
||||
)
|
||||
|
||||
private val quotaActions = Channel<suspend () -> Unit>(Channel.UNLIMITED)
|
||||
|
||||
private val quotaJob = scope.launch {
|
||||
for (callback in quotaActions) {
|
||||
callback()
|
||||
}
|
||||
}.also {
|
||||
it.invokeOnCompletion {
|
||||
quotaActions.close(it)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateQuota(
|
||||
@@ -43,11 +56,11 @@ class RequestQuotaManager (
|
||||
shortQuota = min(newShortQuota, shortMaxQuota)
|
||||
|
||||
when {
|
||||
longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()).millis + LONG_TIME_RECALCULATING_MILLIS
|
||||
shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()).millis + SHORT_TIME_RECALCULATING_MILLIS
|
||||
longQuota < 1 -> (timeManager.getMostOldestInLongPeriod() ?: DateTime.now()) + LONG_TIME_RECALCULATING_MILLIS
|
||||
shortQuota < 1 -> (timeManager.getMostOldestInShortPeriod() ?: DateTime.now()) + SHORT_TIME_RECALCULATING_MILLIS
|
||||
else -> null
|
||||
} ?.also {
|
||||
delay(it - DateTime.now().millis)
|
||||
delay((it - DateTime.now()).millisecondsLong)
|
||||
shortQuota = max(shortQuota, 1)
|
||||
longQuota = max(longQuota, 1)
|
||||
}
|
||||
@@ -64,8 +77,8 @@ class RequestQuotaManager (
|
||||
timeManager
|
||||
)
|
||||
|
||||
suspend fun happenTooManyRequests(timeManager: TimeManager) = updateQuota(
|
||||
1,
|
||||
suspend fun happenTooManyRequests(timeManager: TimeManager, e: TooManyRequestsException) = updateQuota(
|
||||
if (e is TooManyRequestsLongException) 0 else 1,
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
@@ -73,21 +86,16 @@ class RequestQuotaManager (
|
||||
)
|
||||
|
||||
suspend fun getQuota() {
|
||||
return suspendCoroutine {
|
||||
lateinit var callback: suspend () -> Unit
|
||||
callback = suspend {
|
||||
if (longQuota > 0 && shortQuota > 0) {
|
||||
it.resumeWith(Result.success(Unit))
|
||||
} else {
|
||||
quotaActions.send(callback)
|
||||
}
|
||||
val job = Job()
|
||||
lateinit var callback: suspend () -> Unit
|
||||
callback = suspend {
|
||||
if (longQuota > 0 && shortQuota > 0) {
|
||||
job.complete()
|
||||
} else {
|
||||
quotaActions.send(callback)
|
||||
}
|
||||
quotaActions.offer(callback)
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
quotaJob.cancel()
|
||||
quotaActions.close()
|
||||
quotaActions.trySend(callback)
|
||||
return job.join()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
|
||||
interface SauceCloseable {
|
||||
fun close()
|
||||
}
|
||||
|
||||
inline fun <T> SauceCloseable.use(block: (SauceCloseable) -> T): T = try {
|
||||
block(this)
|
||||
} finally {
|
||||
close()
|
||||
}
|
||||
|
||||
@Deprecated("Useless")
|
||||
suspend fun <T> SauceCloseable.useSafe(block: suspend (SauceCloseable) -> T): T = try {
|
||||
supervisorScope {
|
||||
block(this@useSafe)
|
||||
}
|
||||
} finally {
|
||||
close()
|
||||
}
|
||||
@@ -1,17 +1,13 @@
|
||||
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 kotlinx.coroutines.CoroutineScope
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import dev.inmo.saucenaoapi.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.io.core.Closeable
|
||||
import org.joda.time.DateTime
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
private fun MutableList<DateTime>.clearTooOldTimes(relatedTo: DateTime = DateTime.now()) {
|
||||
val limitValue = relatedTo.minusMillis(LONG_TIME_RECALCULATING_MILLIS)
|
||||
val limitValue = relatedTo - LONG_TIME_RECALCULATING_MILLIS
|
||||
|
||||
removeAll {
|
||||
it < limitValue
|
||||
@@ -39,37 +35,35 @@ private data class TimeManagerTimeAdder(
|
||||
}
|
||||
|
||||
private data class TimeManagerMostOldestInLongGetter(
|
||||
private val continuation: Continuation<DateTime?>
|
||||
private val deferred: CompletableDeferred<DateTime?>
|
||||
) : TimeManagerAction {
|
||||
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
|
||||
times.clearTooOldTimes()
|
||||
continuation.resumeWith(Result.success(times.min()))
|
||||
deferred.complete(times.minOrNull())
|
||||
}
|
||||
}
|
||||
|
||||
private data class TimeManagerMostOldestInShortGetter(
|
||||
private val continuation: Continuation<DateTime?>
|
||||
private val deferred: CompletableDeferred<DateTime?>
|
||||
) : TimeManagerAction {
|
||||
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
|
||||
times.clearTooOldTimes()
|
||||
|
||||
val now = DateTime.now()
|
||||
|
||||
val limitTime = now.minusMillis(SHORT_TIME_RECALCULATING_MILLIS)
|
||||
val limitTime = now - SHORT_TIME_RECALCULATING_MILLIS
|
||||
|
||||
continuation.resumeWith(
|
||||
Result.success(
|
||||
times.asSequence().filter {
|
||||
limitTime < it
|
||||
}.min()
|
||||
)
|
||||
deferred.complete(
|
||||
times.asSequence().filter {
|
||||
limitTime < it
|
||||
}.minOrNull()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class TimeManager(
|
||||
internal class TimeManager(
|
||||
scope: CoroutineScope
|
||||
) : Closeable {
|
||||
) {
|
||||
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)
|
||||
|
||||
private val timeUpdateJob = scope.launch {
|
||||
@@ -77,6 +71,10 @@ class TimeManager(
|
||||
for (action in actionsChannel) {
|
||||
action(times)
|
||||
}
|
||||
}.also {
|
||||
it.invokeOnCompletion {
|
||||
actionsChannel.close(it)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addTimeAndClear() {
|
||||
@@ -84,21 +82,20 @@ class TimeManager(
|
||||
}
|
||||
|
||||
suspend fun getMostOldestInLongPeriod(): DateTime? {
|
||||
return suspendCoroutine {
|
||||
actionsChannel.offer(
|
||||
TimeManagerMostOldestInLongGetter(it)
|
||||
)
|
||||
val deferred = CompletableDeferred<DateTime?>()
|
||||
return if (actionsChannel.trySend(TimeManagerMostOldestInLongGetter(deferred)).isSuccess) {
|
||||
deferred.await()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMostOldestInShortPeriod(): DateTime? {
|
||||
return suspendCoroutine {
|
||||
actionsChannel.offer(TimeManagerMostOldestInShortGetter(it))
|
||||
val deferred = CompletableDeferred<DateTime?>()
|
||||
return if (actionsChannel.trySend(TimeManagerMostOldestInShortGetter(deferred)).isSuccess) {
|
||||
deferred.await()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
actionsChannel.close()
|
||||
timeUpdateJob.cancel()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.ByteReadPacket
|
||||
import io.ktor.utils.io.core.Input
|
||||
import org.khronos.webgl.Int8Array
|
||||
import org.w3c.files.File
|
||||
import org.w3c.files.FileReaderSync
|
||||
|
||||
actual typealias MPPFile = File
|
||||
|
||||
actual val MPPFile.input: Input
|
||||
get() {
|
||||
val reader = FileReaderSync()
|
||||
return ByteReadPacket(Int8Array(reader.readAsArrayBuffer(this)) as ByteArray)
|
||||
}
|
||||
actual val MPPFile.contentType: ContentType
|
||||
get() = ContentType.parse(type)
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.inmo.saucenaoapi.utils
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.core.Input
|
||||
import io.ktor.utils.io.streams.asInput
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
|
||||
actual typealias MPPFile = File
|
||||
|
||||
actual val MPPFile.input: Input
|
||||
get() = inputStream().asInput()
|
||||
actual val MPPFile.contentType: ContentType
|
||||
get() = ContentType.parse(Files.probeContentType(toPath()))
|
||||
27
src/jvmTest/kotlin/Launcher.kt
Normal file
27
src/jvmTest/kotlin/Launcher.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
import dev.inmo.saucenaoapi.SauceNaoAPI
|
||||
import io.ktor.client.HttpClient
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
suspend fun main(vararg args: String) {
|
||||
val (key, requestSubject) = args
|
||||
|
||||
val client = HttpClient()
|
||||
val scope = CoroutineScope(Dispatchers.IO).also {
|
||||
it.coroutineContext.job.invokeOnCompletion {
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
|
||||
val api = SauceNaoAPI(key, client, scope = scope)
|
||||
println(
|
||||
when {
|
||||
requestSubject.startsWith("/") -> File(requestSubject).let {
|
||||
api.request(it)
|
||||
}
|
||||
else -> api.request(requestSubject)
|
||||
}
|
||||
)
|
||||
|
||||
scope.cancel()
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
suspend fun main(vararg args: String) {
|
||||
val (key, requestUrl) = args
|
||||
|
||||
val api = SauceNaoAPI(key, scope = GlobalScope)
|
||||
api.use {
|
||||
println(
|
||||
it.request(requestUrl)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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,165 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.*
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.calculateSleepTime
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.client.features.ClientRequestException
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.response.readText
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.io.core.Closeable
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.joda.time.DateTime
|
||||
import java.util.logging.Logger
|
||||
import kotlin.coroutines.*
|
||||
|
||||
private const val API_TOKEN_FIELD = "api_key"
|
||||
private const val OUTPUT_TYPE_FIELD = "output_type"
|
||||
private const val URL_FIELD = "url"
|
||||
private const val DB_FIELD = "db"
|
||||
private const val DBMASK_FIELD = "dbmask"
|
||||
private const val DBMASKI_FIELD = "dbmaski"
|
||||
private const val RESULTS_COUNT_FIELD = "numres"
|
||||
private const val MINIMAL_SIMILARITY_FIELD = "minsim"
|
||||
|
||||
private const val SEARCH_URL = "https://saucenao.com/search.php"
|
||||
|
||||
data class SauceNaoAPI(
|
||||
private val apiToken: String,
|
||||
private val outputType: OutputType = JsonOutputType,
|
||||
private val client: HttpClient = HttpClient(OkHttp),
|
||||
private val searchUrl: String = SEARCH_URL,
|
||||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : Closeable {
|
||||
private val logger = Logger.getLogger("SauceNaoAPI")
|
||||
|
||||
private val requestsChannel = Channel<Pair<Continuation<SauceNaoAnswer>, HttpRequestBuilder>>(Channel.UNLIMITED)
|
||||
private val timeManager = TimeManager(scope)
|
||||
private val quotaManager = RequestQuotaManager(scope)
|
||||
|
||||
private val requestsJob = scope.launch {
|
||||
for ((callback, requestBuilder) in requestsChannel) {
|
||||
quotaManager.getQuota()
|
||||
launch {
|
||||
try {
|
||||
val answer = makeRequest(requestBuilder)
|
||||
callback.resumeWith(Result.success(answer))
|
||||
|
||||
quotaManager.updateQuota(answer.header, timeManager)
|
||||
} catch (e: TooManyRequestsException) {
|
||||
quotaManager.happenTooManyRequests(timeManager)
|
||||
requestsChannel.send(callback to requestBuilder)
|
||||
} catch (e: Exception) {
|
||||
try {
|
||||
callback.resumeWith(Result.failure(e))
|
||||
} catch (e: IllegalStateException) { // may happen when already resumed and api was closed
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun request(
|
||||
url: String,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun requestByDb(
|
||||
url: String,
|
||||
db: Int,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
db = db,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun requestByMask(
|
||||
url: String,
|
||||
dbmask: Int,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
dbmask = dbmask,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
suspend fun requestByMaskI(
|
||||
url: String,
|
||||
dbmaski: Int,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? = makeRequest(
|
||||
url,
|
||||
dbmaski = dbmaski,
|
||||
resultsCount = resultsCount,
|
||||
minSimilarity = minSimilarity
|
||||
)
|
||||
|
||||
private suspend fun makeRequest(
|
||||
builder: HttpRequestBuilder
|
||||
): SauceNaoAnswer {
|
||||
return try {
|
||||
val call = client.execute(builder)
|
||||
val answerText = call.response.readText()
|
||||
logger.info(answerText)
|
||||
timeManager.addTimeAndClear()
|
||||
Json.nonstrict.parse(
|
||||
SauceNaoAnswer.serializer(),
|
||||
answerText
|
||||
)
|
||||
} catch (e: ClientRequestException) {
|
||||
throw e.sauceNaoAPIException
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun makeRequest(
|
||||
url: String,
|
||||
db: Int? = null,
|
||||
dbmask: Int? = null,
|
||||
dbmaski: Int? = null,
|
||||
resultsCount: Int? = null,
|
||||
minSimilarity: Float? = null
|
||||
): SauceNaoAnswer? {
|
||||
return suspendCoroutine<SauceNaoAnswer> {
|
||||
requestsChannel.offer(
|
||||
it to HttpRequestBuilder().apply {
|
||||
url(searchUrl)
|
||||
parameter(URL_FIELD, url)
|
||||
parameter(API_TOKEN_FIELD, apiToken)
|
||||
parameter(OUTPUT_TYPE_FIELD, outputType.typeCode)
|
||||
db ?.also { parameter(DB_FIELD, it) }
|
||||
dbmask ?.also { parameter(DBMASK_FIELD, it) }
|
||||
dbmaski ?.also { parameter(DBMASKI_FIELD, it) }
|
||||
resultsCount ?.also { parameter(RESULTS_COUNT_FIELD, it) }
|
||||
minSimilarity ?.also { parameter(MINIMAL_SIMILARITY_FIELD, it) }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
requestsChannel.close()
|
||||
client.close()
|
||||
requestsJob.cancel()
|
||||
timeManager.close()
|
||||
quotaManager.close()
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
typealias AccountType = Int
|
||||
const val defaultAccountType: AccountType = 1 // "basic"
|
||||
|
||||
typealias UserId = Int
|
||||
|
||||
const val SHORT_TIME_RECALCULATING_MILLIS = 30 * 1000
|
||||
const val LONG_TIME_RECALCULATING_MILLIS = 24 * 60 * 60 * 1000
|
||||
@@ -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,13 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.exceptions
|
||||
|
||||
import io.ktor.client.features.ClientRequestException
|
||||
import io.ktor.http.HttpStatusCode.Companion.TooManyRequests
|
||||
import kotlinx.io.IOException
|
||||
|
||||
val ClientRequestException.sauceNaoAPIException: Exception
|
||||
get() = when (response.status) {
|
||||
TooManyRequests -> TooManyRequestsException()
|
||||
else -> this
|
||||
}
|
||||
|
||||
class TooManyRequestsException : IOException()
|
||||
@@ -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<String> = emptyList()
|
||||
)
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.ArrayListSerializer
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
@Serializable(SauceNaoAnswerSerializer::class)
|
||||
data class SauceNaoAnswer(
|
||||
val header: Header,
|
||||
val results: List<Result> = emptyList(),
|
||||
val raw: JsonObject
|
||||
)
|
||||
|
||||
@Serializer(SauceNaoAnswer::class)
|
||||
object SauceNaoAnswerSerializer : KSerializer<SauceNaoAnswer> {
|
||||
private val resultsSerializer = ArrayListSerializer(Result.serializer())
|
||||
private const val headerField = "header"
|
||||
private const val resultsField = "results"
|
||||
private val serializer = Json.nonstrict
|
||||
|
||||
override fun deserialize(decoder: Decoder): SauceNaoAnswer {
|
||||
val raw = JsonObjectSerializer.deserialize(decoder)
|
||||
val header = serializer.fromJson(Header.serializer(), raw.getObject(headerField))
|
||||
val results = serializer.fromJson(resultsSerializer, raw.getArray(resultsField))
|
||||
|
||||
return SauceNaoAnswer(header, results, 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.internal.ArrayListSerializer
|
||||
import kotlinx.serialization.internal.StringSerializer
|
||||
|
||||
|
||||
@Serializer(String::class)
|
||||
object CommonMultivariantStringSerializer : KSerializer<String> by StringSerializer {
|
||||
private val stringArraySerializer = ArrayListSerializer(StringSerializer)
|
||||
|
||||
override fun deserialize(decoder: Decoder): String {
|
||||
return try {
|
||||
decoder.decodeSerializableValue(StringSerializer)
|
||||
} catch (e: Exception) {
|
||||
decoder.decodeSerializableValue(stringArraySerializer).joinToString()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.utils
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.*
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
|
||||
@Serializer(forClass = JsonElement::class)
|
||||
internal object JsonElementSerializer : KSerializer<JsonElement> {
|
||||
override val descriptor: SerialDescriptor = object : SerialClassDescImpl("JsonElementSerializer") {
|
||||
override val kind: SerialKind
|
||||
get() = UnionKind.SEALED
|
||||
|
||||
init {
|
||||
addElement("JsonElement")
|
||||
}
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonElement) {
|
||||
when (obj) {
|
||||
is JsonPrimitive -> JsonPrimitiveSerializer.serialize(encoder, obj)
|
||||
is JsonObject -> JsonObjectSerializer.serialize(encoder, obj)
|
||||
is JsonArray -> JsonArraySerializer.serialize(encoder, obj)
|
||||
}
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonElement {
|
||||
val input = decoder as? JsonInput ?: error("JsonElement is deserializable only when used by Json")
|
||||
return input.decodeJson()
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonPrimitive::class)
|
||||
internal object JsonPrimitiveSerializer : KSerializer<JsonPrimitive> {
|
||||
override val descriptor: SerialDescriptor =
|
||||
JsonPrimitiveDescriptor
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonPrimitive) {
|
||||
return if (obj is JsonNull) {
|
||||
JsonNullSerializer.serialize(encoder, JsonNull)
|
||||
} else {
|
||||
JsonLiteralSerializer.serialize(encoder, obj as JsonLiteral)
|
||||
}
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonPrimitive {
|
||||
return if (decoder.decodeNotNullMark()) JsonPrimitive(decoder.decodeString())
|
||||
else JsonNullSerializer.deserialize(decoder)
|
||||
}
|
||||
|
||||
private object JsonPrimitiveDescriptor : SerialClassDescImpl("JsonPrimitive") {
|
||||
override val kind: SerialKind
|
||||
get() = PrimitiveKind.STRING
|
||||
|
||||
override val isNullable: Boolean
|
||||
get() = true
|
||||
|
||||
init {
|
||||
JsonPrimitiveSerializer.JsonPrimitiveDescriptor.addElement("JsonPrimitive")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonNull::class)
|
||||
internal object JsonNullSerializer : KSerializer<JsonNull> {
|
||||
override val descriptor: SerialDescriptor =
|
||||
JsonNullDescriptor
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonNull) {
|
||||
encoder.encodeNull()
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonNull {
|
||||
decoder.decodeNull()
|
||||
return JsonNull
|
||||
}
|
||||
|
||||
private object JsonNullDescriptor : SerialClassDescImpl("JsonNull") {
|
||||
override val kind: SerialKind
|
||||
get() = UnionKind.OBJECT
|
||||
|
||||
override val isNullable: Boolean
|
||||
get() = true
|
||||
|
||||
init {
|
||||
JsonNullSerializer.JsonNullDescriptor.addElement("JsonNull")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonLiteral::class)
|
||||
internal object JsonLiteralSerializer : KSerializer<JsonLiteral> {
|
||||
|
||||
override val descriptor: SerialDescriptor =
|
||||
JsonLiteralDescriptor
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonLiteral) {
|
||||
if (obj.isString) {
|
||||
return encoder.encodeString(obj.content)
|
||||
}
|
||||
|
||||
val integer = obj.intOrNull
|
||||
if (integer != null) {
|
||||
return encoder.encodeInt(integer)
|
||||
}
|
||||
|
||||
val double = obj.doubleOrNull
|
||||
if (double != null) {
|
||||
return encoder.encodeDouble(double)
|
||||
}
|
||||
|
||||
val boolean = obj.booleanOrNull
|
||||
if (boolean != null) {
|
||||
return encoder.encodeBoolean(boolean)
|
||||
}
|
||||
|
||||
encoder.encodeString(obj.content)
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonLiteral {
|
||||
return JsonLiteral(decoder.decodeString())
|
||||
}
|
||||
|
||||
private object JsonLiteralDescriptor : SerialClassDescImpl("JsonLiteral") {
|
||||
override val kind: SerialKind
|
||||
get() = PrimitiveKind.STRING
|
||||
|
||||
init {
|
||||
JsonLiteralSerializer.JsonLiteralDescriptor.addElement("JsonLiteral")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonObject::class)
|
||||
internal object JsonObjectSerializer : KSerializer<JsonObject> {
|
||||
override val descriptor: SerialDescriptor =
|
||||
NamedMapClassDescriptor("JsonObject", StringSerializer.descriptor,
|
||||
JsonElementSerializer.descriptor)
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonObject) {
|
||||
LinkedHashMapSerializer(StringSerializer, JsonElementSerializer).serialize(encoder, obj.content)
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonObject {
|
||||
return JsonObject(LinkedHashMapSerializer(StringSerializer, JsonElementSerializer).deserialize(decoder))
|
||||
}
|
||||
}
|
||||
|
||||
@Serializer(forClass = JsonArray::class)
|
||||
internal object JsonArraySerializer : KSerializer<JsonArray> {
|
||||
|
||||
override val descriptor: SerialDescriptor = NamedListClassDescriptor("JsonArray",
|
||||
JsonElementSerializer.descriptor)
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: JsonArray) {
|
||||
ArrayListSerializer(JsonElementSerializer).serialize(encoder, obj)
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): JsonArray {
|
||||
return JsonArray(ArrayListSerializer(JsonElementSerializer).deserialize(decoder))
|
||||
}
|
||||
}
|
||||
@@ -1,18 +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 org.joda.time.DateTime
|
||||
|
||||
internal suspend fun calculateSleepTime(
|
||||
header: Header,
|
||||
mostOldestInShortPeriodGetter: suspend () -> DateTime?,
|
||||
mostOldestInLongPeriodGetter: suspend () -> DateTime?
|
||||
): DateTime? {
|
||||
return when {
|
||||
header.longRemaining < 1 -> mostOldestInLongPeriodGetter() ?.plusMillis(LONG_TIME_RECALCULATING_MILLIS)
|
||||
header.shortRemaining < 1 -> mostOldestInShortPeriodGetter() ?.plusMillis(SHORT_TIME_RECALCULATING_MILLIS)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user