From e53fc63719676738b035a379f8011b15697187e4 Mon Sep 17 00:00:00 2001 From: Joseph Ottinger Date: Mon, 6 Nov 2023 17:04:42 -0500 Subject: [PATCH] Removing rivescript, fixing twitter url parsing, cleaning up compilation --- pom.xml | 7 ------ src/main/kotlin/javabot/JavabotModule.kt | 2 +- .../javabot/javadoc/JavadocAsmParser.kt | 2 +- .../javabot/operations/LinksOperation.kt | 4 ++-- .../javabot/operations/URLTitleOperation.kt | 13 ++++++++--- .../javabot/operations/time/DateUtils.kt | 2 +- .../operations/time/TimezonesAustralia.kt | 4 ++-- .../operations/time/TimezonesCanada.kt | 4 ++-- .../javabot/operations/time/TimezonesUS.kt | 4 ++-- .../kotlin/javabot/operations/time/Tri.kt | 6 ++--- .../javabot/service/RiveScriptService.kt | 22 ------------------- .../operations/URLTitleOperationTest.kt | 17 ++++++++++++++ 12 files changed, 41 insertions(+), 46 deletions(-) delete mode 100644 src/main/kotlin/javabot/service/RiveScriptService.kt diff --git a/pom.xml b/pom.xml index 7b7ac2d8..3dc069af 100644 --- a/pom.xml +++ b/pom.xml @@ -517,12 +517,6 @@ - - - com.rivescript - rivescript-core - ${rivescript.version} - com.squareup.okhttp3 okhttp @@ -561,7 +555,6 @@ 1.4.11 2.3.1 4.10.0 - 0.11.0 2.0.9 0.25 diff --git a/src/main/kotlin/javabot/JavabotModule.kt b/src/main/kotlin/javabot/JavabotModule.kt index 05e7c91a..9c661abb 100644 --- a/src/main/kotlin/javabot/JavabotModule.kt +++ b/src/main/kotlin/javabot/JavabotModule.kt @@ -41,7 +41,7 @@ open class JavabotModule : AbstractModule() { install(FactoryModuleBuilder().build(ViewFactory::class.java)) } - open fun client(): MongoClient? { + open fun client(): MongoClient { return mongoClient } diff --git a/src/main/kotlin/javabot/javadoc/JavadocAsmParser.kt b/src/main/kotlin/javabot/javadoc/JavadocAsmParser.kt index 35969d78..2eceab21 100644 --- a/src/main/kotlin/javabot/javadoc/JavadocAsmParser.kt +++ b/src/main/kotlin/javabot/javadoc/JavadocAsmParser.kt @@ -49,7 +49,7 @@ constructor(private val apiDao: ApiDao, private val config: JavabotConfig) { if ("JDK" == api.name) { File(System.getProperty("java.home"), "jmods") - .listFiles { file, s -> s.startsWith("java") } + .listFiles { _, s -> s.startsWith("java") } .forEach { scanJar(api, type, it) } } else { scanJar(api, type, api.classesUri().download()) diff --git a/src/main/kotlin/javabot/operations/LinksOperation.kt b/src/main/kotlin/javabot/operations/LinksOperation.kt index 6acd2d95..236d549f 100644 --- a/src/main/kotlin/javabot/operations/LinksOperation.kt +++ b/src/main/kotlin/javabot/operations/LinksOperation.kt @@ -1,7 +1,7 @@ package javabot.operations import com.antwerkz.sofia.Sofia -import java.net.URL +import java.net.URI import java.util.Locale import javabot.Javabot import javabot.Message @@ -118,7 +118,7 @@ constructor( tokens .mapNotNull { try { - URL(it) + URI(it).toURL() } catch (e: Exception) { null } diff --git a/src/main/kotlin/javabot/operations/URLTitleOperation.kt b/src/main/kotlin/javabot/operations/URLTitleOperation.kt index fb0f37db..e0a0908a 100644 --- a/src/main/kotlin/javabot/operations/URLTitleOperation.kt +++ b/src/main/kotlin/javabot/operations/URLTitleOperation.kt @@ -1,7 +1,7 @@ package javabot.operations import java.io.IOException -import java.net.URL +import java.net.URI import javabot.Javabot import javabot.Message import javabot.dao.AdminDao @@ -74,12 +74,18 @@ constructor( ) } + fun hostBasename(host: String, sections: Int = 2): String { + return host.split(".").takeLast(sections).joinToString(".").lowercase() + } + private fun findTitle(url: String, loop: Boolean): String? { + val twitterUrls = listOf("twitter.com", "x.com") if (analyzer.precheck(url)) { + @Suppress("GrazieInspection") try { - val typedUrl = URL(url) + val typedUrl = URI(url).toURL() // there used to be parsing here for youtube but they use now - if (typedUrl.host.contains("twitter.com", true)) { + if (twitterUrls.contains(typedUrl.host.lowercase())) { return findTwitterTitle(url) } val document = Jsoup.parse(httpService.get(url)) @@ -102,6 +108,7 @@ constructor( } private fun findTwitterTitle(url: String): String { + @Suppress("GrazieInspection") if (twitterService.isEnabled()) { if (url.contains("status")) { val regex = Regex("status/\\d++") diff --git a/src/main/kotlin/javabot/operations/time/DateUtils.kt b/src/main/kotlin/javabot/operations/time/DateUtils.kt index d6b9037d..82dbf1f3 100644 --- a/src/main/kotlin/javabot/operations/time/DateUtils.kt +++ b/src/main/kotlin/javabot/operations/time/DateUtils.kt @@ -12,7 +12,7 @@ class DateUtils { val date = Date().time val formattedDate = dateFormatter.withZone(DateTimeZone.forID(timezone)).print(date) val formattedTime = - timeFormatter.withZone(DateTimeZone.forID(timezone)).print(date).toLowerCase() + timeFormatter.withZone(DateTimeZone.forID(timezone)).print(date).lowercase() return formattedDate + " @ " + formattedTime } } diff --git a/src/main/kotlin/javabot/operations/time/TimezonesAustralia.kt b/src/main/kotlin/javabot/operations/time/TimezonesAustralia.kt index f0173832..ef1f0ff9 100644 --- a/src/main/kotlin/javabot/operations/time/TimezonesAustralia.kt +++ b/src/main/kotlin/javabot/operations/time/TimezonesAustralia.kt @@ -32,8 +32,8 @@ class TimezonesAustralia : Timezones { init { for (states in AustralianState.values()) { val timezone = states.timezone - timezones.insert(states.name.toLowerCase(), timezone) - timezones.insert(states.abbreviation.toLowerCase(), timezone) + timezones.insert(states.name.lowercase(), timezone) + timezones.insert(states.abbreviation.lowercase(), timezone) timezones.insert(states.capital, timezone) } } diff --git a/src/main/kotlin/javabot/operations/time/TimezonesCanada.kt b/src/main/kotlin/javabot/operations/time/TimezonesCanada.kt index 0cf65b8f..ab70b028 100644 --- a/src/main/kotlin/javabot/operations/time/TimezonesCanada.kt +++ b/src/main/kotlin/javabot/operations/time/TimezonesCanada.kt @@ -38,8 +38,8 @@ class TimezonesCanada : Timezones { init { for (province in CanadianProvince.values()) { val timezone = province.timezone - timezones.insert(province.name.toLowerCase(), timezone) - timezones.insert(province.abbreviation.toLowerCase(), timezone) + timezones.insert(province.name.lowercase(), timezone) + timezones.insert(province.abbreviation.lowercase(), timezone) timezones.insert(province.capital, timezone) } } diff --git a/src/main/kotlin/javabot/operations/time/TimezonesUS.kt b/src/main/kotlin/javabot/operations/time/TimezonesUS.kt index 1c76c902..3a6bbadb 100644 --- a/src/main/kotlin/javabot/operations/time/TimezonesUS.kt +++ b/src/main/kotlin/javabot/operations/time/TimezonesUS.kt @@ -78,8 +78,8 @@ class TimezonesUS : Timezones { init { for (state in UsState.values()) { val timezone = state.timezone - timezones.insert(state.name.toLowerCase(), timezone) - timezones.insert(state.abbreviation.toLowerCase(), timezone) + timezones.insert(state.name.lowercase(), timezone) + timezones.insert(state.abbreviation.lowercase(), timezone) timezones.insert(state.capital, timezone) } } diff --git a/src/main/kotlin/javabot/operations/time/Tri.kt b/src/main/kotlin/javabot/operations/time/Tri.kt index 595e2327..ef7967d3 100644 --- a/src/main/kotlin/javabot/operations/time/Tri.kt +++ b/src/main/kotlin/javabot/operations/time/Tri.kt @@ -35,11 +35,11 @@ class Tri<T> { } private fun clean(key: String): String { - return key.toLowerCase().replace("[^a-z]".toRegex(), "") + return key.lowercase().replace("[^a-z]".toRegex(), "") } private fun hash(c: Char): Int { - return c.toInt() - OFFSET + return c.code - OFFSET } private inner class Node<T> { @@ -56,6 +56,6 @@ class Tri<T> { } companion object { - private val OFFSET = 'a'.toInt() + private val OFFSET = 'a'.code } } diff --git a/src/main/kotlin/javabot/service/RiveScriptService.kt b/src/main/kotlin/javabot/service/RiveScriptService.kt deleted file mode 100644 index a1d77c7d..00000000 --- a/src/main/kotlin/javabot/service/RiveScriptService.kt +++ /dev/null @@ -1,22 +0,0 @@ -package javabot.service - -import com.google.inject.Singleton -import com.rivescript.Config -import com.rivescript.RiveScript - -@Singleton -class RiveScriptService : RiveScript(Config.utf8()) { - var updated = true - fun load(resource: String) { - this::class.java.getResourceAsStream(resource).use { loadInputStream(it) } - updated = true - } - - override fun reply(username: String?, message: String?): String { - if (updated) { - sortReplies() - updated = false - } - return super.reply(username, message) - } -} diff --git a/src/test/kotlin/javabot/operations/URLTitleOperationTest.kt b/src/test/kotlin/javabot/operations/URLTitleOperationTest.kt index a1966884..7de49dc5 100644 --- a/src/test/kotlin/javabot/operations/URLTitleOperationTest.kt +++ b/src/test/kotlin/javabot/operations/URLTitleOperationTest.kt @@ -1,5 +1,6 @@ package javabot.operations +import java.net.URI import javabot.BaseTest import javabot.JavabotConfig import javabot.Message @@ -18,6 +19,22 @@ class URLTitleOperationTest : BaseTest() { @Inject private lateinit var config: JavabotConfig private val analyzer = URLContentAnalyzer() + @DataProvider(name = "longUrls") + fun longUrls(): Array<Array<String>> = + arrayOf( + arrayOf("https://twitter.com", "twitter.com"), + arrayOf("http://www.twitter.com", "twitter.com"), + arrayOf("http://x.com", "x.com"), + arrayOf("https://foo.www.x.com", "x.com"), + arrayOf("https://twitter", "twitter"), // ew + ) + + @Test(dataProvider = "longUrls") + fun testHostBasename(url: String, basenamed: String) { + var host = URI(url).toURL().host + assertEquals(operation.hostBasename(host), basenamed.lowercase()) + } + @Test(dataProvider = "urls") fun testSimpleUrl(url: String, content: String?) { val results = operation.handleChannelMessage(Message(TEST_CHANNEL, TEST_USER, url))