Twitter API Images



So i’m trying to interface twitter with my app by using collecting tweets from a timeline with the api using json. I’m trying to get images from but it’s not working. Most of the tweets on the page have images but only 6 actually showed they had images using the API and looking at the “media_url” in entities. How am i supposed to get all of the images? I am using tweepy with python.


Can you share the code?


Here is the code I am using. The tweepy media call only shows that six of the photos have images even though when I go directly on twitter most of the tweets do have photos.

import tweepy
import json
from ftplib import FTP
import io

#allows access to the account
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
newAPI = tweepy.API(auth)

#gets 50 of the superfan timeline tweets
superFanTimeline = newAPI.user_timeline(id=“UMsuperfans”, count=50,include_rts=True)

#creates a blank array that the superfan json will go into
superFanJSONTimeline = [ ]

media = [ ]
for item in superFanTimeline:
#should get all media items which would be photos in the tweet but doesn’t return as many as it should
media.append(item.entities.get(‘media’, [ ] ))

#pretty prints the json call and adds it into a file
finalJson = json.dumps(superFanJSONTimeline,indent=4)
with open(‘finalJson.json’,‘w’) as f:


include_rts=True includes retweets too, so those don’t have media entities in the Retweet - you need to first select the retweeted_status and then look inside that for media


I did and it still doesn’t show any media inside.


Nevermind, I was able to fix it!


What was the solution? the retweet thing or something else like extended tweets or something?


I had to use extended tweets in my api call. So the line of code that i needed to have was
api.user_timeline(id=“UMsuperfan”, count=50, include_rts=True, tweet_mode=“extended”).
But thanks for your help I really appreciate it! I wouldn’t have known that it worked if i didn’t look inside the retweeted_status for media like you suggested.

closed #9

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.