I've written a short script to use Reddit's PRAW API to search a subreddit for articles and if certain word/words are in the article title, replace those words with a random word taken from a list of other words. I'm putting it in a dictionary because I'm going to write the url & article title to a database before tweeting it out so I don't send out duplicates. I'm mostly curious if there's a better way to write the replacer()
function.
For example: "World leaders brace for Trump's speech" --> "World leaders brace for spongebob's speech"
nicknames
in matches()
is a list of nicknames from another file.
def replacer(title, words, randoms):
'''title is string you want to split.
words is a list of words you are looking to match.
randoms is a list of random words to replace with words.'''
new_title = []
for word in title.split(' '):
if word in words:
word = random.choice(randoms)
new_title.append(word)
return ' '.join(new_title)
def matches(login, subreddit):
page = login.subreddit(subreddit)
# used for the any statement
combined_names = ["Name's", "Mister Name's", "Firstname Lastname", "Firstname", "Lastname", "Mister Firstname", "Mister Lastname"]
# target words to replace
names_apost = ["Name's", "Mister Name's"]
names_no_apost = ["Firstname Lastname", "Firstname", "Lastname", "Mister Firstname", "Mister Lastname"]
# for appropriate conjugation
nicknames_apostrophe = [i + "'s" for i in nicknames]
post_dict = {}
top_posts = page.hot(limit=100)
for post in top_posts:
title = post.title
if any(word in title for word in combined_names):
first_iter = replacer(post.title, names_apost, nicknames_apostrophe)
second_iter = replacer(first_iter, names_no_apost, nicknames)
if post.ups > 1000:
value = post.ups, post.url
post_dict[second_iter] = value
return post_dict