Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import traceback
- import time
- import requests
- import praw
- BOT_NAME = 'Enforce Minimum Score Bot'
- VERSION_NUMBER= '0.1'
- USER_AGENT = BOT_NAME + ' ' + VERSION_NUMBER + ', a service bot for this subreddit. Written by u/kungming2.'
- SUBREDDIT = ""
- USER_NAME = ""
- PROCESSED_USERS = [] # A list of already checked users that have enough karma.
- WAIT = 30
- print('\n|======================Logging in...======================|')
- reddit = praw.Reddit(client_id="",
- client_secret="", password='',
- user_agent=USER_AGENT, username=USER_NAME)
- def check_user_karma(username, subreddit_name):
- """
- This function takes a username and the name of a subreddit, then queries Pushshift to add up the score
- of comments that the user has on that community. It returns True if the user has met the minimum standard of
- comment karma, and False if not.
- """
- list_of_karma = []
- minimum_comment_karma = 100
- can_post = True
- # Form our search query.
- api_search_query = ("https://api.pushshift.io/reddit/search/comment/?subreddit={}"
- "&author={}&sort_type=score&sort=desc&aggs=author&size=500")
- api_search_query = api_search_query.format(subreddit_name, username)
- # Get the data from Pushshift as JSON.
- retrieved_data = requests.get(api_search_query)
- returned_comments = retrieved_data.json()['data']
- # Iterate over the comments and append how much karma they got.
- for item in returned_comments:
- list_of_karma.append(item['score'])
- print("u/{} has a comment score of {} on r/{}.".format(username, sum(list_of_karma), subreddit_name))
- # If the user has less of a score than we want, set the boolean to False.
- if sum(list_of_karma) < minimum_comment_karma:
- can_post = False
- return can_post
- def main_stream():
- """
- The main function to fetch comments from the subreddit.
- """
- r = reddit.subreddit(SUBREDDIT)
- for submission in r.stream.submissions(): # Watching the stream...
- try:
- submission_author = submission.author.name
- except AttributeError:
- # Author is deleted. We don't care about this post.
- continue
- if submission_author == USER_NAME:
- # Don't reply to yourself, bot!
- continue
- elif submission_author in PROCESSED_USERS:
- # We've checked this user before and they can post.
- continue
- if submission.saved: # Saved comments have already been acted on.
- continue
- # Finally, we can act.
- submission.save() # Save this so we won't process it more than once.
- # If the user doesn't have enough comment score, remove the post.
- if not check_user_karma(submission_author, SUBREDDIT):
- submission.mod.remove()
- print("Removed post by u/{}. Insufficient comment score.".format(submission_author))
- else:
- PROCESSED_USERS.append(submission_author)
- return
- while True:
- # noinspection PyBroadException
- try:
- main_stream()
- except Exception as e:
- traceback.print_exc()
- print('The bot will restart in {} seconds. \n'.format(WAIT))
- time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement