Advertisement
kungming2

Posts Weekday Limiter

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