Search code examples
python-3.xtwittertweepy

Error when trying to get full_text for a Tweet using Tweepy and Python


I am trying to use Tweepy and streaming to track Tweets in real time. I am using the following which works fine:

import tweepy
import configparser
import sys

#read configs                                                                                              
config = configparser.ConfigParser()
config.read('config.ini')

api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']

access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']


class StreamCollector(tweepy.Stream):

    def on_status(self, status):
        if not hasattr(status, 'retweeted_status') and status.in_reply_to_screen_name == None and status.i\
s_quote_status == False:
            if status.author.followers_count > 100000:
                print('Twitter Handle: @'+status.author.screen_name)
                print('Followers:',status.author.followers_count)
                print('Tweet:',status.text)
                print('\n')
        #print(status.user.screen_name.encode('UTF-8'))                                                    


stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])

However, I want to produce the untruncated Tweet. I tried substituting status.text for status.full_text but I got the error:

AttributeError: 'Status' object has no attribute 'full_text'

My version of Tweepy is 4.5.0 and Python is 3.9.9.


Solution

  • The tweepy.API has a compatibility mode and extended mode. The extended mode should allow you to get the full text of the Tweet.

    ref: Extended Tweets

    Here is the code with the extended mode call.

    import sys
    import tweepy
    import configparser
    
    #read configs                                                                                              
    config = configparser.ConfigParser()
    config.read('config.ini')
    
    api_key = config['twitter']['api_key']
    api_key_secret = config['twitter']['api_key_secret']
    
    access_token = config['twitter']['access_token']
    access_token_secret = config['twitter']['access_token_secret']
    
    
    class StreamCollector(tweepy.Stream):
    
        def on_status(self, status):
            if not hasattr(status, 'retweeted_status'):
                if status.in_reply_to_screen_name is None and status.is_quote_status is False:
                    if status.author.followers_count > 100000:
                        print(f'Twitter Handle: @{status.author.screen_name}')
                        print(f'Followers: {status.author.followers_count}')
    
                        if 'extended_tweet' in status._json:
                            full_text = status._json['extended_tweet']['full_text']
                            print(f'Tweet: {full_text}')
                        elif 'extended_tweet' not in status._json:
                            print(f'Tweet: {status.text}')
    
                        print('\n')
                                                              
    
    stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
    stream.filter(track=["table"])