Created
September 27, 2012 10:28
-
-
Save youpy/3793349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'cgi' | |
def show_image_on_iterm(url) | |
filename = '/tmp/image_on_iterm' | |
system('curl %s > %s 2>/dev/null' % [url, filename]) | |
command =<<CMD % filename | |
osascript -e " | |
tell application \\"iTerm\\" | |
tell the first terminal | |
tell the last session | |
set background image path to \\"%s\\" | |
end tell | |
end tell | |
end tell | |
" 2>/dev/null | |
CMD | |
system(command) | |
end | |
def embedly(url) | |
embedly_key = ENV['EMBEDLY_KEY'] | |
data = JSON.parse(open('http://api.embed.ly/1/oembed?key=%s&url=%s' % [embedly_key, CGI.escape(url)]).read) | |
#p data | |
data['type'] == 'photo' ? data['url'] : data['thumbnail_url'] | |
rescue => e | |
puts 'error: %s' % e.message | |
nil | |
end | |
Earthquake.init do | |
output_filter do |item| | |
if item['entities'] | |
if item['entities']['media'] | |
media = item['entities']['media'][0] | |
if media['type'] == 'photo' | |
show_image_on_iterm(media['media_url']) | |
end | |
elsif urls = item['entities']['urls'] | |
urls.each_with_index do |url, index| | |
image_url = embedly(url['expanded_url']) | |
if image_url | |
show_image_on_iterm(image_url) | |
break | |
end | |
end | |
end | |
end | |
true | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment