Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- USERNAME = ''
- PASSWORD = ''
- CLIENT_ID = ''
- CLIENT_SECRET = ''
- USER_AGENT = ''
- def connect_to_reddit():
- reddit = praw.Reddit(client_id=CLIENT_ID, client_secret=CLIENT_SECRET,
- user_agent=USER_AGENT,
- username=USERNAME, password=PASSWORD)
- return reddit
- def post_to_subreddit(submission_title, reddit, comment):
- reddit.subreddit('maricoelquelolea').submit(title=submission_title, url='www.reddit.com{}'.format(comment.permalink), send_replies=False)
- def create_submission_title(comment, reddit):
- # Separates each individual word from the comment in to a list. It then scans that list for instances
- # of the word "venezuela", and also check to see if there is in an instance with the word "venezuela" with a
- # comma after it. This then returns the index of the word to the variable.
- comment_word_list = comment.body.lower().split(' ')
- if 'venezuela' in comment_word_list:
- keyword_index = comment_word_list.index('venezuela')
- elif 'venezuela,' in comment_word_list:
- keyword_index = comment_word_list.index('venezuela,')
- else:
- comment.save() # Basically just stops errors from occurring. There is probably a better way to do this
- # but I am too lazy to check. It works, so that is all that matters :P
- return
- # Creates the submission title using the index value that was retrieved in the previous step. If the word
- # "venezuela" appears very early on in the comment, then it will use the first word of the comment as the starting
- # point for the submission title. Otherwise, it finds the index of venezuela in the list, and starts creating
- # the title from the 3rd preceeding word. For example - "... the people in venezuela bla bla bla ...". Was not sure
- # how to word this explanation, as I am bad with words.
- submission_title = '/u/{} in /r/{} | '.format(comment.author.name, comment.subreddit)
- if keyword_index < 4:
- for word in comment_word_list[0:len(comment_word_list)]:
- submission_title += word+' '
- if len(submission_title) > 270:
- break
- elif keyword_index > 3:
- for word in comment_word_list[keyword_index-3:keyword_index+len(comment_word_list)]:
- submission_title += word+' '
- if len(submission_title) > 270:
- break
- # Adds the "..." at the end of the submission title and then posts the submission to the subreddit.
- submission_title += '...'
- post_to_subreddit(submission_title, reddit, comment)
- def search_for_keyword(reddit):
- for comment in reddit.subreddit('all').stream.comments():
- if 'venezuela' in comment.body.lower() and not comment.saved:
- try:
- create_submission_title(comment, reddit)
- comment.save()
- except:
- pass
- def main():
- reddit = connect_to_reddit()
- while True:
- search_for_keyword(reddit)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement