From 0dd02d7039a06ed3585ebd3b850ec38c5eab9aeb Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 14 Feb 2022 22:34:09 -0800 Subject: [PATCH] Added URL reader and encoder extension funtions. --- .../kotlin/net/thauvin/erik/mobibot/Utils.kt | 6 ++--- .../erik/mobibot/commands/ChannelFeed.kt | 2 +- .../erik/mobibot/modules/GoogleSearch.kt | 6 ++--- .../net/thauvin/erik/mobibot/modules/Joke.kt | 4 ++-- .../erik/mobibot/modules/StockQuote.kt | 24 ++++++++----------- .../thauvin/erik/mobibot/modules/Weather2.kt | 2 +- .../net/thauvin/erik/mobibot/PinboardTest.kt | 10 ++++---- .../net/thauvin/erik/mobibot/UtilsTest.kt | 5 ++-- 8 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index d47760a..20286c1 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -145,7 +145,7 @@ object Utils { * URL encodes the given string. */ @JvmStatic - fun encodeUrl(s: String): String = URLEncoder.encode(s, StandardCharsets.UTF_8) + fun String.encodeUrl(): String = URLEncoder.encode(this, StandardCharsets.UTF_8) /** * Returns a property as an int. @@ -347,8 +347,8 @@ object Utils { */ @JvmStatic @Throws(IOException::class) - fun urlReader(url: URL): String { - BufferedReader(InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) + fun URL.reader(): String { + BufferedReader(InputStreamReader(this.openStream(), StandardCharsets.UTF_8)) .use { reader -> return reader.lines().collect(Collectors.joining(System.lineSeparator())) } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt index cc80595..3cc1b1a 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt @@ -57,7 +57,7 @@ class ChannelFeed(channel: String) : AbstractCommand() { if (isEnabled()) { runBlocking { launch { - FeedReader(properties[FEED_PROP]!!, event).run() + properties[FEED_PROP]?.let { FeedReader(it, event).run() } } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index 742e6d9..09091cb 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -34,9 +34,9 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.Utils.capitalise import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.helpFormat +import net.thauvin.erik.mobibot.Utils.reader import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.unescapeXml -import net.thauvin.erik.mobibot.Utils.urlReader import net.thauvin.erik.mobibot.msg.ErrorMessage import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.NoticeMessage @@ -108,9 +108,9 @@ class GoogleSearch : ThreadedModule() { try { val url = URL( "https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$cseKey" + - "&q=${encodeUrl(query)}&filter=1&num=5&alt=json" + "&q=${query.encodeUrl()}&filter=1&num=5&alt=json" ) - val json = JSONObject(urlReader(url)) + val json = JSONObject(url.reader()) if (json.has("items")) { val ja = json.getJSONArray("items") for (i in 0 until ja.length()) { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt index 6922698..c2f15b4 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt @@ -36,8 +36,8 @@ import kotlinx.coroutines.runBlocking import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.cyan import net.thauvin.erik.mobibot.Utils.helpFormat +import net.thauvin.erik.mobibot.Utils.reader import net.thauvin.erik.mobibot.Utils.sendMessage -import net.thauvin.erik.mobibot.Utils.urlReader import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.PublicMessage import org.json.JSONException @@ -94,7 +94,7 @@ class Joke : ThreadedModule() { fun randomJoke(): Message { return try { val url = URL(JOKE_URL) - val json = JSONObject(urlReader(url)) + val json = JSONObject(url.reader()) PublicMessage( json.getJSONObject("value")["joke"].toString().replace("\\'", "'") .replace("\\\"", "\"") diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index 030674f..ce53561 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -34,9 +34,9 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.Utils.capitalise import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.helpFormat +import net.thauvin.erik.mobibot.Utils.reader import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.unescapeXml -import net.thauvin.erik.mobibot.Utils.urlReader import net.thauvin.erik.mobibot.msg.ErrorMessage import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.NoticeMessage @@ -144,12 +144,10 @@ class StockQuote : ThreadedModule() { try { with(messages) { // Search for symbol/keywords - response = urlReader( - URL( - "${ALPHAVANTAGE_URL}SYMBOL_SEARCH&keywords=" + encodeUrl(symbol) - + "&apikey=" + encodeUrl(apiKey) - ) - ) + response = URL( + "${ALPHAVANTAGE_URL}SYMBOL_SEARCH&keywords=" + symbol.encodeUrl() + "&apikey=" + + apiKey.encodeUrl() + ).reader() var json = getJsonResponse(response, debugMessage) val symbols = json.getJSONArray("bestMatches") if (symbols.isEmpty) { @@ -158,13 +156,11 @@ class StockQuote : ThreadedModule() { val symbolInfo = symbols.getJSONObject(0) // Get quote for symbol - response = urlReader( - URL( - "${ALPHAVANTAGE_URL}GLOBAL_QUOTE&symbol=" - + encodeUrl(symbolInfo.getString("1. symbol")) - + "&apikey=" + encodeUrl(apiKey) - ) - ) + response = URL( + "${ALPHAVANTAGE_URL}GLOBAL_QUOTE&symbol=" + + symbolInfo.getString("1. symbol").encodeUrl() + "&apikey=" + + apiKey.encodeUrl() + ).reader() json = getJsonResponse(response, debugMessage) val quote = json.getJSONObject("Global Quote") if (quote.isEmpty) { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt index 96ae93e..403dc79 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -197,7 +197,7 @@ class Weather2 : ThreadedModule() { messages.add( NoticeMessage( "https://openweathermap.org/find?q=" - + encodeUrl("$city,${code.uppercase()}"), + + "$city,${code.uppercase()}".encodeUrl(), Colors.GREEN ) ) diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/PinboardTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/PinboardTest.kt index 70c6b28..4705f11 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/PinboardTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/PinboardTest.kt @@ -32,6 +32,8 @@ package net.thauvin.erik.mobibot +import net.thauvin.erik.mobibot.Utils.encodeUrl +import net.thauvin.erik.mobibot.Utils.reader import net.thauvin.erik.mobibot.entries.EntryLink import org.testng.Assert.assertFalse import org.testng.Assert.assertTrue @@ -66,12 +68,8 @@ class PinboardTest : LocalProperties() { } private fun validatePin(apiToken: String, url: String, vararg matches: String): Boolean { - val response = Utils.urlReader( - URL( - "https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&" - + Utils.encodeUrl(url) - ) - ) + val response = + URL("https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&" + url.encodeUrl()).reader() matches.forEach { if (!response.contains(it)) { diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt index b7a4008..29ae070 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt @@ -49,6 +49,7 @@ import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.obfuscate import net.thauvin.erik.mobibot.Utils.plural +import net.thauvin.erik.mobibot.Utils.reader import net.thauvin.erik.mobibot.Utils.red import net.thauvin.erik.mobibot.Utils.replaceEach import net.thauvin.erik.mobibot.Utils.reverseColor @@ -153,7 +154,7 @@ class UtilsTest { @Test fun testEncodeUrl() { - assertThat(encodeUrl("Hello Günter")).isEqualTo("Hello+G%C3%BCnter") + assertThat("Hello Günter".encodeUrl()).isEqualTo("Hello+G%C3%BCnter") } @Test @@ -274,7 +275,7 @@ class UtilsTest { @Test @Throws(IOException::class) fun testUrlReader() { - assertThat(urlReader(URL("https://postman-echo.com/status/200")), "urlReader()") + assertThat(URL("https://postman-echo.com/status/200").reader(), "urlReader()") .isEqualTo("{\"status\":200}") }