How do I print out the full url with tweepy?

user_78361084 picture user_78361084 · Aug 11, 2012 · Viewed 7.4k times · Source

How do I print out the full urls in tweepy (rather than the t.co link)? The following code prints out "this is a test link http://t.co/93Hme7Jv 90210", even though twitter.com shows "this is a test link http://www.test.com/test 90210".

import tweepy, random

consumer_key="my_key"
consumer_secret="my_secret"
access_token="my_access"
access_token_secret="my_token"

rand = random.randint(1,999999999)

if __name__ == '__main__':
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    status = tweepy.API(auth)
    tweepy.API(auth).update_status('this is a test link http://www.test.com/test %s' % (rand))
    user = 'test_user'
    for status in tweepy.Cursor(status.user_timeline, id=user).items(20): 
        print status.text

Answer

Martijn Pieters picture Martijn Pieters · Aug 11, 2012

Not sure how that'd work with tweepy, but you want to set include_entities to True, and the Twitter API will include the full URLs of t.co URLs with responses.

Probably something like:

for status in tweepy.Cursor(status.user_timeline, id=user, include_entities=True).items(20): 
    for url in status.entities['urls']:
         print url['expanded_url']