0

For instance, with my current code, this tweet shows as:

Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https:// t.co/W0uFKU9jCr

I want to look like as it shown on Twitter's website, e.g.:

Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https://www.instagram.com/p/BSocl5Djf5v/

How can I do this exactly? I mean replacing Twitter's short urls by the urls of media, expanded urls, tweet quotes... I know it has to do with the 'entities' object in the json but I'm not sure how to handle that in my code

for status in new_tweets:
    if ('RT @' not in status.full_text):
        id = status.id
        text = status.full_text
iMe
  • 85
  • 8

1 Answers1

3

You are right that you need to use entities. You can get the expanded_url like so:

 for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
    if status.entities['urls']:
        for url in status.entities['urls']:
            links = url['expanded_url']
print(links)

You can make this print out the the status text and the expanded_url by concatenating them

for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
    if status.entities['urls']:
        for url in status.entities['urls']:
            links = url['expanded_url']
            print(status.text + links)

Not that this code will only print when the tweet has URLs, so I believe it will not print a tweet if no media link is shared.

Brian
  • 148
  • 10
  • Thanks. One note though... I found out after getting some errors is that the right way to check if a tweet contain urls (or media) is `if 'urls' in status.entities:` and not `if status.entities['urls']:` – iMe Apr 08 '17 at 20:52