Added URL reader and encoder extension funtions.

This commit is contained in:
Erik C. Thauvin 2022-02-14 22:34:09 -08:00
parent 25d7a74568
commit 0dd02d7039
8 changed files with 27 additions and 32 deletions

View file

@ -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())) }
}
}

View file

@ -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() }
}
}
}

View file

@ -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()) {

View file

@ -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("\\\"", "\"")

View file

@ -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) {

View file

@ -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
)
)

View file

@ -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)) {

View file

@ -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}")
}