Created
August 31, 2008 10:27
-
-
Save sunny/8178 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
#!/usr/bin/env ruby | |
# Searches for music titles to listen to, using the seeqpod.com API. | |
# | |
# You will need a seeqpod UID for that. To get one, just create a free account | |
# on seeqpod.com and look in your settings. | |
# | |
# $ ruby dlmusic.rb Radiohead Just | |
# Radiohead/Just (You Do It To Yourself) | |
# ⤷ http://infonistacrat.hometownproject.org/audio/2122008/Radiohead%20-%20Just.mp3 | |
# Radiohead/Just | |
# ⤷ http://mi-ban.net/Mp3/Diabeetus/Radiohead/(1995)%20The%20Bends/07%20Radiohead%20-%20Just.mp3 | |
# ... | |
UID = 'YOUR_SEEQPOD.COM_UID' | |
%w(open-uri cgi rubygems hpricot).each { |lib| require lib } | |
class MusicTitle | |
attr_accessor :artist, :title, :uri | |
def initialize(artist, title, uri) | |
@artist, @title, @uri = artist, title, uri.gsub(/ /, '%20') | |
end | |
def to_s | |
"#{@artist}/#{@title}" | |
end | |
# Returns array of MusicItems matching query | |
def self.search(query) | |
query = CGI.escape(query) | |
xml = open("http://www.seeqpod.com/api/v0.2/#{UID}/music/search/#{query}").read | |
doc = Hpricot.XML(xml) | |
(doc/:track).map do |track| | |
title = (track/:title).inner_html | |
creator = (track/:creator).inner_html | |
location = (track/:location).inner_html | |
self.new(creator, title, location) | |
end | |
end | |
end | |
if $0 == __FILE__ | |
abort "Usage: #{$0} search" if ARGV.empty? | |
results = MusicTitle.search(ARGV.join(' ')) | |
abort "No results." if results.empty? | |
results.each do |item| | |
puts "\e[1m#{item}\e[0m" # bold | |
puts " ⤷ #{item.uri}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment