Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import praw
  2.  
  3. r = praw.Reddit(client_id='',
  4. client_secret='',
  5. password='',
  6. user_agent='',
  7. username='')
  8. #Settings
  9. SUBREDDIT = 'Robin_Redbreast'
  10. USER = 'Robin_Redbreast'
  11. POST_ID = '77v6xj'
  12.  
  13. #Message Constants
  14. OPT_OUT = 'You have successfully opted out.'
  15. OPT_IN = 'You have successfully opted in.'
  16. FOOTER = '^(I am a bot, and this message was sent automatically. | Want to opt out? Reply to this message with !optout |) [^Link ^to ^post]({plink})'
  17. TITLE = 'New post in /r/{subreddit}: {post_title}.'
  18. BODY = '''There has been a new post in /r/Robin_Redbreast by /u/{user}:\n\n>{preview}\n\n---\n\n'''
  19.  
  20. def addFriends():
  21. authors = list(set([(c, c.author) for c in r.submission(id=POST_ID).comments if not c.saved]))
  22. friends = r.user.friends()
  23. for comment, author in authors:
  24. comment.save()
  25. if '!quebec' in comment.body.lower():
  26. if author not in friends:
  27. author.friend()
  28. author.message('Opt-in successful', OPT_IN)
  29. else:
  30. author.message('Error', 'You are already opted-in.')
  31.  
  32.  
  33. def removeFriends():
  34. for message in r.inbox.unread(limit=None):
  35. message.mark_read()
  36. if '!optout' in message.body.lower()+message.subject.lower():
  37. if message.author in r.user.friends():
  38. message.author.unfriend()
  39. message.reply(OPT_OUT)
  40. else:
  41. message.reply('Error: You are not currently opted-in.')
  42.  
  43. def update_friends(post):
  44. for friend in r.user.friends():
  45. friend.message(TITLE.format(subreddit=SUBREDDIT, post_title=post.title), BODY.format(user=post.author.name, preview=post.selftext[:300]+'...')+FOOTER.format(plink=post.shortlink))
  46.  
  47. def sendMessages():
  48. for post in r.subreddit(SUBREDDIT).new(limit=5):
  49. if not post.saved:
  50. post.save()
  51. if post.author.name.lower() == USER.lower() and '[ms]' in post.title.lower():
  52. update_friends(post)
  53.  
  54. def main():
  55. addFriends()
  56. removeFriends()
  57. sendMessages()
  58.  
  59.  
  60. if __name__ == '__main__':
  61. while True:
  62. try:
  63. main()
  64. except Exception as e:
  65. print(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement