Skip to content

Instantly share code, notes, and snippets.

@jaspertandy
Created November 22, 2011 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jaspertandy/1385833 to your computer and use it in GitHub Desktop.
Save jaspertandy/1385833 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
Earthquake.init do
output_filter do |item|
next unless item['text']
text = item["text"]
text.scan( /http:\/\/t\.co\/[a-zA-Z0-9\-]+/ ).each do |url|
uri = URI.parse url
Net::HTTP::start( uri.host , uri.port ) do |connection|
connection.request_get uri.path do |response|
text.gsub! url , response['Location']
end
end
end
text
end
end
@beret
Copy link

beret commented Jan 8, 2012

Thanks for writing this, it's helps a lot since the t.co URLs are opaque. Unfortunately, I've encountered a bug with it. Occasionally this plugin throws `gsub!': can't convert nil into String (TypeError) It's related to retweets, since I can reproduce the crash by retweeting certain tweets. (one, two)

If the error happens during a :recent command, it stops printing the recent tweets and puts the error message into a notification (libnotify in my ruby install). If it errors during streaming API mode, then the error is dumped to the shell and earthquake exits.

Examples:

[$jn] exiledonline: MUST READ: I.C.E. paramilitary attack, jail time for 29-Year-Old
American woman who ran link-sharing site " http://wp.me/phI6d-c95 06 Jan 10:59 -
exiledonline
⚡ .earthquake/plugin/expand_tco.rb:10:in `gsub!': can't convert nil into String (TypeError)
[Same error as below.]

The above tweet. The tweet that followed, was a retweet of this, by @exiledonline.

[$ng] exiledonline: RT @DylanRatigan: .#Occupy movement pushing for global amendment that
separates business & state. http://t.co/yjQYjd8z"HUGE! @DeepakChopra @ariannahuff (retweet
of $nf) - 07 Jan 16:11 - web
⚡ .earthquake/plugin/expand_tco.rb:10:in `gsub!': can't convert nil into String (TypeError)
[Full error output: http://pastie.org/3145868]

The above tweet was a retweet of this by @exiledonline. The tweet that followed, was a retweet of this, by @exiledonline. (The url wasn't expanded on the non-crashing @dylanratigan tweet, but that's not an issue)

If I knew my way around ruby I'd try to fix it myself, but I can't even see a pattern in the source tweets that would cause it. It works without a hitch 99% of the time, so I'll use it regardless.

@jaspertandy
Copy link
Author

Ah, I actually came across a similar issue using this previously. The problem actually stems from the core code of earthquake, in that the tweet body of a retweet is stored in a different variable to a regular tweet, which is quite annoying! I ended up (probably foolishly) making a modification to the core of earthquake to fix the issue for myself, but the modification I've made to the existing gist should skirt the issue (though you may still see t.co in retweets).

@bric3
Copy link

bric3 commented Feb 9, 2012

Hi,

Thx for this plugin, it's working great. However I encounter the same erroneous behavior with the latest version of your script.
You said you modified the core, that would be interesting to know what you modified.

Here's the stacktrace that appear in the console for reference:

earthquake: [ERROR] can't convert nil into String
/Users/brice/.earthquake/plugin/expand_tco.rb:11:in `gsub!'
/Users/brice/.earthquake/plugin/expand_tco.rb:11:in `block (5 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1322:in `block (2 levels) in transport_request'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:2671:in `reading_body'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1321:in `block in transport_request'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1293:in `request'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1195:in `request_get'
/Users/brice/.earthquake/plugin/expand_tco.rb:10:in `block (4 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:745:in `start'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/1.9.1/net/http.rb:557:in `start'
/Users/brice/.earthquake/plugin/expand_tco.rb:9:in `block (3 levels) in <top (required)>'
/Users/brice/.earthquake/plugin/expand_tco.rb:7:in `each'
/Users/brice/.earthquake/plugin/expand_tco.rb:7:in `block (2 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/gems/1.9.1/gems/earthquake-0.8.4/lib/earthquake/output.rb:36:in `call'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/gems/1.9.1/gems/earthquake-0.8.4/lib/earthquake/output.rb:36:in `block (2 levels) in puts_items'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/gems/1.9.1/gems/earthquake-0.8.4/lib/earthquake/output.rb:36:in `each'
/usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/gems/1.9.1/gems/earthquake-0.8.4/lib/earthquake/output.rb:36:in `any?'

@adamtomecek
Copy link

https://gist.github.com/3187216

Fixed problem I was getting with empty String with some links.

@valentinritz
Copy link

@adamtomecek Thank's a lot for this!

This solved the issue :)

@tribela
Copy link

tribela commented Mar 20, 2014

https://gist.github.com/Kjwon15/9657129

I fixed it doesn't applied for item object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment