Search code examples
apitwittertweepy

How to get full json object from Twitter API v2 with Tweepy version 4.12


I am streaming Tweets using Tweepy 4.12 and Twitter API v2. I am only getting raw_data partial. How do I add rules to get full json object such as..

"country_code": "US",
"country": "United States",
"bounding_box": ...

Here is the output (real "id" are hidden here for privacy purpose).

b'{"data":{"edit_history_tweet_ids":[[some_id]],"id":[some_id],"text":""},"matching_rules":[{"id":[some_id],"tag":""}]}'

Below is my working code.

import tweepy

bearer_token = ""
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

client = Client(bearer_token = bearer_token, consumer_key = consumer_key, consumer_secret = consumer_secret, access_token = access_token, access_token_secret = access_token_secret)

class MyStreamListener(StreamingClient):
    def setup(self, in_queue):
        self.count = 0
        self.in_queue = in_queue
    
    def on_data(self, raw_data):
        print(raw_data)
    
    def on_connect(self):
        print('Connected')
        
    def on_tweet(self, tweet):
        if tweet.referenced_tweets == None: 
            print(tweet.text)
            time.sleep(0.2)

def process_and_save_to_db(in_queue, keywords):
    # saving to db

if __name__ == '__main__':
 
    myStreamListener = MyStreamListener(bearer_token = bearer_token)

    # Loading keywords
    keywords = ['elon musk', 'tesla', 'electric vehicle']
    
    myStreamListener.filter()

Solution

  • You can add tweet_fields from the following full list

    myStreamListener.filter(tweet_fields = ["attachments","author_id","context_annotations","conversation_id","created_at","edit_controls","edit_history_tweet_ids","entities","geo","id","in_reply_to_user_id","lang","non_public_metrics","organic_metrics","possibly_sensitive","promoted_metrics","public_metrics","referenced_tweets","reply_settings","source","text","withheld"])