Created
November 13, 2009 01:12
-
-
Save ttscoff/233486 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby -rjcode -Ku | |
# TextMate Command "Songza Link" | |
# Scrapes the results of a Songza.fm search for | |
# the selected text, offers a popup menu and | |
# inserts a snippet of the selected link and title | |
# tab-stopped for easy removal of the title. | |
# Requires Rubygems and Hpricot | |
# Input: Selected Text or Word | |
# Output: Insert as Snippet | |
SUPPORT = ENV['TM_SUPPORT_PATH'] | |
DIALOG = ENV['DIALOG'] | |
require SUPPORT + '/lib/escape' | |
require SUPPORT + '/lib/exit_codes' | |
require SUPPORT + '/lib/ui' | |
require SUPPORT + '/lib/osx/plist' | |
require 'open-uri' | |
require 'rubygems' | |
require 'hpricot' | |
term = STDIN.read | |
url = "http://songza.fm/?q="+e_url(term) | |
begin | |
doc = Hpricot(open(url)) | |
rescue | |
puts "Error processing provided url" | |
exit | |
end | |
postsec = doc.search("//ul[@id='results']").first | |
songs = postsec.search("//a[@class='song']") | |
songlist = [] | |
songs.each {|song| | |
songlink = 'http://songza.fm'+song.attributes['href'] | |
songname = song.attributes['title'] | |
songlist << { 'link' => songlink, 'title' => songname } | |
} | |
plist = { 'menuItems' => songlist }.to_plist | |
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`) | |
TextMate.exit_discard unless res.has_key? 'selectedMenuItem' | |
url = res['selectedMenuItem']['link'] | |
title = res['selectedMenuItem']['title'] | |
shorturl = open("http://is.gd/api.php?longurl=#{url}").read | |
print %Q{#{shorturl}${1: "#{title}"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment