Advertisement
Guest User

Untitled

a guest
Feb 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. from datetime import date, timedelta
  2. from getpass import getpass
  3. import os
  4.  
  5. import praw
  6.  
  7.  
  8. def fix_sticky(reddit, subreddit_name):
  9.     title_prefix = "Daily General Discussion "
  10.     for submission in reddit.subreddit(subreddit_name).stream.submissions():
  11.         # check if the post is by automod
  12.         if submission.author.name == "AutoModerator"
  13.             # check if post is today's daily discussion
  14.             if submission.title == f"{title_prefix}{date.today().strftime('%B %d, %Y')}":
  15.                 if submission.stickied:
  16.                     # we do nothing, everything is as it should be.
  17.                     print(f'Sticky did not need fixing at {datetime.now()}')
  18.                     return False
  19.                 # check if old daily discussion is still stickied
  20.                 top_sticky = reddit.get_sticky(subreddit_name, bottom=False)
  21.                 yesterday = date.today() - timedelta(1)
  22.                 if top_sticky.title == f"{title_prefix}{yesterday.strftime('%B %d, %Y')}":
  23.                     raise ValueError('Top sticky invalid, check title prefix and date.')
  24.                 # unsticky old daily discussion.
  25.                 top_sticky.mod.sticky(state=False, top=True)
  26.                 # sticky old daily discussion.
  27.                 submission.mod.sticky(state=True, top=True)
  28.                 print(f'Fixed sticky at {datetime.now()}')
  29.                 return True
  30.  
  31.  
  32. if __name__ == "__main__":
  33.     user = input('Username:')
  34.     password = getpass('Password:')
  35.     reddit = praw.Reddit(client_id=os.getenv('CLIENT_ID'),
  36.                          client_secret=os.getenv('CLIENT_SECRET'),
  37.                          user_agent='Autosticky Fix',
  38.                          username=username,
  39.                          password=password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement