Advertisement
kungming2

Zelda Day Flair Limiter

Apr 5th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3.  
  4. import datetime
  5. import time
  6. import traceback
  7.  
  8. import praw
  9.  
  10. BOT_NAME = 'Zelda Day Flair Limiter'
  11. VERSION_NUMBER= '0.1'
  12. USER_AGENT = BOT_NAME + ' ' + VERSION_NUMBER + ',  a service bot written by u/kungming2.'
  13. SUBREDDIT = "zelda"
  14. USER_NAME = ""
  15. WAIT = 60  # How many seconds the bot sleeps between cycles.
  16.  
  17. # This bot will only allow the following flairs to be posted on their equivalent days. Edit it however you like.
  18. RESTRICTED_DAYS = {"Merchandise": "Monday", "Screenshot": "Saturday"}
  19. RESTRICTED_REPLY = """
  20. Hey there u/{},
  21.  
  22. We apologize for the inconvenience, but "{}" posts are only allowed to be posted on **{}s** (UTC).
  23.  
  24. Please re-submit your post then!
  25.  
  26. ---
  27.  
  28. ^u/{}: ^an ^assistant ^bot ^for ^r/{}
  29. """
  30.  
  31. print('\nLogging in as u/{}...'.format(USER_NAME))
  32.  
  33.  
  34. reddit = praw.Reddit(client_id="",
  35.                      client_secret="", password='',
  36.                      user_agent=USER_AGENT, username=USER_NAME)
  37.  
  38.  
  39. def fetch_posts():
  40.     """
  41.    The main routine that checks the subreddit for posts.
  42.    """
  43.    
  44.     # Get the current day as a string (e.g. Monday)
  45.     date_string = datetime.datetime.utcfromtimestamp(time.time()).strftime("%Y-%m-%d")
  46.    
  47.     posts = []
  48.     posts += list(reddit.subreddit(SUBREDDIT).new(limit=50))
  49.    
  50.     # Go through posts to analyze them.
  51.     for post in posts:
  52.         post_flair = post.link_flair_text
  53.        
  54.         try:
  55.             post_author = post.author.name
  56.         except AttributeError:  # Author is deleted.
  57.             continue
  58.  
  59.         # There is no link flair (this should be taken care of by Artemis)
  60.         if post_flair is None:
  61.             continue
  62.        
  63.         # If the post is saved, that means I've acted upon it already.
  64.         if post.saved:
  65.             continue
  66.  
  67.         # The post flair of this post is restricted to certain days only. Let's check.
  68.         if post_flair in RESTRICTED_DAYS:
  69.             allowable_day = RESTRICTED_DAYS[post_flair]
  70.            
  71.             # The day does not match the allowable day. Sticky a comment about that, and remove the post.
  72.             if allowable_day != date_string:
  73.                 print("\n>> Post is a type that should not be posted today.\n")
  74.                 my_reply = post.reply(RESTRICTED_REPLY.format(post_author, post_flair, allowable_day, USER_NAME,
  75.                                                               SUBREDDIT))  # Reply to the post about our rules.
  76.                 my_reply.mod.distinguish(sticky=True)  # Distinguish the bot's comment.
  77.                 post.mod.remove()  # Remove the post.
  78.                 post.save()  # Save the post so we know we've acted on it.
  79.        
  80. while True:
  81.     # noinspection PyBroadException
  82.     try:
  83.         fetch_posts()
  84.     except Exception as e:
  85.         traceback.print_exc()
  86.  
  87.     print('Cycle completed. I will restart in {} seconds. \n'.format(WAIT))
  88.     time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement