Switched to JokeAPI library

This commit is contained in:
Erik C. Thauvin 2022-09-24 10:54:39 -07:00
parent b9deb55de6
commit 9bb7b34c69
5 changed files with 22 additions and 20 deletions

View file

@ -80,6 +80,7 @@ dependencies {
// Thauvin
implementation 'net.thauvin.erik:cryptoprice:1.0.0'
implementation 'net.thauvin.erik:jokeapi:0.9-SNAPSHOT'
implementation 'net.thauvin.erik:pinboard-poster:1.0.3'
testImplementation 'com.willowtreeapps.assertk:assertk-jvm:0.25'

View file

@ -70,6 +70,7 @@
<ID>SwallowedException:StockQuoteTest.kt$StockQuoteTest$e: ModuleException</ID>
<ID>SwallowedException:WolframAlphaTest.kt$WolframAlphaTest$e: ModuleException</ID>
<ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List&lt;Message&gt;</ID>
<ID>ThrowsCount:Joke.kt$Joke.Companion$@JvmStatic @Throws(ModuleException::class) fun randomJoke(): List&lt;Message&gt;</ID>
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List&lt;Message&gt;</ID>
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID>
<ID>ThrowsCount:Weather2.kt$Weather2.Companion$@JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List&lt;Message&gt;</ID>

View file

@ -33,6 +33,11 @@ package net.thauvin.erik.mobibot.modules
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.jokeapi.JokeApi
import net.thauvin.erik.jokeapi.JokeApi.Companion.getJoke
import net.thauvin.erik.jokeapi.exceptions.HttpErrorException
import net.thauvin.erik.jokeapi.exceptions.JokeException
import net.thauvin.erik.jokeapi.models.Type
import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.colorize
import net.thauvin.erik.mobibot.Utils.helpFormat
@ -64,7 +69,7 @@ class Joke : ThreadedModule() {
}
/**
* Returns a random joke from [JokeAPI](https://sv443.net/jokeapi/v2/).
* Returns a random joke from [JokeAPI](https://v2.jokeapi.dev/).
*/
override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
with(event.bot()) {
@ -85,10 +90,6 @@ class Joke : ThreadedModule() {
// Joke command
private const val JOKE_CMD = "joke"
// ICNDB URL
private const val JOKE_URL =
"https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=single"
/**
* Retrieves a random joke.
*/
@ -97,28 +98,26 @@ class Joke : ThreadedModule() {
fun randomJoke(): List<Message> {
return try {
val messages = mutableListOf<Message>()
val url = URL(JOKE_URL)
val json = JSONObject(url.reader().body)
if (json.has("joke")) {
val joke = json.getString("joke").split("\n")
joke.forEach {
messages.add(PublicMessage(it, Colors.CYAN))
}
} else {
messages.add(ErrorMessage(json.getString("message"), Colors.RED))
val joke = getJoke(safe = true, type = Type.SINGLE)
joke.joke.forEach {
messages.add(PublicMessage(it, Colors.CYAN))
}
messages
} catch (e: JokeException) {
throw ModuleException("randomJoke(): ${e.additionalInfo}", e.message, e)
} catch (e: HttpErrorException) {
throw ModuleException("randomJoke(): HTTP: ${e.statusCode}", e.message, e)
} catch (e: IOException) {
throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e)
} catch (e: JSONException) {
throw ModuleException("randomJoke(): JSON", "An JSON error has occurred retrieving a random joke.", e)
throw ModuleException("randomJoke(): JSON", "A parsing error has occurred retrieving a random joke.", e)
}
}
}
init {
commands.add(JOKE_CMD)
help.add("To retrieve a random joke:")
help.add("To display a random joke:")
help.add(helpFormat("%c $JOKE_CMD"))
}
}

View file

@ -47,10 +47,10 @@ class JokeTest {
@Throws(ModuleException::class)
fun testRandomJoke() {
val joke = randomJoke()
assertThat(joke.size, "joke is empty").isGreaterThan(0)
assertThat(joke.size, "joke should not be empty").isGreaterThan(0)
joke.forEach {
assertThat(it, "is not a public message").isInstanceOf(PublicMessage::class.java)
assertThat(it.msg, "msg is empty").isNotEmpty()
assertThat(it, "message should be public").isInstanceOf(PublicMessage::class.java)
assertThat(it.msg, "message should not be empty").isNotEmpty()
}
}
}

View file

@ -39,6 +39,7 @@
<li><a href="https://commons.apache.org/proper/commons-net/">Apache Commons Net</a></li>
<li><a href="https://github.com/ethauvin/cryptoprice">CryptoPrice</a></li>
<li><a href="https://www.objecthunter.net/exp4j/">exp4j</a></li>
<li><a href="https://github.com/ethauvin/jokeapi">JokeAPI</a></li>
<li><a href="https://jsoup.org/">jsoup</a></li>
<li><a href="https://github.com/Kotlin/kotlinx-cli">kotlinx-cli</a></li>
<li><a href="https://square.github.io/okhttp/">OkHttp</a></li>
@ -112,7 +113,7 @@
<li>Viewing when a nickname was last seen
<div><code>/msg mobibot seen nickname</code></div>
</li>
<li>Random jokes from <a href="https://sv443.net/jokeapi/v2/">JokeAPI</a>
<li>Random jokes from <a href="https://v2.jokeapi.dev/">Sv443's JokeAPI</a>
<div><code>mobibot: joke</code></div>
</li>
<li>Rolling dice or Playing war and rock paper scissors