from ragnar.api import Api
import time
import random
from ragnar.cache import method_cache
class Bot:
def __init__(self, username, password):
self.username = username
self.password = password
self.name = self.username.split("@")[0]
names = {
"no-spam": "anna",
"no-spam1": "ira",
"no-spam2": "katya",
"no-spam3": "nastya",
"no-spam4": "vira",
}
self.name = names.get(self.name, "everyone")
self.mark_text = "You rant is flagged as spam by {}. Read bot source code to find out how to prevent this. Have a nice day! (Bot does not downvote yet, couldn't figure out what the downvote value should be. Upvote these bots btw so they can post a link. They're quite effective, they'll end spam.)".format(
self.name
)
self.auth = None
self.triggers = ["$", "crypto", "hacker", "recovery"]
self.api = Api(username=self.username, password=self.password)
def rsleepii(self):
time.sleep(random.randint(1, 3))
@method_cache
def login(self):
self.rsleepii()
self.auth = self.api.login()
if not self.auth:
print("Authentication for {} failed.".format(self.username))
raise Exception("Login error")
print("Authentication succesful for {}.".format(self.username))
@method_cache
def is_sus_rant(self, rant_id, rant_text):
clean_text = rant_text.replace(" ", "").lower()
for trigger in self.triggers:
if trigger in clean_text:
return True
def is_flagged_as_sus(self, rant_id, num_comments):
if not num_comments:
return False
self.rsleepii()
rant = self.api.get_rant(rant_id)
for comment in rant.get("comments", []):
if self.mark_text in comment.get("body", ""):
return True
return False
@method_cache
def is_user_sus(self, username):
user_id = self.api.get_user_id(username)
profile = self.api.get_profile(user_id)
score = profile["score"]
if score < 5:
print("User {} is sus with his score of only {}.".format(username, score))
return True
else:
return False
def mark_as_sus(self, rant):
self.rsleepii()
self.api.post_comment(rant["id"], self.mark_text)
def fight(self):
self.rsleepii()
rants = self.api.get_rants("recent", 5, 0)
for rant in rants:
if not self.is_user_sus(rant["user_username"]):
print("User {} is trusted.".format(rant["user_username"]))
continue
if not self.is_sus_rant(rant["id"], rant["text"]):
print("Rant by {} is not sus.".format(rant["user_username"]))
continue
if self.is_flagged_as_sus(rant["id"], rant.get("num_comments")):
continue
print("Rant is not {} flagged as sus yet.".format(rant["user_username"]))
print("Flagging rant by {} as sus.".format(rant["user_username"]))
self.mark_as_sus(rant)
self.down_vote_rant(rant)
def down_vote_rant(self, rant):
print("Downvoting rant by {}.".format(rant["user_username"]))
print(self.api.post_rant_vote(rant["id"], 4))