From b99a2f568e6b991b34d9cb39763e3bb9f3d0cf3c Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 1 Apr 2020 20:00:51 -0700 Subject: [PATCH] Fixed defaultTags. --- .../erik/mobibot/commands/links/UrlMgr.kt | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt index de2db08..b07d909 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt @@ -45,8 +45,8 @@ import org.jsoup.Jsoup import java.io.IOException class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { - private val keywords = ArrayList() - private val tags = ArrayList() + private val keywords: List + private val defaultTags: List override val command = Constants.LINK_CMD override val help = emptyList() override val isOp = false @@ -54,8 +54,8 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { override val isVisible = false init { - this.keywords.addAll(keywords.split(TAG_MATCH.toRegex())) - tags.addAll(defaultTags.split(TAG_MATCH.toRegex())) + this.keywords = keywords.split(TAG_MATCH.toRegex()) + this.defaultTags = defaultTags.split(TAG_MATCH.toRegex()) } companion object { @@ -132,6 +132,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { if (!isDupEntry(bot, sender, link, isPrivate)) { val isBackup = saveDayBackup(bot) var title = Constants.NO_TITLE + val tags = ArrayList(defaultTags) if (cmds.size == 2) { val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2) title = data[0].trim() @@ -140,7 +141,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { } } title = fetchTitle(link, title) - tags.addAll(matchTagKeywords(title)) + matchTagKeywords(title, tags) entries.add(EntryLink(link, title, sender, login, bot.channel, tags)) val index: Int = entries.size - 1 @@ -182,13 +183,17 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { private fun fetchTitle(link: String, title: String): String { if (Constants.NO_TITLE == title) { try { - val html = Jsoup.connect(link).userAgent("Mozilla").get() + val html = Jsoup.connect(link) + .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0") + .get() val htmlTitle = html.title() val split = htmlTitle.split("( \\| )".toRegex(), 2) - return if (split.size == 2) { + return if (split.size == 2 && split[0].isNotBlank()) { split[0] - } else { + } else if (htmlTitle.isNotBlank()) { htmlTitle + } else { + title } } catch (ignore: IOException) { // Do nothing @@ -210,17 +215,13 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { return false } - private fun matchTagKeywords(title: String): List { - val matches = ArrayList() - if (keywords.isNotEmpty()) { - for (match in keywords) { - val m = match.trim() - if (title.matches("(?i).*\\b$m\\b.*".toRegex())) { - matches.add(m) - } + private fun matchTagKeywords(title: String, tags: ArrayList) { + for (match in keywords) { + val m = Regex.escape(match.trim()) + if (title.matches("(?i).*\\b$m\\b.*".toRegex())) { + tags.add(m) } } - return matches } private fun saveDayBackup(bot: Mobibot): Boolean {