Added unit test and regex
Some checks failed
Build Ragnar anti spam bot / Build (push) Failing after 1m1s
Some checks failed
Build Ragnar anti spam bot / Build (push) Failing after 1m1s
This commit is contained in:
parent
c48bf54dc1
commit
42eca170a6
7
Makefile
7
Makefile
@ -1,4 +1,4 @@
|
|||||||
all: ensure_env format build install
|
all: ensure_env format build install test
|
||||||
|
|
||||||
format:
|
format:
|
||||||
./.venv/bin/python -m pip install black
|
./.venv/bin/python -m pip install black
|
||||||
@ -15,4 +15,7 @@ install:
|
|||||||
./.venv/bin/python -m pip install -e .
|
./.venv/bin/python -m pip install -e .
|
||||||
|
|
||||||
run:
|
run:
|
||||||
python -m ragnar.run
|
python -m ragnar.run
|
||||||
|
|
||||||
|
test:
|
||||||
|
./.venv/bin/python -m unittest ragnar.tests
|
@ -2,6 +2,7 @@ from ragnar.api import Api
|
|||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
from ragnar.cache import method_cache
|
from ragnar.cache import method_cache
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
class Bot:
|
class Bot:
|
||||||
@ -23,7 +24,15 @@ class Bot:
|
|||||||
self.name
|
self.name
|
||||||
)
|
)
|
||||||
self.auth = None
|
self.auth = None
|
||||||
self.triggers = ["$", "crypto", "hacker", "recovery"]
|
|
||||||
|
self.triggers = [
|
||||||
|
"$",
|
||||||
|
"crypto",
|
||||||
|
"hacker",
|
||||||
|
"recovery",
|
||||||
|
{"regex": "\([+,(,0-9,),-]{7,}"},
|
||||||
|
"money",
|
||||||
|
]
|
||||||
|
|
||||||
self.api = Api(username=self.username, password=self.password)
|
self.api = Api(username=self.username, password=self.password)
|
||||||
|
|
||||||
@ -39,12 +48,23 @@ class Bot:
|
|||||||
raise Exception("Login error")
|
raise Exception("Login error")
|
||||||
print("Authentication succesful for {}.".format(self.username))
|
print("Authentication succesful for {}.".format(self.username))
|
||||||
|
|
||||||
@method_cache
|
def clean_rant_text(self, rant_text):
|
||||||
|
return rant_text.replace(" ", "").lower()
|
||||||
|
|
||||||
|
# @method_cache
|
||||||
def is_sus_rant(self, rant_id, rant_text):
|
def is_sus_rant(self, rant_id, rant_text):
|
||||||
clean_text = rant_text.replace(" ", "").lower()
|
clean_text = self.clean_rant_text(rant_text)
|
||||||
for trigger in self.triggers:
|
for trigger in self.triggers:
|
||||||
if trigger in clean_text:
|
if type(trigger) == dict:
|
||||||
|
if trigger.get("regex"):
|
||||||
|
regex = trigger["regex"]
|
||||||
|
if re.search(regex, clean_text):
|
||||||
|
print("Regex trigger {} matched!".format(regex))
|
||||||
|
return True
|
||||||
|
elif trigger in clean_text:
|
||||||
|
print("Trigger {} matched!".format(trigger))
|
||||||
return True
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def is_flagged_as_sus(self, rant_id, num_comments):
|
def is_flagged_as_sus(self, rant_id, num_comments):
|
||||||
if not num_comments:
|
if not num_comments:
|
||||||
|
Loading…
Reference in New Issue
Block a user