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. * URL encodes the given string.
*/ */
@JvmStatic @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. * Returns a property as an int.
@ -347,8 +347,8 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
@Throws(IOException::class) @Throws(IOException::class)
fun urlReader(url: URL): String { fun URL.reader(): String {
BufferedReader(InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) BufferedReader(InputStreamReader(this.openStream(), StandardCharsets.UTF_8))
.use { reader -> return reader.lines().collect(Collectors.joining(System.lineSeparator())) } .use { reader -> return reader.lines().collect(Collectors.joining(System.lineSeparator())) }
} }
} }

View file

@ -57,7 +57,7 @@ class ChannelFeed(channel: String) : AbstractCommand() {
if (isEnabled()) { if (isEnabled()) {
runBlocking { runBlocking {
launch { 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.capitalise
import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.encodeUrl
import net.thauvin.erik.mobibot.Utils.helpFormat 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.sendMessage
import net.thauvin.erik.mobibot.Utils.unescapeXml 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.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.NoticeMessage import net.thauvin.erik.mobibot.msg.NoticeMessage
@ -108,9 +108,9 @@ class GoogleSearch : ThreadedModule() {
try { try {
val url = URL( val url = URL(
"https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$cseKey" + "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")) { if (json.has("items")) {
val ja = json.getJSONArray("items") val ja = json.getJSONArray("items")
for (i in 0 until ja.length()) { 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.bot
import net.thauvin.erik.mobibot.Utils.cyan import net.thauvin.erik.mobibot.Utils.cyan
import net.thauvin.erik.mobibot.Utils.helpFormat 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.sendMessage
import net.thauvin.erik.mobibot.Utils.urlReader
import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage import net.thauvin.erik.mobibot.msg.PublicMessage
import org.json.JSONException import org.json.JSONException
@ -94,7 +94,7 @@ class Joke : ThreadedModule() {
fun randomJoke(): Message { fun randomJoke(): Message {
return try { return try {
val url = URL(JOKE_URL) val url = URL(JOKE_URL)
val json = JSONObject(urlReader(url)) val json = JSONObject(url.reader())
PublicMessage( PublicMessage(
json.getJSONObject("value")["joke"].toString().replace("\\'", "'") json.getJSONObject("value")["joke"].toString().replace("\\'", "'")
.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.capitalise
import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.encodeUrl
import net.thauvin.erik.mobibot.Utils.helpFormat 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.sendMessage
import net.thauvin.erik.mobibot.Utils.unescapeXml 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.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.NoticeMessage import net.thauvin.erik.mobibot.msg.NoticeMessage
@ -144,12 +144,10 @@ class StockQuote : ThreadedModule() {
try { try {
with(messages) { with(messages) {
// Search for symbol/keywords // Search for symbol/keywords
response = urlReader( response = URL(
URL( "${ALPHAVANTAGE_URL}SYMBOL_SEARCH&keywords=" + symbol.encodeUrl() + "&apikey="
"${ALPHAVANTAGE_URL}SYMBOL_SEARCH&keywords=" + encodeUrl(symbol) + apiKey.encodeUrl()
+ "&apikey=" + encodeUrl(apiKey) ).reader()
)
)
var json = getJsonResponse(response, debugMessage) var json = getJsonResponse(response, debugMessage)
val symbols = json.getJSONArray("bestMatches") val symbols = json.getJSONArray("bestMatches")
if (symbols.isEmpty) { if (symbols.isEmpty) {
@ -158,13 +156,11 @@ class StockQuote : ThreadedModule() {
val symbolInfo = symbols.getJSONObject(0) val symbolInfo = symbols.getJSONObject(0)
// Get quote for symbol // Get quote for symbol
response = urlReader( response = URL(
URL(
"${ALPHAVANTAGE_URL}GLOBAL_QUOTE&symbol=" "${ALPHAVANTAGE_URL}GLOBAL_QUOTE&symbol="
+ encodeUrl(symbolInfo.getString("1. symbol")) + symbolInfo.getString("1. symbol").encodeUrl() + "&apikey="
+ "&apikey=" + encodeUrl(apiKey) + apiKey.encodeUrl()
) ).reader()
)
json = getJsonResponse(response, debugMessage) json = getJsonResponse(response, debugMessage)
val quote = json.getJSONObject("Global Quote") val quote = json.getJSONObject("Global Quote")
if (quote.isEmpty) { if (quote.isEmpty) {

View file

@ -197,7 +197,7 @@ class Weather2 : ThreadedModule() {
messages.add( messages.add(
NoticeMessage( NoticeMessage(
"https://openweathermap.org/find?q=" "https://openweathermap.org/find?q="
+ encodeUrl("$city,${code.uppercase()}"), + "$city,${code.uppercase()}".encodeUrl(),
Colors.GREEN Colors.GREEN
) )
) )

View file

@ -32,6 +32,8 @@
package net.thauvin.erik.mobibot 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 net.thauvin.erik.mobibot.entries.EntryLink
import org.testng.Assert.assertFalse import org.testng.Assert.assertFalse
import org.testng.Assert.assertTrue import org.testng.Assert.assertTrue
@ -66,12 +68,8 @@ class PinboardTest : LocalProperties() {
} }
private fun validatePin(apiToken: String, url: String, vararg matches: String): Boolean { private fun validatePin(apiToken: String, url: String, vararg matches: String): Boolean {
val response = Utils.urlReader( val response =
URL( URL("https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&" + url.encodeUrl()).reader()
"https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&"
+ Utils.encodeUrl(url)
)
)
matches.forEach { matches.forEach {
if (!response.contains(it)) { 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.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.obfuscate import net.thauvin.erik.mobibot.Utils.obfuscate
import net.thauvin.erik.mobibot.Utils.plural 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.red
import net.thauvin.erik.mobibot.Utils.replaceEach import net.thauvin.erik.mobibot.Utils.replaceEach
import net.thauvin.erik.mobibot.Utils.reverseColor import net.thauvin.erik.mobibot.Utils.reverseColor
@ -153,7 +154,7 @@ class UtilsTest {
@Test @Test
fun testEncodeUrl() { fun testEncodeUrl() {
assertThat(encodeUrl("Hello Günter")).isEqualTo("Hello+G%C3%BCnter") assertThat("Hello Günter".encodeUrl()).isEqualTo("Hello+G%C3%BCnter")
} }
@Test @Test
@ -274,7 +275,7 @@ class UtilsTest {
@Test @Test
@Throws(IOException::class) @Throws(IOException::class)
fun testUrlReader() { 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}") .isEqualTo("{\"status\":200}")
} }