Ignore Duplicate Tweet Error Tweepy

python
tweepy

#1

I’m running a script that tweets out product names and links should that product be added to a site/modified within a minute of the current time when running the script. I want the script to loop endlessly in order to keep tweeting out new products. I have all of that working perfectly. The only problem is that when the script loops it often tries to send the same tweet again. After a minute since it was first tweeted it won’t tweet again as that is how I designed the script. But once I get the following error:

tweepy.error.TweepError: [{u'message': u'Status is a duplicate.', u'code': 187}]

the script stops and is unable to endlessly loop like intended. Basically I want to know if there is any way to ignore the error and keep the script running. The following is the end to my code:

def get_api(cfg):
    auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
    auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
    return tweepy.API(auth)
def main():
    cfg = {
    "consumer_key"        : "3i9xxxxxqmr",
    "consumer_secret"     : "yYkxmaxxxnBqn7T0ofVOWOxSnjLQ",
    "access_token"        : "814612329xxxxxxxxxxf4Wfqwbb9CyQ0b4",
    "access_token_secret" : "J5JQxxxxxxx6xNZiN"
    }
    api = get_api(cfg)
    status = api.update_status(status = new_product)
if __name__ == "__main__":
    main()

#2

Your script is breaking the endless loop because it hits an error/exception. Python isn’t my primary language but I think modifying this bit:

status = api.update_status(status = new_product)

to

try:
    status = api.update_status(status = new_product)
except tweepy.error.TweepError:
    pass

Like I said, Python isn’t my language of choice and I’m not 100% confident in that “except tweepy.error.TweepError” part but catching the exception/error seems to be what you need to do.