Fixed defaultTags.

This commit is contained in:
Erik C. Thauvin 2020-04-01 20:00:51 -07:00
parent 4048636696
commit b99a2f568e

View file

@ -45,8 +45,8 @@ import org.jsoup.Jsoup
import java.io.IOException import java.io.IOException
class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
private val keywords = ArrayList<String>() private val keywords: List<String>
private val tags = ArrayList<String>() private val defaultTags: List<String>
override val command = Constants.LINK_CMD override val command = Constants.LINK_CMD
override val help = emptyList<String>() override val help = emptyList<String>()
override val isOp = false override val isOp = false
@ -54,8 +54,8 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
override val isVisible = false override val isVisible = false
init { init {
this.keywords.addAll(keywords.split(TAG_MATCH.toRegex())) this.keywords = keywords.split(TAG_MATCH.toRegex())
tags.addAll(defaultTags.split(TAG_MATCH.toRegex())) this.defaultTags = defaultTags.split(TAG_MATCH.toRegex())
} }
companion object { companion object {
@ -132,6 +132,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
if (!isDupEntry(bot, sender, link, isPrivate)) { if (!isDupEntry(bot, sender, link, isPrivate)) {
val isBackup = saveDayBackup(bot) val isBackup = saveDayBackup(bot)
var title = Constants.NO_TITLE var title = Constants.NO_TITLE
val tags = ArrayList<String>(defaultTags)
if (cmds.size == 2) { if (cmds.size == 2) {
val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2) val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2)
title = data[0].trim() title = data[0].trim()
@ -140,7 +141,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
} }
} }
title = fetchTitle(link, title) title = fetchTitle(link, title)
tags.addAll(matchTagKeywords(title)) matchTagKeywords(title, tags)
entries.add(EntryLink(link, title, sender, login, bot.channel, tags)) entries.add(EntryLink(link, title, sender, login, bot.channel, tags))
val index: Int = entries.size - 1 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 { private fun fetchTitle(link: String, title: String): String {
if (Constants.NO_TITLE == title) { if (Constants.NO_TITLE == title) {
try { 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 htmlTitle = html.title()
val split = htmlTitle.split("( \\| )".toRegex(), 2) val split = htmlTitle.split("( \\| )".toRegex(), 2)
return if (split.size == 2) { return if (split.size == 2 && split[0].isNotBlank()) {
split[0] split[0]
} else { } else if (htmlTitle.isNotBlank()) {
htmlTitle htmlTitle
} else {
title
} }
} catch (ignore: IOException) { } catch (ignore: IOException) {
// Do nothing // Do nothing
@ -210,17 +215,13 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
return false return false
} }
private fun matchTagKeywords(title: String): List<String> { private fun matchTagKeywords(title: String, tags: ArrayList<String>) {
val matches = ArrayList<String>() for (match in keywords) {
if (keywords.isNotEmpty()) { val m = Regex.escape(match.trim())
for (match in keywords) { if (title.matches("(?i).*\\b$m\\b.*".toRegex())) {
val m = match.trim() tags.add(m)
if (title.matches("(?i).*\\b$m\\b.*".toRegex())) {
matches.add(m)
}
} }
} }
return matches
} }
private fun saveDayBackup(bot: Mobibot): Boolean { private fun saveDayBackup(bot: Mobibot): Boolean {