Twitter API Images

python
extended-tweet

#1

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 https://twitter.com/UMsuperfans 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.


#2

Can you share the code?


#3

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’, [ ] ))
superFanJSONTimeline.append(item._json)

print(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:
f.write(finalJson)


#4

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


#5

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


#6

Nevermind, I was able to fix it!


#7

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


#8

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.