Advertisement
inqw

Untitled

Jun 15th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  1. import praw
  2. import sqlite3
  3. import threading
  4. import re
  5.  
  6. reddit = praw.Reddit(
  7.     username='SaferBot2',
  8.     client_id='tc1sXOkrxp-L6A',
  9.     client_secret='DcYb9HEuhfsLNdEHObnUu0-6S2I',
  10.     refresh_token='3268258423-YjURQF3jQWMazs3ItIIpJKRp5vk',
  11.     redirect_uri='http://localhost:8080',
  12.     user_agent='saferbot v2',
  13.     api_request_delay=1)
  14.  
  15.  
  16. #define a function to connect to the sqlite3 database
  17. def connect_database():
  18.     db = re.sub('\.\w+','',__file__)+'.db'
  19.     conn = sqlite3.connect(db)
  20.     return conn
  21.  
  22. def hate_subreddits():
  23.     db = connect_database()
  24.     reddits = db.cursor().execute('select * from hatesubreddit').fetchall()
  25.     subreddits = '+'.join([subreddit[0] for subreddit in reddits])
  26.     return reddit.subreddit(subreddits)
  27.  
  28. def safe_subreddits():
  29.     db = connect_database()
  30.     reddits = db.cursor().execute('select * from safesubreddit').fetchall()
  31.     return reddits
  32.  
  33. def ban(username,subreddit,percentage,toxicity,message):
  34.     subreddit.banned.add(username,ban_message=message,ban_reason='saferbot2: {}% | {}'.format(percentage, format(toxicity,',d')))
  35.  
  36.  
  37.  
  38. class safeSubreddit(object):
  39.  
  40.     def __init__(self, subreddit):
  41.         self.subreddit = subreddit
  42.         self.sub_name = str(self.subreddit[0])
  43.         self.__call__()
  44.  
  45.     def __call__(self):
  46.         db = connect_database()
  47.         sub_settings = db.cursor().execute('select * from safesubreddit where name=?',[self.sub_name]).fetchone()
  48.         self.ban_message = sub_settings[1]
  49.         self.karma_threshold = sub_settings[2]
  50.         self.ban_length = sub_settings[3]
  51.  
  52. class commentAnalysis(object):
  53.  
  54.     def __init__(self, comments, subreddit):
  55.         self.comments = comments
  56.         self.sub_name=subreddit[0]
  57.         self.redditsubreddit = reddit.subreddit(self.sub_name)
  58.         self.subreddit = safeSubreddit(subreddit)
  59.         self.ban_length = self.subreddit.ban_length
  60.         self.ban_message = self.subreddit.ban_message
  61.         self.karma_threshold = self.subreddit.karma_threshold
  62.         self.__call__()
  63.  
  64.  
  65.     @staticmethod
  66.     def analyze_author(comment, lim=200):
  67.         toxicity = 0
  68.         count = 0
  69.         db = connect_database()
  70.         for post in comment.author.new(limit=lim):
  71.             if str(post.subreddit) in hate_subs:
  72.                 count+=1
  73.                 try:
  74.                     multiplier = db.cursor().execute('select * from hatesubreddit where name=?',[str(comment.subreddit)]).fetchone()[1]
  75.                 except:
  76.                     multiplier = 1
  77.                 toxicity += (post.score*multiplier)
  78.         if toxicity >= self.karma_threshold:
  79.             percentage = round((count/lim)*100,2)
  80.             if not db.cursor().execute('select * from banned where username=?',[str(comment.author)]).fetchone():
  81.                 db.cursor().execute('insert into banned values (?)',[str(comment.author)])
  82.                 db.commit()
  83.                 try:
  84.                     print("[SaferBot2] - /u/{} | {} | {}%".format(comment.author,toxicity,percentage))
  85.                     ban(str(comment.author),self.redditsubreddit,percentage,toxicity,self.ban_message)
  86.                 except Exception as e:
  87.                     print(e)
  88.             else:
  89.                 print("[SaferBot2] - Already banned /u/{} | {} | {}%".format(comment.author,toxicity,percentage))
  90.  
  91.     def __call__(self):
  92.         for comment in self.comments:
  93.             self.analyze_author(comment)
  94.  
  95. def main():
  96.     target = hate_subreddits()
  97.     global hate_subs
  98.     hate_subs = str(target).split('+')
  99.     safe_subs = safe_subreddits()
  100.     db = connect_database()
  101.     while True:
  102.         db = connect_database()
  103.         active = db.cursor().execute('select * from safesubreddit where name=?',['TwoXChromosomes']).fetchone()
  104.         if not active[4]:
  105.             return
  106.         for subreddit in safe_subs:
  107.             hate_comments = [comment for comment in target.comments(limit=5)]
  108.             commentAnalysis(hate_comments,subreddit)
  109.             hate_posts = [post for post in target.new(limit=10)]
  110.             commentAnalysis(hate_posts,subreddit)
  111.  
  112.  
  113. if __name__ == '__main__':
  114.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement