Skip to content

Commit

Permalink
Add link moderator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Deuchnord committed Nov 7, 2021
1 parent 8f40cdc commit 87935e6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
irc3 = "*"
urlextract = "*"

[dev-packages]

Expand Down
41 changes: 40 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions _twitchbot/moderator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from enum import Enum
from typing import Union
from datetime import datetime, timedelta
from urlextract import URLExtract
from urllib.parse import urlparse

EPOCH = datetime(1970, 1, 1)

Expand Down Expand Up @@ -148,3 +150,41 @@ def vote(self, msg: str, author: str) -> ModerationDecision:

def declare_raid(self):
self.last_raid = datetime.now()


class LinkModerator(Moderator):
def __init__(
self,
message: str,
decision: ModerationDecision,
timeout_duration: Union[None, int],
authorized_urls: [str]
):
super().__init__(message, decision, timeout_duration)
self.authorized_urls = authorized_urls

def get_name(self) -> str:
return 'Link'

def vote(self, msg: str, author: str) -> ModerationDecision:
url_extractor = URLExtract()
links = url_extractor.find_urls(msg)

if len(links) == 0:
return ModerationDecision.ABSTAIN

if not self.are_urls_authorized(links):
return self.decision

return ModerationDecision.ABSTAIN

@staticmethod
def are_urls_authorized(links: [str]) -> bool:
for link in links:
if not link.startswith('http://') or link.startswith('https://'):
link = 'http://%s' % link

result = urlparse(link)
# TODO: check with authorized_urls and the "*" wildcard

return True

0 comments on commit 87935e6

Please sign in to comment.