diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index b68ee47..d47760a 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -340,44 +340,7 @@ object Utils { * Converts XML/XHTML entities to plain text. */ @JvmStatic - fun unescapeXml(str: String): String = Jsoup.parse(str).text() - - /** - * Converts milliseconds to year month week day hour and minutes. - */ - @JvmStatic - fun uptime(uptime: Long): String { - uptime.toDuration(DurationUnit.MILLISECONDS).toComponents { wholeDays, hours, minutes, _, _ -> - val years = wholeDays / 365 - var days = wholeDays % 365 - val months = days / 30 - days %= 30 - val weeks = days / 7 - days %= 7 - - with(StringBuffer()) { - if (years > 0) { - append(years).append(" year".plural(years)).append(' ') - } - if (months > 0) { - append(weeks).append(" month".plural(months)).append(' ') - } - if (weeks > 0) { - append(weeks).append(" week".plural(weeks)).append(' ') - } - if (days > 0) { - append(days).append(" day".plural(days)).append(' ') - } - if (hours > 0) { - append(hours).append(" hour".plural(hours.toLong())).append(' ') - } - - append(minutes).append(" minute".plural(minutes.toLong())) - - return toString() - } - } - } + fun String.unescapeXml(): String = Jsoup.parse(this).text() /** * Reads contents of a URL. 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 6adffdd..742e6d9 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -115,7 +115,7 @@ class GoogleSearch : ThreadedModule() { val ja = json.getJSONArray("items") for (i in 0 until ja.length()) { val j = ja.getJSONObject(i) - results.add(NoticeMessage(unescapeXml(j.getString("title")))) + results.add(NoticeMessage(j.getString("title").unescapeXml())) results.add(NoticeMessage(helpFormat(j.getString("link"), false), Colors.DARK_GREEN)) } } else { 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 63d358c..030674f 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -102,7 +102,7 @@ class StockQuote : ThreadedModule() { try { val info = json.getString("Information") if (info.isNotEmpty()) { - throw ModuleException(debugMessage, unescapeXml(info)) + throw ModuleException(debugMessage, info.unescapeXml()) } } catch (ignore: JSONException) { // Do nothing @@ -110,11 +110,11 @@ class StockQuote : ThreadedModule() { try { var error = json.getString("Note") if (error.isNotEmpty()) { - throw ModuleException(debugMessage, unescapeXml(error)) + throw ModuleException(debugMessage, error.unescapeXml()) } error = json.getString("Error Message") if (error.isNotEmpty()) { - throw ModuleException(debugMessage, unescapeXml(error)) + throw ModuleException(debugMessage, error.unescapeXml()) } } catch (ignore: JSONException) { // Do nothing @@ -173,8 +173,8 @@ class StockQuote : ThreadedModule() { add( PublicMessage( - "Symbol: " + unescapeXml(quote.getString("01. symbol")) - + " [" + unescapeXml(symbolInfo.getString("2. name")) + ']' + "Symbol: " + quote.getString("01. symbol").unescapeXml() + + " [" + symbolInfo.getString("2. name").unescapeXml() + ']' ) ) @@ -183,13 +183,13 @@ class StockQuote : ThreadedModule() { add( PublicMessage( "Price:".padEnd(pad).prependIndent() - + unescapeXml(quote.getString("05. price")) + + quote.getString("05. price").unescapeXml() ) ) add( PublicMessage( "Previous:".padEnd(pad).prependIndent() - + unescapeXml(quote.getString("08. previous close")) + + quote.getString("08. previous close").unescapeXml() ) ) @@ -205,7 +205,7 @@ class StockQuote : ThreadedModule() { add( NoticeMessage( "${it.first}:".padEnd(pad).prependIndent() - + unescapeXml(quote.getString(it.second)) + + quote.getString(it.second).unescapeXml() ) ) } @@ -213,8 +213,8 @@ class StockQuote : ThreadedModule() { add( NoticeMessage( "Change:".padEnd(pad).prependIndent() - + unescapeXml(quote.getString("09. change")) - + " [" + unescapeXml(quote.getString("10. change percent")) + ']' + + quote.getString("09. change").unescapeXml() + + " [" + quote.getString("10. change percent").unescapeXml() + ']' ) ) } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt index 06bb589..b7a4008 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt @@ -254,7 +254,7 @@ class UtilsTest { @Test fun testUnescapeXml() { - assertThat(unescapeXml("<a name="test & ''">")).isEqualTo( + assertThat("<a name="test & ''">".unescapeXml()).isEqualTo( "" ) }