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