I am trying to use the code I made below to fetch the media url of a video in a tweet when someone replies to that tweet with my @handle:
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
class StdOutListener(StreamListener):
def on_data(self, data):
clean_data = json.loads(data)
tweetId = clean_data['id']
tweet_name = clean_data['user']['screen_name']
auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")
auth.set_access_token("access_token", "access_token_secret")
api = tweepy.API(auth)
mediaupload_id = clean_data['in_reply_to_status_id']
origtweet_media = api.get_status(mediaupload_id)
print()
print(origtweet_media)
native_media = origtweet_media['extended_entities']['media'][0]['video_info']
tweet_media = native_media['variants'][0]['url']
print(tweet_media)
def setUpAuth():
auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")
auth.set_access_token("access_token", "access_token_secret")
api = tweepy.API(auth)
return api, auth
def followStream():
api, auth = setUpAuth()
listener = StdOutListener()
stream = Stream(auth, listener)
stream.filter(track=["@YOUR_TWITTER_HANDLE"], is_async=True)
if __name__ == "__main__":
followStream()
Everything seems to be set up well, but when I run the code, and someone replies to a tweet with a video in it (the person replying mentioning my @handle), I get this error:
Exception has occurred: TypeError
'Status' object is not subscriptable
native_media = origtweet_media['extended_entities']['media'][0]['video_info']
Why is 'Status' non-subscriptable? And is there a way I can fix this issue, so it successfully grabs the video url (tweet_media
)?
Someone please help me!
SOLVED!
After closely looking at the print
ed origtweet_media
, I noticed that the tweet info was inside _json=()
. So I added ._json
onto the end of origtweet_media = api.get_status(mediaupload_id)
as that just isolates the json section, allowing me to find the video url for tweet_media
. Here's what the new updated line looks like:
origtweet_media = api.get_status(mediaupload_id)._json
and then it worked successfully!