I am using the Twitter API StreamingClient using the python module Tweepy. I am currently doing a short stream where I am collecting tweets and saving the entire ID and text from the tweet inside of a json object and writing it to a file.
My goal is to be able to collect the Twitter handle from each specific tweet and save it to a json file (preferably print it in the output terminal as well).
This is what the current code looks like:
KEY_FILE = './keys/bearer_token'
DURATION = 10
def on_data(json_data):
json_obj = json.loads(json_data.decode())
#print('Received tweet:', json_obj)
print(f'Tweet Screen Name: {json_obj.user.screen_name}')
with open('./collected_tweets/tweets.json', 'a') as out:
json.dump(json_obj, out)
bearer_token = open(KEY_FILE).read().strip()
streaming_client = tweepy.StreamingClient(bearer_token)
streaming_client.on_data = on_data
streaming_client.sample(threaded=True)
time.sleep(DURATION)
streaming_client.disconnect()
And I have no idea how to do this, the only thing I found is that someone did this:
json_obj.user.screen_name
However, this did not work at all, and I am completely stuck.
2
Answers
So a couple of things
Firstly, I’d recommend using
on_response
rather thanon_data
because StreamClient already defines aon_data
function to parse the json. (Then it will fireon_tweet
,on_response
,on_error
, etc)Secondly,
json_obj.user.screen_name
is part of API v1 I believe, which is why it doesn’t work.To get extra data using Twitter Apiv2, you’ll want to use Expansions and Fields (Tweepy Documentation, Twitter Documentation)
For your case, you’ll probably want to use
"username"
which is under theuser_fields
.Hope this helped.
also tweepy documentation definitely needs more examples for api v2