daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jun 30th, 2018 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import praw
  2.  
  3. USERNAME = ''
  4. PASSWORD = ''
  5. CLIENT_ID = ''
  6. CLIENT_SECRET = ''
  7. USER_AGENT = ''
  8.  
  9. def connect_to_reddit():
  10.     reddit = praw.Reddit(client_id=CLIENT_ID, client_secret=CLIENT_SECRET,
  11.                          user_agent=USER_AGENT,
  12.                          username=USERNAME, password=PASSWORD)
  13.     return reddit
  14.  
  15.  
  16. def post_to_subreddit(submission_title, reddit, comment):
  17.     reddit.subreddit('maricoelquelolea').submit(title=submission_title, url='www.reddit.com{}'.format(comment.permalink), send_replies=False)
  18.  
  19.  
  20. def create_submission_title(comment, reddit):
  21.     # Separates each individual word from the comment in to a list. It then scans that list for instances
  22.     # of the word "venezuela", and also check to see if there is in an instance with the word "venezuela" with a
  23.     # comma after it. This then returns the index of the word to the variable.
  24.     comment_word_list = comment.body.lower().split(' ')
  25.     if 'venezuela' in comment_word_list:
  26.         keyword_index = comment_word_list.index('venezuela')
  27.     elif 'venezuela,' in comment_word_list:
  28.         keyword_index = comment_word_list.index('venezuela,')
  29.     else:
  30.         comment.save() # Basically just stops errors from occurring. There is probably a better way to do this
  31.                        # but I am too lazy to check. It works, so that is all that matters :P
  32.         return
  33.  
  34.     # Creates the submission title using the index value that was retrieved in the previous step. If the word
  35.     # "venezuela" appears very early on in the comment, then it will use the first word of the comment as the starting
  36.     # point for the submission title. Otherwise, it finds the index of venezuela in the list, and starts creating
  37.     # the title from the 3rd preceeding word. For example - "... the people in venezuela bla bla bla ...". Was not sure
  38.     # how to word this explanation, as I am bad with words.
  39.     submission_title = '/u/{} in /r/{} | '.format(comment.author.name, comment.subreddit)
  40.     if keyword_index < 4:
  41.         for word in comment_word_list[0:len(comment_word_list)]:
  42.             submission_title += word+' '
  43.             if len(submission_title) > 270:
  44.                 break
  45.     elif keyword_index > 3:
  46.         for word in comment_word_list[keyword_index-3:keyword_index+len(comment_word_list)]:
  47.             submission_title += word+' '
  48.             if len(submission_title) > 270:
  49.                 break
  50.  
  51.     # Adds the "..." at the end of the submission title and then posts the submission to the subreddit.
  52.     submission_title += '...'
  53.     post_to_subreddit(submission_title, reddit, comment)
  54.  
  55.  
  56. def search_for_keyword(reddit):
  57.     for comment in reddit.subreddit('all').stream.comments():
  58.         if 'venezuela' in comment.body.lower() and not comment.saved:
  59.             try:
  60.                 create_submission_title(comment, reddit)
  61.                 comment.save()
  62.             except:
  63.                 pass
  64.  
  65.  
  66. def main():
  67.     reddit = connect_to_reddit()
  68.     while True:
  69.         search_for_keyword(reddit)
  70.  
  71.  
  72. if __name__ == '__main__':
  73.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top