87

why text field value is truncated and how can i get complete value. As of now i am trying to get text field value like below

do {
       if let responseObject = try NSJSONSerialization.JSONObjectWithData(response, options: []) as? [String:AnyObject],
           arrayStatuses = responseObject["statuses"] as? [[String:AnyObject]] {
               let arrTweets:NSMutableArray = NSMutableArray()
               for status in arrayStatuses {
                   let text = status["text"]!
                   print(status["text"]!)
               }
       }
}

output is

RT @WarfareWW: #Russia/#India may hold launches of BrahMos cruise missiles from Su-30MKI vs sea/grnd trgts at the end of this year https://…

three dots at the end of line. i need to print complete text without truncation.

Twitter sample search result JSON Data

{
      "created_at": "Mon Aug 01 08:07:43 +0000 2016",
      "id": 760024194079916032,
      "id_str": "760024194079916032",
      "text": "RT @khalidasopore: #KEXIT #KASHEXIT #KashmirKillings #Inida #Pakistan Just trend it my dear Indians to save #Kashmir from Pak Goons https:/…",
      "truncated": false
}
Sheshnath
  • 3,293
  • 1
  • 32
  • 60
  • can you proof us that you are getting correct string as a response, show that request response in web or something – Lu_ Aug 02 '16 at 10:26
  • `UILabel`s will truncate text if they are a fixed height and width, you may also need to set `label.numberOfLines = 0` - Otherwise following @Lu_'s comment can you provide a json example. – Wez Aug 02 '16 at 10:29
  • it's not just a issue of UILabel even print() does truncate string – Sheshnath Aug 02 '16 at 10:32
  • provide us some proof of that, show json in web or something, it is not normal that saving to variable is turncating string – Lu_ Aug 02 '16 at 10:33
  • to be specific it's twitter hastag search api response, updated my question with json – Sheshnath Aug 02 '16 at 10:45

3 Answers3

194

The Twitter API has been changed recently, to support new rules regarding the 280 characters limit.

  1. To get the full text of the tweet, add parameter tweet_mode with value extended to your request parameters.
  2. Field text in the JSON response has been replaced by full_text

More info here: https://dev.twitter.com/overview/api/upcoming-changes-to-tweets

Ely
  • 8,259
  • 1
  • 54
  • 67
  • 1
    In a Java Spark Streaming application, where would I set the `tweet_mode` parameters? I have a `FilterQuery` which works with a listener. @Ely – sirdan Jun 11 '17 at 16:02
  • 43
    Not sure why Twitter doesn't include this in their official API documentation. Thank goodness for Stack Overflow. – Dalin Jun 19 '18 at 14:53
  • This helped, I am getting the full_text attribute back, but its still cutting off at around 210 characters for me. – TroySteven Apr 30 '19 at 14:21
  • 5
    It is ridiculous that the official documentation is outdated like this. Who updates their API docs with a "blog post"? – Marcos Pereira Jan 30 '20 at 03:41
  • Thanks a lot! I was trying to implement spam filter functionality for the Twitter bot and drilled down to the fact that I am not getting the entire text response from Twitter API. Implementing this has made everything work. – rishuverma Nov 08 '21 at 16:00
34

The status in this example is a retweet, and the text for retweets will be truncated to 140 characters even after including tweet_mode=extended. The full text of the original tweet is in the retweeted_status field of the JSON response. Here's what you want:

let text = status["retweeted_status"]["full_text"].

Keep in mind that you should still include tweet_mode=extended in your request.

pterry26
  • 1,230
  • 1
  • 10
  • 20
  • Retweets are still truncated today, however, if you include `expansions=referenced_tweets.id` in your API call it can give you the id of the original tweet to lookup directly, which will give you the full tweet text under `text`. – tayler6000 Aug 21 '22 at 02:21
0

This worked for me!

 tweets = api.search_tweets(q=search_term, tweet_mode='extended', count=tweet_amount)

for tweet in tweets:
    # check if the tweet is a retweet
    if tweet.full_text.startswith('RT @'):
    # if the tweet is a retweet, use the retweeted_status attribute
        full_text = tweet.retweeted_status.full_text
    else:
        full_text = tweet.full_text
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 04 '23 at 00:42