implement whitelist filters

This commit is contained in:
Corban-Lee Jones 2024-06-13 00:00:07 +01:00
parent 43ee2992ff
commit 327fa2b215

View File

@ -152,12 +152,18 @@ class TaskCog(commands.Cog):
log.debug("filter result: %s", "blocked" if blocked else "ok")
try:
await api.create_tracked_content(guid=article.guid, title=article.title, url=article.url, subscription=sub_id, blocked=blocked)
await api.create_tracked_content(
guid=article.guid,
title=article.title,
url=article.url,
subscription=sub_id,
blocked=blocked
)
log.debug("successfully tracked %s", article.guid)
except aiohttp.ClientResponseError as error:
log.error(error)
if error.status == 400:
if error.status == 409:
log.debug("It looks like this article already exists, skipping")
return
@ -176,26 +182,27 @@ class TaskCog(commands.Cog):
Returns True if article should be ignored due to filters.
"""
log.debug("trying filter '%s'", _filter["name"])
match_found = False # This is the flag to determine if the content should be filtered
keywords = _filter["keywords"].split(",")
regex_pattern = _filter["regex"]
is_whitelist = _filter["whitelist"]
log.debug(
"trying filter '%s', keyword '%s', regex '%s', is whitelist: '%s'",
_filter["name"], keywords, regex_pattern, is_whitelist
)
assert not (keywords and regex_pattern), "Keywords and Regex used, only 1 can be used."
for word in keywords:
if word in article.title or word in article.description:
log.debug("keyword '%s' found in title or description", word)
return True
# if any(word in article.title or word in article.description for word in keywords):
# return True
if any(word in article.title or word in article.description for word in keywords):
match_found = True
if regex_pattern:
regex = re.compile(regex_pattern)
return regex.search(article.title) or regex.search(article.description)
match_found = regex.search(article.title) or regex.search(article.description)
return False
return not match_found if is_whitelist else match_found
async def setup(bot):
"""