The Python Oracle

Tweepy Truncated Status

This video explains
Tweepy Truncated Status

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind

--

Chapters
00:00 Question
01:07 Accepted answer (Score 19)
03:08 Answer 2 (Score 7)
03:46 Answer 3 (Score 4)
04:19 Answer 4 (Score 3)
04:47 Thank you

--

Full question
https://stackoverflow.com/questions/4205...

Accepted answer links:
[https://tco/kALZ2ki9Vc]: https://tco/kALZ2ki9Vc

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #tweepy

#avk47



ACCEPTED ANSWER

Score 19


Is the 'retweeted' and 'truncated' attribute referring to the same thing (i.e., status text beyond 140 characters)? If not, what is the difference?

No, tweets can be truncated not only after a retweet (forward), but also after a reply or a mention (see my example below which is not a retweet). If it is the case, 'truncated' will be set to 'True' (and 'retweeted' will be True or False). It is also possible to have 'retweeted' equals to True while 'truncated' being equals to False, if the tweet is not truncated because its size is well below 140 characters.

It suggested that there is a retweeted attribute in the _json dictionary which will be true if that is the case and the full status text will be under status->retweeted_status->text.

This is true only if the tweet is a genuine retweet. Actually the retweeted_status could itself be truncated if it is coming from another truncated tweet. The best way is to use the tweet_mode='extended' parameter in Tweepy to retrieve the full text (unfortunately not documented in the Tweepy doc). For instance:

(not extended)

print api.get_status('862328512405004288')._json['text']

@tousuncotefoot @equipedefrance @CreditAgricole @AntoGriezmann @KMbappe @layvinkurzawa @UmtitiSam J'ai jamais vue d… https://tco/kALZ2ki9Vc

(extended)

print api.get_status('862328512405004288', tweet_mode='extended')._json['full_text']

@tousuncotefoot @equipedefrance @CreditAgricole @AntoGriezmann @KMbappe @layvinkurzawa @UmtitiSam J'ai jamais vue de match de foot et cela ferait un beau cadeau pour mon copain !! šŸ™šŸ»šŸ™šŸ»šŸ™šŸ»šŸ˜šŸ˜




ANSWER 2

Score 7


Regarding question 3)

Below is a way to access the extended status text from a Tweepy stream listener, given that the tweet is more than 140 characters (in this case the status object will have an extended_tweet dictionary). If the tweet is not more than 140 characters, then just get the status text as usual:

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:
            text = status.extended_tweet["full_text"]
        except AttributeError:
            text = status.text



ANSWER 3

Score 4


I am also troubled due to this problem.

When the tweet is retweeted, there are two "full_text" tags in .json file. The real full_text can be extracted by calling tag of 'retweet_status'. The solution is as below:

for tweet in tweepy.Cursor(api.search, q = "social", tweet_mode='extended',
                           wait_on_rate_limit = True, wait_on_rate_limit_notify = True, include_entities=True).items(10):
    try:
        print tweet.retweet.im_self._json['retweeted_status']['full_text']
    except:
        print tweet.retweet.im_self._json['full_text']




ANSWER 4

Score 4


The simplest way worked for me is simply get the status and search for the retweeted full text.

status = api.get_status(id, tweet_mode="extended")
try:
    print(status.retweeted_status.full_text)
except AttributeError:  # Not a Retweet
    print(status.full_text)

this works even in search

tweets=[]
for tweet in api.search(q=keyword, lang="en", count=count, tweet_mode='extended'):
    try:
       tweets.append(tweet.retweeted_status.full_text)
    except AttributeError:  # Not a Retweet
       tweets.append(tweet.full_text)