From cbb3f2cc39d755ab189a4ae57a7a0f870665a6b4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 20 Feb 2019 20:18:01 +0800 Subject: [PATCH] main classes --- .../insanusmokrassar/SauceNaoAPI/Launcher.kt | 16 +++ .../SauceNaoAPI/OutputType.kt | 17 +++ .../SauceNaoAPI/SauceNaoAPI.kt | 100 ++++++++++++++++++ .../models/{Result.kt => SauceNaoAnswer.kt} | 2 +- 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt rename src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/{Result.kt => SauceNaoAnswer.kt} (85%) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt new file mode 100644 index 0000000..81e0133 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/Launcher.kt @@ -0,0 +1,16 @@ +package com.github.insanusmokrassar.SauceNaoAPI + +import kotlinx.coroutines.runBlocking + +fun main(vararg args: String) { + val key = args.first() + val api = SauceNaoAPI(key) + + runBlocking { + api.request( + args[1] + ).also { + println(it) + } + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt new file mode 100644 index 0000000..841c06f --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/OutputType.kt @@ -0,0 +1,17 @@ +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 +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt new file mode 100644 index 0000000..c976d16 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt @@ -0,0 +1,100 @@ +package com.github.insanusmokrassar.SauceNaoAPI + +import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer +import io.ktor.client.HttpClient +import io.ktor.client.call.call +import io.ktor.client.engine.okhttp.OkHttp +import io.ktor.client.request.parameter +import io.ktor.client.request.url +import io.ktor.client.response.readText +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 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 +) { + 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( + url: String, + db: Int? = null, + dbmask: Int? = null, + dbmaski: Int? = null, + resultsCount: Int? = null, + minSimilarity: Float? = null + ): SauceNaoAnswer? { + return client.call { + 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) } + }.response.readText().let { + JSON.nonstrict.parse( + SauceNaoAnswer.serializer(), + it + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Result.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt similarity index 85% rename from src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Result.kt rename to src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt index c5bab7e..9834cb7 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/Result.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/models/SauceNaoAnswer.kt @@ -3,7 +3,7 @@ package com.github.insanusmokrassar.SauceNaoAPI.models import kotlinx.serialization.Serializable @Serializable -data class Result( +data class SauceNaoAnswer( val header: Header, val data: ResultData )