Advertisement
Guest User

VirtueTron9000 v0.1.4e

a guest
Jun 27th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.94 KB | None | 0 0
  1. #!/usr/bin/python
  2. LICENCE = "WTFPL", "http://www.wtfpl.net/about/"
  3. VERSION = "v0.1.4e"
  4.  
  5. print("Hello, Reddit!")
  6. print("VirtueTron9000 {0} (c) CrashARuntimeToday@outlook.com".format(VERSION))
  7.  
  8. import pickle
  9. import praw
  10. from datetime import datetime, timedelta
  11. from math import ceil
  12. from time import sleep
  13.  
  14.  
  15. reddit = praw.Reddit(client_id="VirtueTron9000",
  16.                      client_secret="🤖🤡🍆💯™",
  17.                      username="SignalAVirtueToday",
  18.                      password="https://youtu.be/RCVJ7bujnSc",
  19.                      user_agent="VirtueTron 9000 {0}".format(VERSION))
  20. next_recalc = datetime.now() + timedelta(minutes=20)
  21.  
  22. class Tracker:
  23.     def __init__(self, name, good_karma, bad_karma):
  24.         self.name = name
  25.         self.smv = None
  26.         self.scan_count = 0
  27.         self.next_refresh = None
  28.         self.update(good_karma, bad_karma)
  29.    
  30.     def update(self, good_karma, bad_karma):
  31.         self.good_karma = good_karma
  32.         self.bad_karma = bad_karma
  33.         self.scan_count += 1
  34.         self.next_refresh = datetime.now() + timedelta(minutes=15)
  35.  
  36.  
  37. def calc_score(name):
  38.     NAUGHTY_LIST = "TheRedPill", "MarriedRedPill", "ChristianRedPill", "MGTOW", "Braincels", "AskTRP", "AskMRP", "RedPillWomen", "RedPillWives", "CringeAnarchy", "The_Donald", "RPChristians"
  39.     good_karma, bad_karma, good_count, bad_count = 0, 0, 0, 0
  40.     for comment in reddit.redditor(name).comments.new(limit=100):
  41.         if comment.subreddit.display_name == "TheBluePill":
  42.             good_karma += comment.score - 1
  43.             good_count += 1
  44.         elif comment.subreddit.display_name in NAUGHTY_LIST and comment.score > 1:
  45.             bad_karma -= comment.score - 1
  46.             bad_count += 1
  47.     if good_count > 0:
  48.         good_karma /= good_count
  49.     if bad_count > 0:
  50.         bad_karma /= bad_count
  51.         if bad_count > 20:
  52.             print("User: {0} is vexatious ({1} posts in NAUGHTY_LIST)".format(name, bad_count))
  53.             reddit.subreddit("TheBluePill").flair.set(name, "VEXATIOUS LITIGANT", "vexatious")
  54.     print("Scanned user: {0}, good_karma: {1} ({2} comments), bad_karma: {3} ({4} comments)".format(name, good_karma, good_count, bad_karma, bad_count))
  55.     return good_karma, bad_karma
  56.  
  57.  
  58. def update_flairs():
  59.     IMMUTABLE_FLAIRS = "vanguard", "vexatious", "endorsedflair", "alpha", "betaasfuck", "feeemale", "purged"
  60.     print("Recalculating SMV")
  61.     i = 0
  62.     total = len(users)
  63.     for user in sorted(users.values(), key=lambda x: x.good_karma + x.bad_karma):
  64.         i += 1
  65.         user.smv = ceil((i / total) * 10)
  66.         current_flair = None
  67.         try:
  68.             for flair in reddit.subreddit("TheBluePill").flair(redditor=user.name):
  69.                 current_flair = flair["flair_css_class"]
  70.         except prawcore.exceptions.Forbidden:       #Not sure why Reddit doesn't like this API call and too lazy to run Wireshark
  71.             sleep(1)
  72.             continue
  73.        
  74.         if any(reddit.subreddit("TheBluePill").banned(redditor=user.name)):
  75.             if current_flair != "purged":
  76.                 print("Marking user: {0} purged".format(user.name))
  77.                 reddit.subreddit("TheBluePill").flair.set(user.name, "PURGED", "purged")
  78.             else:
  79.                 print("User: {0} is purged".format(user.name))
  80.         else:
  81.             print("User: {0}, SMV: {1}, score: {2} (current flair {3})".format(user.name, user.smv, user.good_karma + user.bad_karma, current_flair))
  82.  
  83.             if current_flair in IMMUTABLE_FLAIRS:
  84.                 print("Not changing user: {0} (immutable flair {1})".format(user.name, current_flair))
  85.             elif current_flair != "hb{0}".format(user.smv):
  86.                 print("Updating user: {0} flair to hb{1}".format(user.name, user.smv))
  87.                 reddit.subreddit("TheBluePill").flair.set(user.name, "Hβ{0}".format(user.smv), "hb{0}".format(user.smv))
  88.                 if user.smv > 7:
  89.                     if user.name not in reddit.subreddit("TheBluePill").contributor():
  90.                         print("Adding approved contributor: {0}".format(user.name))
  91.                         reddit.subreddit("TheBluePill").contributor.add(user.name)
  92.                 elif user.smv < 4:
  93.                     if user.name in reddit.subreddit("TheBluePill").contributor():
  94.                         print("Removing approved contributor: {0}".format(user.name))
  95.                         reddit.subreddit("TheBluePill").contributor.remove(user.name)
  96.             else:
  97.                 print("User: {0} still an HB{1}".format(user.name, user.smv))
  98.  
  99.    
  100.     pickle.dump(users, open("users.pickle", "wb"))
  101.     print("Next refresh: {0}".format(next_recalc))
  102.  
  103.  
  104. def botloop():
  105.     for comment in reddit.subreddit("TheBluePill").stream.comments():
  106.         if datetime.now() > next_recalc:
  107.             update_flairs()
  108.  
  109.         if comment.author != None:
  110.             name = comment.author.name
  111.             if not name in users.keys():
  112.                 good_karma, bad_karma = calc_score(name)
  113.                 users[name] = Tracker(name, good_karma, bad_karma)
  114.                 print("New user: {0}".format(name))
  115.             elif datetime.now() > users[name].next_refresh:
  116.                 good_karma, bad_karma = calc_score(name)
  117.                 users[name].update(good_karma, bad_karma)
  118.                 print("User: {0} scanned {1} times".format(name, users[name].scan_count))
  119.             else:
  120.                 print("Skipping user:{0}, next refresh {1}".format(name, users[name].next_refresh))
  121.  
  122.  
  123. try:
  124.     users = pickle.load(open("users.pickle", "rb"))
  125.     print("Re-loading database")
  126. except IOError:
  127.     users = {}
  128.     print("I/O error accessing database, starting fresh")
  129.  
  130. try:
  131.     botloop()
  132. except Exception as e:
  133.     if e == KeyboardInterrupt:
  134.         print("VirtuteTron going off-line")
  135.         pickle.dump(users, open("users.pickle", "wb"))
  136.     elif e == praw.exceptions.PRAWException:
  137.         sleep(120)
  138.         botloop()
  139.     else:
  140.         raise e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement