Guest User

Reddit to Slack

a guest
Nov 19th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. import json
  2. import requests
  3. import pickle
  4. import praw
  5. from time import sleep
  6.  
  7. #  http://praw.readthedocs.io/en/latest/getting_started/authentication.html
  8. reddit = praw.Reddit(client_id='your app client id',
  9.                      client_secret='your app client secret',
  10.                      username='your bot reddit username',
  11.                      password='your bot reddit password',
  12.                      user_agent='python3:KeyWordNotify:v1 (/u/Arkaon)')
  13.  
  14. webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
  15.  
  16. # this is the account that will receive the messages
  17. you = reddit.redditor('your reddit username')
  18. # scan comments in this subreddit
  19. subreddit = reddit.subreddit('your subreddit name')
  20.  
  21. keywords = ['keyword1', 'keyword2', 'keyword3']  # case insensitive
  22. ignore_users = ['baduser1', 'baduser2', 'baduser3']  # case SENSITIVE
  23.  
  24. # a list of submission ids that you have already been notified for
  25. already_alerted_submissions = []
  26.  
  27. comment_stream = subreddit.stream.comments()
  28.  
  29.  
  30. # a list of submission ids that you have already been notified for
  31. already_alerted_submissions = []
  32.  
  33. comment_stream = subreddit.stream.comments()
  34.  
  35. def get_list_from_pickle(filename):
  36.     try:
  37.         with open(filename, 'rb') as fp:
  38.             return pickle.load(fp)
  39.     except:
  40.         with open(filename, 'wb') as fp:
  41.             pickle.dump([], fp)
  42.         return []
  43.  
  44. def main():
  45.  
  46.     alerted_comments = get_list_from_pickle('alerted_comments.pickle')
  47.  
  48.     try:
  49.  
  50.         for comment in comment_stream:
  51.             if comment.id in alerted_comments:
  52.                 continue
  53.  
  54.             if comment.author:  # if comment author hasn't deleted
  55.                 if comment.author.name in ignore_users:
  56.                     continue
  57.  
  58.             for kw in keywords:
  59.                 if kw.lower() in comment.body.lower():  # case insensitive check
  60.                     alerted_comments.append(comment.id)
  61.  
  62.                     while len(alerted_comments) > 100:
  63.                         del alerted_comments[0]
  64.  
  65.                     with open('alerted_comments.pickle', 'wb') as fp:
  66.                         pickle.dump(alerted_comments, fp)
  67.  
  68.             for kw in keywords:
  69.                 if kw.lower() in comment.body.lower():  # case insensitive check
  70.                     already_alerted_submissions.append(comment.submission.id)
  71.                     msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
  72.                         kw, comment.permalink)
  73.                     slack_data = {'text': msg, 'mrkdwn': True}
  74.  
  75.                     response = requests.post('https://hooks.slack.com/services/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',
  76.                                              data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
  77.                     if response.status_code != 200:
  78.                         raise ValueError('Request to slack returned an error %s, the response is:\n%s' % (
  79.                             response.status_code, response.text))
  80.  
  81.     except Exception as e:
  82.         print('There was an error: ' + str(e))
  83.         sleep(60)  # wait for 60 seconds before restarting
  84.         main()
  85.  
  86. if __name__ == '__main__':
  87.     main()
Add Comment
Please, Sign In to add comment