Search code examples
jsonpython-3.xpython-requeststweepy

How do I find the full text of a tweet with json data (in tweepy)?


In tweepy I am trying to stream data and I can only get a short amount of text displayed to my screen. I spent 30 minutes trying to find an answer, but none worked. I am just trying to get the full tweet's text, thank you

import tweepy
import json
from time import sleep
import requests

# Specify the account credentials in the following variables:
consumer_key = 'xxxxxxxxxx'
consumer_secret = 'xxxxxxxxxx'
access_token = 'xxxxxxxx-xxxxxxxxx'
access_token_secret = 'xxxxxxxxxx'

# This listener will print out all Tweets it receives
class PrintListener(tweepy.StreamListener):
    def on_data(self, data):
        # Decode the JSON data
        tweet = json.loads(data)

        id = tweet["id"]
        user = tweet['user']['screen_name']
        user_message = tweet['text']

    def on_error(self, status):
        print(status)

if __name__ == '__main__':
    listener = PrintListener()

    # Show system message
    print('searching stuff ==>')

    # Authenticate
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    # Connect the stream to our listener
    stream = tweepy.Stream(auth, listener)
    # stream.filter(track=['test'])
    stream.filter(track=['test'])

Solution

  • If it's not a Retweet, you can use the extended_tweet field of the JSON data representing the Tweet and the full_text sub-field of that field to get the full text of the Tweet. Otherwise, if it's a Retweet, you'll have to do so with the JSON data representing the Retweeted Tweet, which would be the retweeted_status field of the Retweet JSON data. Note, the extended_tweet field will only exist for extended Tweets.

    I would recommend using the Status object that's passed to on_status and the corresponding attributes of that object instead, since you don't seem to be doing anything that requires the raw JSON data.

    For further information about extended Tweets, see Tweepy's documentation on the subject and Twitter's Tweet updates documentation.