Advertisement
kungming2

Check Comment Karma for Posting

Jan 6th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. import json
  2. import traceback
  3. import time
  4. import requests
  5.  
  6. import praw
  7.  
  8. BOT_NAME = 'Enforce Minimum Score Bot'
  9. VERSION_NUMBER= '0.1'
  10. USER_AGENT = BOT_NAME + ' ' + VERSION_NUMBER + ',  a service bot for this subreddit. Written by u/kungming2.'
  11. SUBREDDIT = ""
  12. USER_NAME = ""
  13. PROCESSED_USERS = []  # A list of already checked users that have enough karma.
  14. WAIT = 30
  15.  
  16. print('\n|======================Logging in...======================|')
  17.  
  18.  
  19. reddit = praw.Reddit(client_id="",
  20.                      client_secret="", password='',
  21.                      user_agent=USER_AGENT, username=USER_NAME)
  22.  
  23.  
  24. def check_user_karma(username, subreddit_name):
  25.     """
  26.    This function takes a username and the name of a subreddit, then queries Pushshift to add up the score
  27.    of comments that the user has on that community. It returns True if the user has met the minimum standard of
  28.    comment karma, and False if not.
  29.    """
  30.  
  31.     list_of_karma = []
  32.     minimum_comment_karma = 100
  33.     can_post = True
  34.    
  35.     # Form our search query.
  36.     api_search_query = ("https://api.pushshift.io/reddit/search/comment/?subreddit={}"
  37.                         "&author={}&sort_type=score&sort=desc&aggs=author&size=500")
  38.     api_search_query = api_search_query.format(subreddit_name, username)
  39.    
  40.     # Get the data from Pushshift as JSON.
  41.     retrieved_data = requests.get(api_search_query)
  42.     returned_comments = retrieved_data.json()['data']
  43.    
  44.     # Iterate over the comments and append how much karma they got.
  45.     for item in returned_comments:
  46.         list_of_karma.append(item['score'])
  47.    
  48.     print("u/{} has a comment score of {} on r/{}.".format(username, sum(list_of_karma), subreddit_name))
  49.    
  50.     # If the user has less of a score than we want, set the boolean to False.
  51.     if sum(list_of_karma) < minimum_comment_karma:
  52.         can_post = False
  53.        
  54.     return can_post
  55.  
  56.  
  57. def main_stream():
  58.     """
  59.    The main function to fetch comments from the subreddit.
  60.    """
  61.    
  62.     r = reddit.subreddit(SUBREDDIT)
  63.    
  64.     for submission in r.stream.submissions():  # Watching the stream...
  65.    
  66.         try:
  67.             submission_author = submission.author.name
  68.         except AttributeError:
  69.             # Author is deleted. We don't care about this post.
  70.             continue
  71.        
  72.         if submission_author == USER_NAME:
  73.             # Don't reply to yourself, bot!
  74.             continue
  75.         elif submission_author in PROCESSED_USERS:
  76.             # We've checked this user before and they can post.
  77.             continue
  78.            
  79.         if submission.saved:  # Saved comments have already been acted on.
  80.             continue
  81.    
  82.         # Finally, we can act.
  83.         submission.save()  # Save this so we won't process it more than once.
  84.        
  85.         # If the user doesn't have enough comment score, remove the post.
  86.         if not check_user_karma(submission_author, SUBREDDIT):
  87.             submission.mod.remove()
  88.             print("Removed post by u/{}. Insufficient comment score.".format(submission_author))
  89.         else:
  90.             PROCESSED_USERS.append(submission_author)
  91.    
  92.     return
  93.  
  94.  
  95. while True:
  96.     # noinspection PyBroadException
  97.     try:
  98.         main_stream()
  99.     except Exception as e:
  100.         traceback.print_exc()
  101.  
  102.     print('The bot will restart in {} seconds. \n'.format(WAIT))
  103.     time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement