Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- LICENCE = "WTFPL", "http://www.wtfpl.net/about/"
- VERSION = "v0.3.2_pre20"
- import logging
- import pickle
- import praw
- import prawcore
- import sys
- from datetime import datetime, timedelta
- from math import ceil
- from time import sleep
- from random import randint
- log = logging.getLogger("VirtueTron")
- log.setLevel(logging.DEBUG)
- formatter = logging.Formatter(fmt="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y/%m/%d %I:%M:%S%p")
- file_log = logging.FileHandler(filename="VirtueTron.log", mode="a")
- file_log.setLevel(logging.INFO)
- file_log.setFormatter(formatter)
- log.addHandler(file_log)
- console_log = logging.StreamHandler(stream=sys.stdout)
- console_log.setLevel(logging.DEBUG)
- console_log.setFormatter(formatter)
- log.addHandler(console_log)
- log.info("Hello, Reddit!")
- log.info("VirtueTron® 9000™ {0} © CrashARuntimeToday@outlook.com".format(VERSION))
- credentials = pickle.load(open("credentials.pickle", "rb")) # {client_id:"VirtueTron9000", client_secret:"🤖🤡🍆💯™", username:"SignalAVirtueToday", password:"https://youtu.be/RCVJ7bujnSc"}
- credentials["user_agent"] = "VirtueTron 9000 {0}".format(VERSION)
- reddit = praw.Reddit(**credentials)
- tbp = reddit.subreddit("TheBluePill")
- IMMUTABLE_FLAIRS = ["vanguard", "vexatious", "endorsedflair", "alpha", "betaasfuck", "feeemale", "purged"]
- BAD_FLAIRS = ["purged", "vexatious"]
- GOOD_FLAIRS = ["endorsedflair", "vanguard", "alpha", "betaasfuck", "feeemale"]
- TODAYS_THREAT_LEVEL = {"tlsevere":"Severe", "tlhigh":"High", "tlelevated":"Elevated", "tlguarded":"Guarded", "tllow":"Low"}
- THREAT_MATRIX = {"tllow": BAD_FLAIRS + ["hb{0}".format(x) for x in range(1,7)],
- "tlguarded": BAD_FLAIRS + ["hb{0}".format(x) for x in range(1,5)],
- "tlelevated": BAD_FLAIRS + ["hb{0}".format(x) for x in range(1,3)],
- "tlhigh": BAD_FLAIRS,
- "tlsevere": []}
- FLAIR_EXEMPT = "sofcknwrong"
- delay = 2
- last_fuckup = None
- def praw_fucked_up():
- global delay
- global last_fuckup
- log.warning("Reddit API error: waiting {0} seconds".format(delay))
- try:
- if delay > 128 or datetime.now() > last_fuckup + timedelta(minutes=2): delay = 2
- except TypeError:
- delay = 2
- sleep(delay)
- delay *= 2
- last_fuckup = datetime.now()
- def get_flair(name):
- while True:
- try:
- for flair in tbp.flair(redditor=name):
- return flair["flair_css_class"]
- except prawcore.PrawcoreException:
- praw_fucked_up()
- class Tracker:
- NICE_LIST = []
- NAUGHTY_LIST = ["theredpill", "marriedredpill", "mgtow", "braincels", "asktrp", "askmrp", "redpillwomen", "redpillwives", "cringeanarchy", "the_donald", "rpchristians", "pussypassdenied", "mensrights", "milliondollarextreme", "4chan", "whereareallthegoodmen"]
- # For some reason VSCode eats the indents on blank lines and this gives the python interpreter fits when I copy/paste, so empty comments at the correct indent level it is!
- def __init__(self, name, new_user=True, good_points=[0], bad_points=[0], scan_count=0, next_refresh=(datetime.now()+timedelta(minutes=15)), current_flair=None, warned=None, comments_eaten=[], current_shittest=None, last_seen=datetime.now()):
- self.name = name
- self.smv = 0
- self.scan_count = scan_count
- self.next_refresh = next_refresh
- self.current_flair = current_flair
- self.warned = warned
- self.comments_eaten = comments_eaten
- self.good_points = good_points
- self.bad_points = bad_points
- self.best_post_karma = 0
- self.best_post_id = None
- self.worst_post_karma = 0
- self.worst_post_id = None
- self.current_shittest = current_shittest
- self.last_seen = last_seen
- if new_user:
- self.update()
- #
- @property
- def good_karma(self):
- return sum(self.good_points) / len(self.good_points)
- @property
- def bad_karma(self):
- return sum(self.bad_points) / len(self.bad_points)
- @property
- def score(self):
- return self.good_karma + self.bad_karma
- #
- def update(self):
- good, bad, good_count, bad_count = 0, 0, 0, 0
- for comment in reddit.redditor(self.name).comments.new(limit=100):
- sub = comment.subreddit.display_name.lower()
- karma = comment.score - 1
- if sub == "thebluepill":
- good += karma
- good_count += 1
- if karma > self.best_post_karma:
- self.best_post_karma = karma
- self.best_post_id = comment.id
- if karma < self.worst_post_karma:
- self.worst_post_karma = karma
- self.worst_post_id = comment.id
- elif sub in Tracker.NICE_LIST:
- good += (karma) / 2
- elif sub in Tracker.NAUGHTY_LIST and karma > 1:
- bad -= karma
- bad_count += 1
- #
- if good_count > 0:
- good /= good_count
- if bad_count > 0:
- bad /= bad_count
- bad *= bad_count / 5 # Every 10 posts in NAUGHTY_LIST doubles your bad karma
- # Are they vexatious? (Of course they are!)
- if self.current_flair != "vexatious" and bad_count > 10 and abs(bad) > good:
- log.info("User: {0} is vexatious ({1} posts in NAUGHTY_LIST)".format(self.name, bad_count))
- tbp.flair.set(self.name, "VEXATIOUS LITIGANT", "vexatious")
- self.current_flair = "vexatious"
- #
- log.info("Scanned user: {0}, good_karma: {1:.2f} ({2} comments), bad_karma: {3:.2f} ({4} comments)".format(self.name, good, good_count, bad, bad_count))
- self.good_points.insert(0, good)
- if len(self.good_points) > 8:
- self.good_points.pop()
- log.debug("Dropping oldest good karma sample for user: {0}".format(self.name))
- self.bad_points.insert(0, bad)
- if len(self.bad_points) > 8:
- self.bad_points.pop()
- log.debug("Dropping oldest bad karma sample for user: {0}".format(self.name))
- self.scan_count += 1
- self.next_refresh = datetime.now() + timedelta(minutes=15)
- def update_flairs():
- log.info("Recalculating SMV")
- i = 0
- total = len(users)
- # I'm so fuckin' dumb, we should've been doing it this way from the beginning
- purged_users = list(tbp.banned(limit=None))
- approved_users = list(tbp.contributor(limit=None))
- #
- for user in sorted(users.values(), key=lambda x: x.score):
- if user.name in purged_users and user.current_flair != "purged":
- log.info("Marking user: {0} purged".format(user.name))
- tbp.flair.set(user.name, "PURGED", "purged")
- user.current_flair = "purged"
- elif user.current_flair == "purged" and user.name not in purged_users:
- log.info("User: {0} was unbanned, correcting flair".format(user.name))
- tbp.flair.set(user.name, "", "")
- user.current_flair = ""
- #
- i += 1
- user.smv = ceil((i / total) * 10)
- log.debug("User: {0}, SMV: {1}, score: {2:.2f} (current flair {3})".format(user.name, user.smv, user.score, user.current_flair))
- #
- if user.current_flair == None:
- # Check if user deleted their account
- try:
- for x in reddit.redditor(user.name).new(limit=1):
- y = x
- except (prawcore.exceptions.NotFound, prawcore.exceptions.Forbidden):
- log.info("User: {0} is deleted, dropping from userlist".format(user.name))
- del users[user.name]
- total -= 1
- continue
- log.debug("No cached flair for user: {0}, checking Reddit".format(user.name))
- user.current_flair = get_flair(user.name)
- log.debug("Reddit says user: {0}'s flair is {1}".format(user.name, user.current_flair))
- #
- if user.current_flair in IMMUTABLE_FLAIRS:
- if user.current_flair == "vexatious" and user.smv > 2 and user.good_karma > abs(user.bad_karma):
- log.warn("Should user: {0} be vexatious? SMV: {1}, good_karma: {2:.2f}, bad_karma: {3:.2f}".format(user.name, user.smv, user.good_karma, user.bad_karma))
- else:
- log.debug("Not changing user: {0} (immutable flair {1})".format(user.name, user.current_flair))
- elif user.current_flair != "hb{0}".format(user.smv):
- log.info("Updating user: {0} flair to hb{1}".format(user.name, user.smv))
- tbp.flair.set(user.name, "Hβ{0}".format(user.smv), "hb{0}".format(user.smv))
- user.current_flair = "hb{0}".format(user.smv)
- #
- else:
- log.debug("User: {0} still an Hβ{1}".format(user.name, user.smv))
- #
- if (user.smv > 7 or user.current_flair in GOOD_FLAIRS) and user.name not in approved_users:
- log.debug("Adding approved contributor: {0}".format(user.name))
- tbp.contributor.add(user.name)
- elif (user.smv < 4 or user.current_flair in BAD_FLAIRS) and user.name in approved_users:
- log.info("Removing approved contributor: {0}".format(user.name))
- tbp.contributor.remove(user.name)
- #
- pickle.dump(users, open("users.pickle", "wb"))
- def botloop(first_recalc=datetime.now()):
- next_recalc = first_recalc
- #
- for comment in tbp.stream.comments():
- if datetime.now() > next_recalc:
- update_flairs()
- next_recalc = datetime.now() + timedelta(minutes=15)
- log.debug("Next refresh: {0:%Y/%m/%d %I:%M:%S%p}".format(next_recalc))
- #
- threat_level = comment.submission.link_flair_css_class
- if threat_level == None or comment.submission.link_flair_text == None or comment.submission.link_flair_text not in TODAYS_THREAT_LEVEL.values():
- if randint(0,1) == 0: # 50-50 shot of getting "Severe"
- threat_selector = 0
- else:
- threat_selector = randint(1, len(TODAYS_THREAT_LEVEL) - 3) # Won't randomly assign the last two link flairs
- threat_level = list(TODAYS_THREAT_LEVEL.keys())[threat_selector]
- log.info("Setting threat level for submission '{0}' [id: {1}] to {2}".format(comment.submission.title, comment.submission.id, TODAYS_THREAT_LEVEL[threat_level]))
- comment.submission.mod.flair(css_class=threat_level, text=TODAYS_THREAT_LEVEL[threat_level])
- else:
- log.debug("Threat level for submission '{0}' [id: {1}] is {2})".format(comment.submission.title, comment.submission.id, TODAYS_THREAT_LEVEL[threat_level]))
- #
- if comment.author != None:
- name = comment.author.name
- author_rank = comment.author_flair_css_class
- #
- if not name in users.keys():
- users[name] = Tracker(name)
- log.info("New user: {0}".format(name))
- elif datetime.now() > users[name].next_refresh:
- users[name].update()
- log.debug("User: {0} scanned {1} times".format(name, users[name].scan_count))
- users[name].last_seen = datetime.now()
- else:
- log.debug("Skipping user: {0}, next refresh {1:%Y/%m/%d %I:%M:%S%p}".format(name, users[name].next_refresh))
- users[name].last_seen = datetime.now()
- if users[name].current_flair != author_rank:
- log.info("Updating tracked flair for user: {0} - was {1}, now {2}".format(name, users[name].current_flair, author_rank))
- users[name].current_flair = author_rank
- #
- if (author_rank == None or author_rank == "") and users[name].scan_count > 4:
- if datetime.now() > users[name].warned + timedelta(minutes=30):
- if name not in FLAIR_EXEMPT and (users[name].smv < 4 or comment.score < 1) and users[name].current_flair not in BAD_FLAIRS and comment.id not in users[name].comments_eaten:
- users[name].comments_eaten.append(comment.id)
- comment.mod.remove()
- log.info("Removed comment {0} by unflaired user: {1}".format(comment.id, name))
- else:
- log.info("Not removing comment {0} by unflaired user: {1} (exempt or too high value)".format(comment.id, name))
- else:
- reply = comment.reply("Apologies for the inconvenience, but user flairs are mandatory on /r/TheBluePill. Please toggle your user flair back on at your earliest convenience (if you've opted out of the redesign, you'll need to disable this Subreddit's custom CSS to do so.) Your comments will be automatically filtered until you've re-enabled your flair, but the filtered comments will be restored when you're properly flaired again.\n\nThanks for choosing r/TheBluePill!\n\netc. etc., -Management\n\n*NOTE: This is an automated action, but this account is also attached to a human operator if you have any concerns. We appreciate your continued business!*")
- reply.mod.distinguish()
- users[name].warned = datetime.now()
- log.info("Warned user: {0}, flairs are mandatory.".format(name))
- else:
- if bool(users[name].warned) and len(users[name].comments_eaten) > 0:
- for comment in users[name].comments_eaten:
- reddit.comment(id=comment).mod.approve()
- log.info("Restoring comment {0} for user: {1} (is wearing flair now)".format(comment, name))
- users[name].comments_eaten = []
- if author_rank in THREAT_MATRIX[threat_level] and name != comment.submission.author:
- comment.mod.remove()
- log.info("Removing comment by user: {0} ({1}) on submission '{2}' [id: {3}] ({4}), SMV too low!".format(name, author_rank, comment.submission.title, comment.id, TODAYS_THREAT_LEVEL[threat_level]))
- NEED_TO_UPDATE_OBJCLASS = False
- try:
- users = pickle.load(open("users.pickle", "rb"))
- log.info("Re-loading database")
- if NEED_TO_UPDATE_OBJCLASS:
- log.debug("Updating object format")
- for user in users.values():
- users[user.name] = Tracker(user.name, False, user.good_points, user.bad_points, user.scan_count, user.next_refresh, user.current_flair, user.warned, user.comments_eaten, user.current_shittest, user.last_seen)
- except IOError:
- users = {}
- log.info("I/O error accessing database, starting fresh")
- try:
- botloop()
- except prawcore.PrawcoreException:
- praw_fucked_up()
- botloop()
- except KeyboardInterrupt:
- log.info("VirtuteTron going off-line")
- pickle.dump(users, open("users.pickle", "wb"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement