So i wrote a twitter bot and it pulls the top submission from a certain subreddit and posts it to twitter.
It runs as a cron job, if the top post is the same as last time it ran the twitter api throws an error that the post is a duplicate, im looking to catch this error and then re run the function with the key incremented so as to try the next post heres my code (api credentials left out)
def pull_reddit_post(key):
r = reddit.Reddit(user_agent='ghettoTwit')
submissions = r.get_subreddit('ghettojerk').get_hot(limit=10)
post = [str(x) for x in submissions]
post = post[key]
post = post.lstrip('0123456789: ')
return post
def tweet_reddit_post(key):
global api
post = pull_reddit_post(key)
api.PostUpdates(pull_reddit_post(key))
try:
key = 0
tweet_reddit_post(key)
except:
key = key + 1
continue
else:
break
Just edit the try…except and put it inside a loop