Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import sqlite3
- import threading
- import re
- reddit = praw.Reddit(
- username='SaferBot2',
- client_id='tc1sXOkrxp-L6A',
- client_secret='DcYb9HEuhfsLNdEHObnUu0-6S2I',
- refresh_token='3268258423-YjURQF3jQWMazs3ItIIpJKRp5vk',
- redirect_uri='http://localhost:8080',
- user_agent='saferbot v2',
- api_request_delay=1)
- #define a function to connect to the sqlite3 database
- def connect_database():
- db = re.sub('\.\w+','',__file__)+'.db'
- conn = sqlite3.connect(db)
- return conn
- def hate_subreddits():
- db = connect_database()
- reddits = db.cursor().execute('select * from hatesubreddit').fetchall()
- subreddits = '+'.join([subreddit[0] for subreddit in reddits])
- return reddit.subreddit(subreddits)
- def safe_subreddits():
- db = connect_database()
- reddits = db.cursor().execute('select * from safesubreddit').fetchall()
- return reddits
- def ban(username,subreddit,percentage,toxicity,message):
- subreddit.banned.add(username,ban_message=message,ban_reason='saferbot2: {}% | {}'.format(percentage, format(toxicity,',d')))
- class safeSubreddit(object):
- def __init__(self, subreddit):
- self.subreddit = subreddit
- self.sub_name = str(self.subreddit[0])
- self.__call__()
- def __call__(self):
- db = connect_database()
- sub_settings = db.cursor().execute('select * from safesubreddit where name=?',[self.sub_name]).fetchone()
- self.ban_message = sub_settings[1]
- self.karma_threshold = sub_settings[2]
- self.ban_length = sub_settings[3]
- class commentAnalysis(object):
- def __init__(self, comments, subreddit):
- self.comments = comments
- self.sub_name=subreddit[0]
- self.redditsubreddit = reddit.subreddit(self.sub_name)
- self.subreddit = safeSubreddit(subreddit)
- self.ban_length = self.subreddit.ban_length
- self.ban_message = self.subreddit.ban_message
- self.karma_threshold = self.subreddit.karma_threshold
- self.__call__()
- @staticmethod
- def analyze_author(comment, lim=200):
- toxicity = 0
- count = 0
- db = connect_database()
- for post in comment.author.new(limit=lim):
- if str(post.subreddit) in hate_subs:
- count+=1
- try:
- multiplier = db.cursor().execute('select * from hatesubreddit where name=?',[str(comment.subreddit)]).fetchone()[1]
- except:
- multiplier = 1
- toxicity += (post.score*multiplier)
- if toxicity >= self.karma_threshold:
- percentage = round((count/lim)*100,2)
- if not db.cursor().execute('select * from banned where username=?',[str(comment.author)]).fetchone():
- db.cursor().execute('insert into banned values (?)',[str(comment.author)])
- db.commit()
- try:
- print("[SaferBot2] - /u/{} | {} | {}%".format(comment.author,toxicity,percentage))
- ban(str(comment.author),self.redditsubreddit,percentage,toxicity,self.ban_message)
- except Exception as e:
- print(e)
- else:
- print("[SaferBot2] - Already banned /u/{} | {} | {}%".format(comment.author,toxicity,percentage))
- def __call__(self):
- for comment in self.comments:
- self.analyze_author(comment)
- def main():
- target = hate_subreddits()
- global hate_subs
- hate_subs = str(target).split('+')
- safe_subs = safe_subreddits()
- db = connect_database()
- while True:
- db = connect_database()
- active = db.cursor().execute('select * from safesubreddit where name=?',['TwoXChromosomes']).fetchone()
- if not active[4]:
- return
- for subreddit in safe_subs:
- hate_comments = [comment for comment in target.comments(limit=5)]
- commentAnalysis(hate_comments,subreddit)
- hate_posts = [post for post in target.new(limit=10)]
- commentAnalysis(hate_posts,subreddit)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement