Style fixes.
All checks were successful
Build Ragnar anti spam bot / Build (push) Successful in 56s

This commit is contained in:
retoor 2024-12-01 09:01:57 +01:00
parent c68e3b743c
commit 516f971530
4 changed files with 24 additions and 23 deletions

View File

@ -1,7 +1,9 @@
import requests, json import json
import requests
from ragnar.cache import method_cache
from ragnar import log from ragnar import log
from ragnar.cache import method_cache
class Api: class Api:
@ -28,7 +30,7 @@ class Api:
@method_cache @method_cache
def login(self): def login(self):
log.info("Logged in as {}".format(self.username)) log.info(f"Logged in as {self.username}")
rawdata = requests.post( rawdata = requests.post(
self.base_url + "users/auth-token", self.base_url + "users/auth-token",
data={"username": self.username, "password": self.password, "app": 3}, data={"username": self.username, "password": self.password, "app": 3},

View File

@ -1,10 +1,11 @@
from ragnar.api import Api
import time
import random
from ragnar.cache import method_cache
import re
from ragnar import log
import json import json
import random
import re
import time
from ragnar import log
from ragnar.api import Api
from ragnar.cache import method_cache
class Bot: class Bot:
@ -23,9 +24,7 @@ class Bot:
"no-spam4": "Vira", "no-spam4": "Vira",
} }
self.name = names.get(self.name, "everyone") self.name = names.get(self.name, "everyone")
self.mark_text = "I am {} and downvoted this post because post is considered spam. Your message will be removed from this community site due too much downvotes. See my profile for more information. Read my source code mentioned on my profile to see what you did wrong. Should be no problem for a developer.\n\nHave a nice day!\n\n\nIf the post is not spam, please mention @retoor in the comments of this rant.".format( self.mark_text = f"I am {self.name} and downvoted this post because post is considered spam. Your message will be removed from this community site due too much downvotes. See my profile for more information. Read my source code mentioned on my profile to see what you did wrong. Should be no problem for a developer.\n\nHave a nice day!\n\n\nIf the post is not spam, please mention @retoor in the comments of this rant."
self.name
)
self.auth = None self.auth = None
self.triggers = [ self.triggers = [
"$", "$",
@ -60,9 +59,9 @@ class Bot:
self.rsleepii() self.rsleepii()
self.auth = self.api.login() self.auth = self.api.login()
if not self.auth: if not self.auth:
log.error("Authentication for {} failed.".format(self.username)) log.error(f"Authentication for {self.username} failed.")
raise Exception("Login error") raise Exception("Login error")
log.info("Authentication succesful for {}.".format(self.username)) log.info(f"Authentication succesful for {self.username}.")
def clean_rant_text(self, rant_text): def clean_rant_text(self, rant_text):
return rant_text.replace(" ", "").lower() return rant_text.replace(" ", "").lower()
@ -75,10 +74,10 @@ class Bot:
if trigger.get("regex"): if trigger.get("regex"):
regex = trigger["regex"] regex = trigger["regex"]
if re.search(regex, clean_text): if re.search(regex, clean_text):
log.info("Regex trigger {} matched!".format(regex)) log.info(f"Regex trigger {regex} matched!")
return True return True
elif trigger in clean_text: elif trigger in clean_text:
log.info("Trigger {} matched!".format(trigger)) log.info(f"Trigger {trigger} matched!")
return True return True
return False return False
@ -102,15 +101,13 @@ class Bot:
profile = self.api.get_profile(user_id) profile = self.api.get_profile(user_id)
score = profile["score"] score = profile["score"]
if score < 5: if score < 5:
log.warning( log.warning(f"User {username} is sus with his score of only {score}.")
"User {} is sus with his score of only {}.".format(username, score)
)
return True return True
else: else:
return False return False
def is_comments_sus(self, rant_id): def is_comments_sus(self, rant_id):
log.info("Checking if comments are sus of rant {}.".format(rant_id)) log.info(f"Checking if comments are sus of rant {rant_id}.")
rant = self.api.get_rant(rant_id) rant = self.api.get_rant(rant_id)
for comment in rant.get("comments", []): for comment in rant.get("comments", []):
print("Checking if sus comment: ", comment["body"]) print("Checking if sus comment: ", comment["body"])

View File

@ -1,9 +1,10 @@
import argparse import argparse
from ragnar.bot import Bot
import random import random
import time import time
from concurrent.futures import ThreadPoolExecutor as Executor from concurrent.futures import ThreadPoolExecutor as Executor
from ragnar import log from ragnar import log
from ragnar.bot import Bot
def parse_args(): def parse_args():
@ -16,7 +17,7 @@ def parse_args():
def bot_task(username, password): def bot_task(username, password):
log.info("Created new bot runniner. Username: {}".format(username)) log.info(f"Created new bot runniner. Username: {username}")
time.sleep(random.randint(1, 20)) time.sleep(random.randint(1, 20))
bot = Bot(username=username, password=password) bot = Bot(username=username, password=password)
bot.login() bot.login()
@ -32,7 +33,7 @@ def main():
args = parse_args() args = parse_args()
with Executor(4) as executor: with Executor(4) as executor:
for x in range(1, 5): for x in range(1, 5):
username = "no-spam{}@molodetz.nl".format(str(x)) username = f"no-spam{str(x)}@molodetz.nl"
password = args.password password = args.password
executor.submit(bot_task, username, password) executor.submit(bot_task, username, password)
executor.shutdown(wait=True) executor.shutdown(wait=True)

View File

@ -1,4 +1,5 @@
import unittest import unittest
from ragnar.bot import Bot from ragnar.bot import Bot