当有人用我的@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) …Run Code Online (Sandbox Code Playgroud)