SauceNaoAPI/src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt

365 lines
14 KiB
Kotlin
Raw Normal View History

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:44:44 +00:00
import io.ktor.client.statement.bodyAsText
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"
2022-12-22 09:55:32 +00:00
private const val DBS_FIELD = "dbs[]"
2019-02-20 12:18:01 +00:00
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(),
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
val limitsState: LimitsState by quotaManager::limitsState
val longQuotaFlow by quotaManager::longQuotaFlow
val shortQuotaFlow by quotaManager::shortQuotaFlow
val longMaxQuotaFlow by quotaManager::longMaxQuotaFlow
val shortMaxQuotaFlow by quotaManager::shortMaxQuotaFlow
val limitsStateFlow by quotaManager::limitsStateFlow
2019-12-13 18:29:12 +00:00
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)
} catch (e: TooManyRequestsException) {
2019-12-13 18:21:12 +00:00
quotaManager.happenTooManyRequests(timeManager, e)
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
)
2022-12-22 09:55:32 +00:00
/**
* @param db search a specific index number or all without needing to generate a bitmask.
* @param dbs search one or more specific index number, set more than once to search multiple.
*/
suspend fun requestByDBs(
url: String,
db: Int? = null,
dbs: Array<Int>? = null,
resultsCount: Int? = null,
minSimilarity: Float? = null
): SauceNaoAnswer = makeRequest(
url.asSauceRequestSubject,
db = db,
dbs = dbs,
resultsCount = resultsCount,
minSimilarity = minSimilarity
)
/**
* @param mask Mask for selecting specific indexes to ENABLE. dbmask=8191 will search all of the first 14 indexes. If intending to search all databases, the db=999 option is more appropriate.
* @param excludedMask Mask for selecting specific indexes to DISABLE. dbmaski=8191 would search only indexes higher than the first 14. This is ideal when attempting to disable only certain indexes, while allowing future indexes to be included by default.
*
* Bitmask Note: Index numbers start with 0. Even though pixiv is labeled as index 5, it would be controlled with the 6th bit position, which has a decimal value of 32 when set.
* db=<index num or 999 for all>
*/
suspend fun requestByMasks(
url: String,
mask: Int?,
excludedMask: Int? = null,
resultsCount: Int? = null,
minSimilarity: Float? = null
): SauceNaoAnswer = makeRequest(
url.asSauceRequestSubject,
dbmask = mask,
dbmaski = excludedMask,
resultsCount = resultsCount,
minSimilarity = minSimilarity
)
2019-12-12 18:47:52 +00:00
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-12-22 09:55:32 +00:00
/**
* @param db search a specific index number or all without needing to generate a bitmask.
* @param dbs search one or more specific index number, set more than once to search multiple.
*/
suspend fun requestByDBs(
mediaInput: Input,
mimeType: ContentType,
db: Int? = null,
dbs: Array<Int>? = null,
resultsCount: Int? = null,
minSimilarity: Float? = null
): SauceNaoAnswer = makeRequest(
mediaInput.asSauceRequestSubject(mimeType),
db = db,
dbs = dbs,
resultsCount = resultsCount,
minSimilarity = minSimilarity
)
/**
* @param mask Mask for selecting specific indexes to ENABLE. dbmask=8191 will search all of the first 14 indexes. If intending to search all databases, the db=999 option is more appropriate.
* @param excludedMask Mask for selecting specific indexes to DISABLE. dbmaski=8191 would search only indexes higher than the first 14. This is ideal when attempting to disable only certain indexes, while allowing future indexes to be included by default.
*
* Bitmask Note: Index numbers start with 0. Even though pixiv is labeled as index 5, it would be controlled with the 6th bit position, which has a decimal value of 32 when set.
* db=<index num or 999 for all>
*/
suspend fun requestByMasks(
mediaInput: Input,
mimeType: ContentType,
mask: Int?,
excludedMask: Int? = null,
resultsCount: Int? = null,
minSimilarity: Float? = null
): SauceNaoAnswer = makeRequest(
mediaInput.asSauceRequestSubject(mimeType),
dbmask = mask,
dbmaski = excludedMask,
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,
2022-12-22 09:55:32 +00:00
resultsCount,
minSimilarity
)
/**
* @param db search a specific index number or all without needing to generate a bitmask.
* @param dbs search one or more specific index number, set more than once to search multiple.
*/
suspend fun requestByDBs(
file: MPPFile,
db: Int? = null,
dbs: Array<Int>? = null,
resultsCount: Int? = null,
minSimilarity: Float? = null
): SauceNaoAnswer = requestByDBs(
file.input,
file.contentType,
db,
dbs,
resultsCount,
minSimilarity
)
/**
* @param mask Mask for selecting specific indexes to ENABLE. dbmask=8191 will search all of the first 14 indexes. If intending to search all databases, the db=999 option is more appropriate.
* @param excludedMask Mask for selecting specific indexes to DISABLE. dbmaski=8191 would search only indexes higher than the first 14. This is ideal when attempting to disable only certain indexes, while allowing future indexes to be included by default.
*
* Bitmask Note: Index numbers start with 0. Even though pixiv is labeled as index 5, it would be controlled with the 6th bit position, which has a decimal value of 32 when set.
* db=<index num or 999 for all>
*/
suspend fun requestByMasks(
file: MPPFile,
mask: Int?,
excludedMask: Int? = null,
resultsCount: Int? = null,
minSimilarity: Float? = null
): SauceNaoAnswer = requestByMasks(
file.input,
file.contentType,
mask,
excludedMask,
resultsCount,
minSimilarity
2022-05-14 07:43:28 +00:00
)
2022-12-22 09:55:32 +00:00
@Deprecated("Renamed", ReplaceWith("requestByDBs(url, db, null, resultsCount, minSimilarity)"))
2019-02-20 12:18:01 +00:00
suspend fun requestByDb(
url: String,
db: Int,
resultsCount: Int? = null,
minSimilarity: Float? = null
2022-12-22 09:55:32 +00:00
): SauceNaoAnswer = requestByDBs(
url,
db,
null,
resultsCount,
minSimilarity
2019-02-20 12:18:01 +00:00
)
2022-12-22 09:55:32 +00:00
@Deprecated("Renamed", ReplaceWith("requestByMasks(url, dbmask, null, resultsCount, minSimilarity)"))
suspend fun request(
2019-02-20 12:18:01 +00:00
url: String,
dbmask: Int,
resultsCount: Int? = null,
minSimilarity: Float? = null
2022-12-22 09:55:32 +00:00
): SauceNaoAnswer = requestByMasks(
url,
dbmask,
null,
resultsCount,
minSimilarity
2019-02-20 12:18:01 +00:00
)
2022-12-22 09:55:32 +00:00
@Deprecated("Renamed", ReplaceWith("requestByMasks(url, null, dbmaski, resultsCount, minSimilarity)"))
2019-02-20 12:18:01 +00:00
suspend fun requestByMaskI(
url: String,
dbmaski: Int,
resultsCount: Int? = null,
minSimilarity: Float? = null
2022-12-22 09:55:32 +00:00
): SauceNaoAnswer = requestByMasks(
url,
null,
dbmaski,
resultsCount,
minSimilarity
2019-02-20 12:18:01 +00:00
)
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,
2022-12-22 09:55:32 +00:00
dbs: Array<Int>? = null,
2019-02-20 12:18:01 +00:00
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) }
2022-12-22 09:55:32 +00:00
dbs ?.forEach { parameter(DBS_FIELD, it) }
2022-05-14 07:43:28 +00:00
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
}