Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Twitter, Retrieving Full Tweet Text

I'm using the Ruby Twitter gem to retrieve the full text of a tweet.

I first tried this, and as you can see the text was truncated.

[5] pry(main)> t = client.status(782845350918971393)
=> #<Twitter::Tweet id=782845350918971393>
[6] pry(main)> t.text
=> "A #Gameofthrones fan? Our #earlybird Dublin starter will get you 
touring the GOT location in 2017
#traveldealls… (SHORTENED URL WAS HERE)"

Then I tried this:

[2] pry(main)> t = client.status(782845350918971393, tweet_mode: 'extended')
=> #<Twitter::Tweet id=782845350918971393>
[3] pry(main)> t.full_text
=>
[4] pry(main)> t.text
=>

Both the text and full text are empty when I use the tweet_mode: 'extended' option.

I also tried editing the bit of the gem that makes the request, the response was the same. perform_get_with_object("/1.1/statuses/show/#{extract_id(tweet)}.json?tweet_mode=extended", options, Twitter::Tweet)

Any help would be greatly appreciated.

like image 253
sbowenwilliams Avatar asked Nov 20 '17 00:11

sbowenwilliams


1 Answers

Here's a workaround I found helpful:

Below is the way I am handling this issue ATM. Seems to be working. I am using both Streaming (with Tweetstream) and REST APIs.

status = @client.status(1234567890, tweet_mode: "extended")

if status.truncated? && status.attrs[:extended_tweet]
  # Streaming API, and REST API default
  t = status.attrs[:extended_tweet][:full_text]
else
  # REST API with extended mode, or untruncated text in Streaming API
  t = status.attrs[:text] || status.attrs[:full_text]
end

From https://github.com/sferik/twitter/pull/848#issuecomment-329425006

like image 141
hunj Avatar answered Oct 30 '22 00:10

hunj