2020-12-02 08:39:54 +00:00
|
|
|
package dev.inmo.saucenaoapi
|
2019-02-20 12:18:01 +00:00
|
|
|
|
2020-12-02 08:39:54 +00:00
|
|
|
import dev.inmo.saucenaoapi.exceptions.TooManyRequestsException
|
|
|
|
import dev.inmo.saucenaoapi.exceptions.sauceNaoAPIException
|
|
|
|
import dev.inmo.saucenaoapi.models.*
|
|
|
|
import dev.inmo.saucenaoapi.utils.*
|
2019-02-20 12:18:01 +00:00
|
|
|
import io.ktor.client.HttpClient
|
2022-05-14 07:43:28 +00:00
|
|
|
import io.ktor.client.plugins.ClientRequestException
|
2019-08-19 08:13:43 +00:00
|
|
|
import io.ktor.client.request.*
|
2020-08-29 13:09:54 +00:00
|
|
|
import io.ktor.client.request.forms.MultiPartFormDataContent
|
|
|
|
import io.ktor.client.request.forms.formData
|
2022-05-14 07:43:28 +00:00
|
|
|
import io.ktor.client.statement.*
|
2019-12-12 18:47:52 +00:00
|
|
|
import io.ktor.http.*
|
2020-08-13 05:17:11 +00:00
|
|
|
import io.ktor.utils.io.core.Input
|
2019-08-19 08:13:43 +00:00
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.coroutines.channels.Channel
|
2019-03-21 04:25:23 +00:00
|
|
|
import kotlinx.serialization.json.Json
|
2019-02-20 12:18:01 +00:00
|
|
|
|
|
|
|
private const val API_TOKEN_FIELD = "api_key"
|
|
|
|
private const val OUTPUT_TYPE_FIELD = "output_type"
|
|
|
|
private const val URL_FIELD = "url"
|
2019-12-12 18:47:52 +00:00
|
|
|
private const val FILE_FIELD = "file"
|
|
|
|
private const val FILENAME_FIELD = "filename"
|
2019-02-20 12:18:01 +00:00
|
|
|
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"
|
|
|
|
|
2020-08-22 17:00:07 +00:00
|
|
|
val defaultSauceNaoParser = Json {
|
|
|
|
allowSpecialFloatingPointValues = true
|
|
|
|
allowStructuredMapKeys = true
|
|
|
|
ignoreUnknownKeys = true
|
|
|
|
useArrayPolymorphism = true
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:18:01 +00:00
|
|
|
data class SauceNaoAPI(
|
2019-12-13 16:51:16 +00:00
|
|
|
private val apiToken: String? = null,
|
2020-08-22 17:00:07 +00:00
|
|
|
private val client: HttpClient = HttpClient(),
|
2019-10-10 18:10:19 +00:00
|
|
|
private val searchUrl: String = SEARCH_URL,
|
2020-08-22 17:00:07 +00:00
|
|
|
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
|
|
|
private val parser: Json = defaultSauceNaoParser
|
2020-08-29 13:09:54 +00:00
|
|
|
) : SauceCloseable {
|
2022-05-14 07:43:28 +00:00
|
|
|
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)
|
2019-08-19 08:13:43 +00:00
|
|
|
|
2019-12-13 18:29:12 +00:00
|
|
|
val limitsState: LimitsState
|
|
|
|
get() = quotaManager.limitsState
|
|
|
|
|
2022-05-14 07:43:28 +00:00
|
|
|
private val requestsJob = subscope.launch {
|
2019-10-12 06:30:02 +00:00
|
|
|
for ((callback, requestBuilder) in requestsChannel) {
|
2019-10-12 06:32:17 +00:00
|
|
|
quotaManager.getQuota()
|
|
|
|
launch {
|
2019-10-12 06:30:02 +00:00
|
|
|
try {
|
2019-10-12 06:32:17 +00:00
|
|
|
val answer = makeRequest(requestBuilder)
|
2022-05-14 07:43:28 +00:00
|
|
|
callback.complete(answer)
|
2019-10-12 06:32:17 +00:00
|
|
|
|
|
|
|
quotaManager.updateQuota(answer.header, timeManager)
|
2019-10-12 07:43:28 +00:00
|
|
|
} catch (e: TooManyRequestsException) {
|
2019-12-13 18:21:12 +00:00
|
|
|
quotaManager.happenTooManyRequests(timeManager, e)
|
2019-10-12 07:43:28 +00:00
|
|
|
requestsChannel.send(callback to requestBuilder)
|
2019-10-12 06:32:17 +00:00
|
|
|
} catch (e: Exception) {
|
|
|
|
try {
|
2022-05-14 07:43:28 +00:00
|
|
|
callback.completeExceptionally(e)
|
2019-10-12 06:32:17 +00:00
|
|
|
} catch (e: IllegalStateException) { // may happen when already resumed and api was closed
|
|
|
|
// do nothing
|
|
|
|
}
|
2019-08-19 08:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:18:01 +00:00
|
|
|
suspend fun request(
|
|
|
|
url: String,
|
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
2022-05-14 07:43:28 +00:00
|
|
|
): SauceNaoAnswer = makeRequest(
|
2019-12-12 18:47:52 +00:00
|
|
|
url.asSauceRequestSubject,
|
|
|
|
resultsCount = resultsCount,
|
|
|
|
minSimilarity = minSimilarity
|
|
|
|
)
|
|
|
|
|
|
|
|
suspend fun request(
|
|
|
|
mediaInput: Input,
|
2020-08-22 17:00:07 +00:00
|
|
|
mimeType: ContentType,
|
2019-12-12 18:47:52 +00:00
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
2022-05-14 07:43:28 +00:00
|
|
|
): SauceNaoAnswer = makeRequest(
|
2019-12-12 18:47:52 +00:00
|
|
|
mediaInput.asSauceRequestSubject(mimeType),
|
2019-02-20 12:18:01 +00:00
|
|
|
resultsCount = resultsCount,
|
|
|
|
minSimilarity = minSimilarity
|
|
|
|
)
|
|
|
|
|
2022-05-14 07:43:28 +00:00
|
|
|
suspend fun request(
|
|
|
|
file: MPPFile,
|
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
|
|
|
): SauceNaoAnswer = request(
|
|
|
|
file.input,
|
|
|
|
file.contentType,
|
|
|
|
resultsCount = resultsCount,
|
|
|
|
minSimilarity = minSimilarity
|
|
|
|
)
|
|
|
|
|
2019-02-20 12:18:01 +00:00
|
|
|
suspend fun requestByDb(
|
|
|
|
url: String,
|
|
|
|
db: Int,
|
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
2022-05-14 07:43:28 +00:00
|
|
|
): SauceNaoAnswer = makeRequest(
|
2019-12-12 18:47:52 +00:00
|
|
|
url.asSauceRequestSubject,
|
2019-02-20 12:18:01 +00:00
|
|
|
db = db,
|
|
|
|
resultsCount = resultsCount,
|
|
|
|
minSimilarity = minSimilarity
|
|
|
|
)
|
|
|
|
|
|
|
|
suspend fun requestByMask(
|
|
|
|
url: String,
|
|
|
|
dbmask: Int,
|
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
2022-05-14 07:43:28 +00:00
|
|
|
): SauceNaoAnswer = makeRequest(
|
2019-12-12 18:47:52 +00:00
|
|
|
url.asSauceRequestSubject,
|
2019-02-20 12:18:01 +00:00
|
|
|
dbmask = dbmask,
|
|
|
|
resultsCount = resultsCount,
|
|
|
|
minSimilarity = minSimilarity
|
|
|
|
)
|
|
|
|
|
|
|
|
suspend fun requestByMaskI(
|
|
|
|
url: String,
|
|
|
|
dbmaski: Int,
|
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
2022-05-14 07:43:28 +00:00
|
|
|
): SauceNaoAnswer = makeRequest(
|
2019-12-12 18:47:52 +00:00
|
|
|
url.asSauceRequestSubject,
|
2019-02-20 12:18:01 +00:00
|
|
|
dbmaski = dbmaski,
|
|
|
|
resultsCount = resultsCount,
|
|
|
|
minSimilarity = minSimilarity
|
|
|
|
)
|
|
|
|
|
2019-08-19 08:13:43 +00:00
|
|
|
private suspend fun makeRequest(
|
|
|
|
builder: HttpRequestBuilder
|
|
|
|
): SauceNaoAnswer {
|
|
|
|
return try {
|
2022-05-14 07:43:28 +00:00
|
|
|
val call = client.request(builder)
|
|
|
|
val answerText = call.bodyAsText()
|
2019-10-12 06:30:02 +00:00
|
|
|
timeManager.addTimeAndClear()
|
2020-08-22 17:00:07 +00:00
|
|
|
parser.decodeFromString(
|
2019-10-12 10:28:27 +00:00
|
|
|
SauceNaoAnswerSerializer,
|
2019-08-19 08:13:43 +00:00
|
|
|
answerText
|
|
|
|
)
|
|
|
|
} catch (e: ClientRequestException) {
|
2019-12-13 18:21:12 +00:00
|
|
|
throw e.sauceNaoAPIException()
|
2019-08-19 08:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:18:01 +00:00
|
|
|
private suspend fun makeRequest(
|
2019-12-12 18:47:52 +00:00
|
|
|
request: SauceRequestSubject,
|
2019-02-20 12:18:01 +00:00
|
|
|
db: Int? = null,
|
|
|
|
dbmask: Int? = null,
|
|
|
|
dbmaski: Int? = null,
|
|
|
|
resultsCount: Int? = null,
|
|
|
|
minSimilarity: Float? = null
|
2022-05-14 07:43:28 +00:00
|
|
|
): 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 {
|
2019-12-12 18:47:52 +00:00
|
|
|
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
|
|
|
|
)
|
2022-05-14 07:43:28 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2019-12-12 18:47:52 +00:00
|
|
|
}
|
2019-08-19 08:13:43 +00:00
|
|
|
}
|
2022-05-14 07:43:28 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return deferred.await()
|
2019-02-20 12:18:01 +00:00
|
|
|
}
|
2019-08-19 08:13:43 +00:00
|
|
|
|
|
|
|
override fun close() {
|
2022-05-14 07:43:28 +00:00
|
|
|
subscope.cancel()
|
2019-08-19 08:13:43 +00:00
|
|
|
}
|
2021-05-26 16:16:08 +00:00
|
|
|
}
|