Search code examples
pythontwittertwython

Is this a good method for filtering retweets from a search?


I'm very new to twython (and not usually much of a tweeter, either).

I'm trying to return a search with no retweeted tweets in it (only originals). I've looked around a fair bit but answers are hard to come by on this.

So I noticed that each retweeted status has an object 'retweeted_status' which doesn't exist for original tweets.

I'm therefore filtering using something like this:

a_retweet = twitter.show_status(id=702944259981365249)
an_original_tweet = twitter.show_status(id=702937516098375681)

try:
    if an_original_tweet['retweeted_status']:
        print("FAIL")
except KeyError:
    print("KeyError: Tweet is original")

Now - it seems to work nicely ("FAIL" doesn't print), but to a more experienced python programmer, is the code any good?


Solution

  • You can use the in operator to check whether a dictionary contains a key:

    if 'retweeted_status' in an_original_tweet:
        print('FAIL')
    else:
        print('Tweet is original')