Skip to content

Instantly share code, notes, and snippets.

@BenWard
Created March 15, 2013 23:36
Show Gist options
  • Save BenWard/5174053 to your computer and use it in GitHub Desktop.
Save BenWard/5174053 to your computer and use it in GitHub Desktop.
Dirty little ruby script to get oEmbed from URLs, for use with automator actions in Mac OSX. Enables embedding Tweets and Flickr photos (and probably others) for selected URLs. Rather fragile.
require 'rubygems'
require "open-uri"
require 'nokogiri'
require 'json'
ARGV.each do |f|
doc = Nokogiri::HTML(open(f))
doc.css('link[type="application/json+oembed"]').each do |link|
oembed_uri = link['href']
oembed = open(oembed_uri).read
j = JSON.parse(oembed)
if j["type"] == "rich"
puts "#{j["html"]}\n\n"
elsif j["type"] == "photo"
puts "<p><a href=\"#{j["web_page"]}\" title=\"#{j["title"]}\"><img src=\"#{j["url"]}\" alt=\"#{j["title"]}\" width=\"#{j["width"]}\" height=\"#{j["height"]}\"></a></p>\n"
puts "<p>"
puts "By <a href=\"#{j["author_url"]}\">#{j["author_name"]}</a>." if j["author_name"]
puts "On <a href=\"#{j["provider_url"]}\">#{j["provider_name"]}</a>." if j["provider_name"]
puts "</p>\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment