daily pastebin goal
47%
SHARE
TWEET

VirtueTron9000 v0.2.2F

a guest Jun 29th, 2018 181 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. LICENCE = "WTFPL", "http://www.wtfpl.net/about/"
  3. VERSION = "v0.2.2f"
  4.  
  5. import logging
  6. import pickle
  7. import praw
  8. import prawcore
  9. import sys
  10. from datetime import datetime, timedelta
  11. from math import ceil
  12. from time import sleep
  13.  
  14. log = logging.getLogger("VirtueTron")
  15. log.setLevel(logging.DEBUG)
  16. formatter = logging.Formatter(fmt="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y/%m/%d %I:%M:%S%p")
  17. file_log = logging.FileHandler(filename="VirtueTron.log", mode="a")
  18. file_log.setLevel(logging.INFO)
  19. file_log.setFormatter(formatter)
  20. log.addHandler(file_log)
  21. console_log = logging.StreamHandler(stream=sys.stdout)
  22. console_log.setLevel(logging.DEBUG)
  23. console_log.setFormatter(formatter)
  24. log.addHandler(console_log)
  25.  
  26. log.info("Hello, Reddit!")
  27. log.info("VirtueTron® 9000™ {0} © CrashARuntimeToday@outlook.com".format(VERSION))
  28.  
  29. credentials = pickle.load(open("credentials.pickle", "rb")) # {client_id:"VirtueTron9000", client_secret:"🤖🤡🍆💯™", username:"SignalAVirtueToday", password:"https://youtu.be/RCVJ7bujnSc" }
  30. credentials["user_agent"] = "VirtueTron 9000 {0}".format(VERSION)
  31. reddit = praw.Reddit(**credentials)
  32. tbp = reddit.subreddit("TheBluePill")
  33.  
  34.  
  35. class Tracker:
  36.     def __init__(self, name, good_karma, bad_karma):
  37.         self.name = name
  38.         self.smv = None
  39.         self.scan_count = 0
  40.         self.next_refresh = None
  41.         self.update(good_karma, bad_karma)
  42.    
  43.     def update(self, good_karma, bad_karma):
  44.         self.good_karma = good_karma
  45.         self.bad_karma = bad_karma
  46.         self.scan_count += 1
  47.         self.next_refresh = datetime.now() + timedelta(minutes=15)
  48.  
  49.  
  50. delay = 2
  51. def praw_fucked_up():
  52.     global delay
  53.     log.warning("Reddit API errored: waiting {0} seconds".format(delay))
  54.     if delay > 128: delay = 2
  55.     sleep(delay)
  56.     delay *= 2
  57.  
  58. NICE_LIST = []
  59. NAUGHTY_LIST = "TheRedPill", "MarriedRedPill", "ChristianRedPill", "MGTOW", "Braincels", "AskTRP", "AskMRP", "RedPillWomen", "RedPillWives", "CringeAnarchy", "The_Donald", "RPChristians"
  60. def calc_score(name):
  61.     good_karma, bad_karma, good_count, bad_count = 0, 0, 0, 0
  62.    
  63.     for comment in reddit.redditor(name).comments.new(limit=100):
  64.         sub = comment.subreddit.display_name
  65.         karma = comment.score
  66.         if sub == "TheBluePill":
  67.             good_karma += karma - 1
  68.             good_count += 1
  69.         elif sub in NICE_LIST:
  70.             good_karma += (karma - 1) / 2
  71.         elif sub in NAUGHTY_LIST and karma > 1:
  72.             bad_karma -= karma - 1
  73.             bad_count += 1
  74.    
  75.     if good_count > 0:
  76.         good_karma /= good_count
  77.    
  78.     if bad_count > 0:
  79.         bad_karma /= bad_count
  80.         if bad_count > 20 and not any(tbp.banned(redditor=name)):
  81.             log.info("User: {0} is vexatious ({1} posts in NAUGHTY_LIST)".format(name, bad_count))
  82.             tbp.flair.set(name, "VEXATIOUS LITIGANT", "vexatious")
  83.  
  84.     log.info("Scanned user: {0}, good_karma: {1} ({2} comments), bad_karma: {3} ({4} comments)".format(name, good_karma, good_count, bad_karma, bad_count))
  85.     return good_karma, bad_karma
  86.  
  87. IMMUTABLE_FLAIRS = "vanguard", "vexatious", "endorsedflair", "alpha", "betaasfuck", "feeemale", "purged"
  88. def update_flairs():
  89.     log.info("Recalculating SMV")
  90.     i = 0
  91.     total = len(users)
  92.     for user in sorted(users.values(), key=lambda x: x.good_karma + x.bad_karma):
  93.         i += 1
  94.         user.smv = ceil((i / total) * 10)
  95.         current_flair = None
  96.        
  97.         # Not sure why Reddit doesn't like this bit and too lazy to run Wireshark
  98.         try:      
  99.             for flair in tbp.flair(redditor=user.name):
  100.                 current_flair = flair["flair_css_class"]
  101.         except prawcore.PrawcoreException:
  102.             praw_fucked_up()
  103.             pass
  104.  
  105.         if any(tbp.banned(redditor=user.name)):
  106.             if current_flair != "purged":
  107.                 log.info("Marking user: {0} purged".format(user.name))
  108.                 tbp.flair.set(user.name, "PURGED", "purged")
  109.             else:
  110.                 log.info("User: {0} is purged".format(user.name))
  111.  
  112.         else:
  113.             log.info("User: {0}, SMV: {1}, score: {2} (current flair {3})".format(user.name, user.smv, user.good_karma + user.bad_karma, current_flair))
  114.  
  115.             if current_flair in IMMUTABLE_FLAIRS:
  116.                 log.info("Not changing user: {0} (immutable flair {1})".format(user.name, current_flair))
  117.             elif current_flair != "hb{0}".format(user.smv):
  118.                 log.info("Updating user: {0} flair to hb{1}".format(user.name, user.smv))
  119.                 tbp.flair.set(user.name, "Hβ{0}".format(user.smv), "hb{0}".format(user.smv))
  120.                
  121.                 if user.smv > 7:
  122.                     if user.name not in tbp.contributor():
  123.                         log.info("Adding approved contributor: {0}".format(user.name))
  124.                         tbp.contributor.add(user.name)
  125.                 elif user.smv < 4:
  126.                     if user.name in tbp.contributor():
  127.                         log.info("Removing approved contributor: {0}".format(user.name))
  128.                         tbp.contributor.remove(user.name)
  129.            
  130.             else:
  131.                 log.info("User: {0} still an HB{1}".format(user.name, user.smv))
  132.    
  133.     pickle.dump(users, open("users.pickle", "wb"))
  134.  
  135. BAD_FLAIRS = ["purged", "vexatious"]
  136. threat_matrix = {"tllow": BAD_FLAIRS + ["hb{0}".format(x) for x in range(1,7)],
  137.                  "tlguarded": BAD_FLAIRS + ["hb{0}".format(x) for x in range(1,5)],
  138.                  "tlelevated": BAD_FLAIRS + ["hb{0}".format(x) for x in range(1,3)],
  139.                  "tlhigh": BAD_FLAIRS }
  140. def botloop(first_recalc):
  141.     next_recalc = first_recalc
  142.    
  143.     for comment in tbp.stream.comments():
  144.         threat_level = comment.submission.link_flair_css_class
  145.         author_rank = comment.author_flair_css_class
  146.        
  147.         try:
  148.             if author_rank in threat_matrix["threat_level"]:
  149.                 #comment.remove()
  150.                 log.info("Removing comment by {0} ({1}) on post '{2}' ({3}), SMV too low!".format(comment.author.name, comment.author_css_class, comment.submission.title, threat_level))
  151.         except KeyError:
  152.             pass
  153.        
  154.         if datetime.now() > next_recalc:
  155.             update_flairs()
  156.             next_recalc = datetime.now() + timedelta(minutes=20)
  157.             log.info("Next refresh: {0}".format(next_recalc))
  158.        
  159.         if comment.author != None:
  160.             name = comment.author.name
  161.            
  162.             if not name in users.keys():
  163.                 good_karma, bad_karma = calc_score(name)
  164.                 users[name] = Tracker(name, good_karma, bad_karma)
  165.                 log.info("New user: {0}".format(name))
  166.             elif datetime.now() > users[name].next_refresh:
  167.                 good_karma, bad_karma = calc_score(name)
  168.                 users[name].update(good_karma, bad_karma)
  169.                 log.info("User: {0} scanned {1} times".format(name, users[name].scan_count))
  170.             else:
  171.                 log.info("Skipping user: {0}, next refresh {1}".format(name, users[name].next_refresh))
  172.         #if comment.author_flair_css_class == None:
  173.             #if users[comment.author].warned:
  174.             #   if TimeDelta(users[comment.author].warned_on, datetime.now()) > TimeDelta(days=1):
  175.             #       users[comment.author].comments_eaten.append(comment.id)
  176.             #       comment.remove()
  177.             #else:
  178.             #   Send PM to comment.author:
  179.             #       "To whom it may concern:
  180.             #  
  181.             #           Apologies for the inconvenience, but user flairs are mandatory on r/TheBluePill. Please disable the subreddit's CSS, and toggle your user flair back on. You have 24 hours to comply. After which, all of your posts will be automatically removed (posts automatically removed will be restored when you have re-enabled your flair.) This action was performed automatically, but this account is also attached to a human operator if you have any concerns. We appreciate your continued business!
  182.             #    
  183.             #       etc. etc.
  184.             #       -Management"
  185.             #   users[comment.author].warned = True
  186.             #   users[comment.author].warned_on = datetime.now()
  187.         #else:
  188.             #if users[comment.author].warned = True and len(users[comment.author].comments_eaten) > 0:
  189.             #   for comment in users[comment.author].comments_eaten:
  190.             #       reddit.comment(id=comment).approve()        <-- might be pseudocode
  191.  
  192. NEED_TO_UPDATE_OBJCLASS = False
  193. try:
  194.     users = pickle.load(open("users.pickle", "rb"))
  195.     log.info("Re-loading database")
  196.     if NEED_TO_UPDATE_OBJCLASS:
  197.         log.debug("Updating object format")
  198.         for user in users.values():
  199.             users[user] = Tracker(user.name, user.good_karma, user.bad_karma)
  200.  
  201. except IOError:
  202.     users = {}
  203.     log.info("I/O error accessing database, starting fresh")
  204.  
  205.  
  206. try:
  207.     botloop(datetime.now() + timedelta(minutes=20))
  208.  
  209. except prawcore.PrawcoreException:
  210.     praw_fucked_up()
  211.     botloop(datetime.now() + timedelta(minutes=20))
  212.  
  213. except KeyboardInterrupt:
  214.     log.info("VirtuteTron going off-line")
  215.     pickle.dump(users, open("users.pickle", "wb"))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top