Guest User

reddit_ribbon

a guest
Aug 14th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. """
  2. Credit: Rudy Pikulik - for comment validation from RedditSilverRobot
  3. Reddit_Ribbon: A bot for handing out Reddit participation ribbons
  4. Copyright 2018 /u/CaulkParty
  5.  
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9.  
  10.    http://www.apache.org/licenses/LICENSE-2.0
  11.  
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17.  
  18. """
  19. import re
  20. import praw
  21.  
  22. # Create the Reddit instance
  23. REDDIT = praw.Reddit('reddit_ribbon')
  24.  
  25. SUBREDDIT = REDDIT.subreddit('all')
  26. COMMENTS = SUBREDDIT.stream.comments()
  27. COMMAND = "!redditribbon"
  28.  
  29. def main():
  30.     """
  31.    The main function for running the bot.
  32.    """
  33.     for comment in COMMENTS:
  34.         #print(submission.title)
  35.         if validate_comment(comment):
  36.             # Do a case insensitive search
  37.             if re.search(COMMAND, comment.body.lower(), re.IGNORECASE):
  38.                 # Reply to the post
  39.                 comment.reply("[Here is your Reddit Participation Ribbon!](https://i.imgur.com/7HcVdqw.jpg)")
  40.                 print("Bot replying to : ", comment.author)
  41.  
  42. def validate_comment(comment):
  43.     """
  44.    Decides whether or not to reply to a given comment.
  45.        - Must contain command
  46.        - Must not have already replied
  47.        - Must not reply to self
  48.  
  49.    Args:
  50.        comment - the comment object to validate
  51.    """
  52.     if COMMAND in comment.body.lower():
  53.         # We wrote the comment, don't loop.
  54.         if comment.author.name is "reddit_ribbon":
  55.             print(comment, "Cannot respond to self.")
  56.             return False
  57.         # Parent comment was deleted, don't respond.
  58.         if get_receiver(comment) == '[deleted]':
  59.             print(comment, "Parent comment was deleted!")
  60.             return False
  61.  
  62.         comment.refresh()
  63.         for child_comment in comment.replies:
  64.             if child_comment.author.name == "reddit_ribbon":
  65.                 print(comment, "Already replied to this comment. Will not do it again.")
  66.                 return False
  67.         return True
  68.     return False
  69.  
  70. def get_receiver(comment):
  71.     """
  72.    Gets the receiver of the comment.
  73.  
  74.    Args:
  75.        comment - the comment object to get the receiver for.
  76.    """
  77.     text = comment.body.lower().split()
  78.     try:
  79.         # Kind of gross looking code below. Splits the comment exactly once at '!RedditSilver',
  80.         # then figures out if the very next character is a new line. If it is, respond to parent.
  81.         # If it is not a new line, either respond to the designated person or the parent.
  82.  
  83.         split = comment.body.lower().split(COMMAND, 1)[1].replace(' ', '')
  84.         if split[0] is "\n":
  85.             try:
  86.                 receiver = comment.parent().author.name
  87.             except AttributeError:
  88.                 receiver = '[deleted]'
  89.         else:
  90.             receiver = text[text.index(COMMAND) + 1]
  91.     # An IndexError is thrown if the user did not specify a recipient.
  92.     except IndexError:
  93.         try:
  94.             receiver = comment.parent().author.name
  95.         except AttributeError:
  96.             receiver = '[deleted]'
  97.     # A ValueError is thrown if '!RedditSilver' is not found. Example: !RedditSilverTest would throw this.
  98.     except ValueError:
  99.         return None
  100.     if '/u/' in receiver:
  101.         receiver = receiver.replace('/u/', '')
  102.     if 'u/' in receiver:
  103.         receiver = receiver.replace('u/', '')
  104.     if '/' in receiver:
  105.         receiver = receiver.replace('/', '')
  106.     # This line is to change the name from all lowercase.
  107.         receiver = REDDIT.redditor(receiver).name
  108.     return receiver
  109.  
  110. if __name__ == '__main__':
  111.     main()
Add Comment
Please, Sign In to add comment