Skip to content

Instantly share code, notes, and snippets.

@szaboat
Created October 11, 2009 16:22
Show Gist options
  • Save szaboat/207733 to your computer and use it in GitHub Desktop.
Save szaboat/207733 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
# if this fails, run:
# sudo gem install soundcloud-ruby-api-wrapper --no-ri --no-rdoc --source http://gems.github.com
gem 'soundcloud-ruby-api-wrapper'
require 'soundcloud'
require 'mechanize'
class Music
def initialize
@defaultsearch = ARGV[0] || 'fine-cut-bodies'
#config for audioplayer
@PLAYER = 'mplayer'
@sc_client = Soundcloud.register
@agent = WWW::Mechanize.new
@page = @agent.get 'http://spreadsheets.google.com/embeddedform?key=tpSZ6ITsuuTeIpCpmjvuxqA'
the_loop
puts 'bye'
end
def question
puts <<EOT
Enter search, like : #{@defaultsearch} or just hit enter to start player!
t search -> track search
u search -> user search
q -> exists! :)
EOT
search = STDIN.gets.strip
if search.empty?
@defaultsearch
else
search
end
end
# search query: if there is a leading search by character, use it:
# u for uer search
# t for track search
def get_tracks search
sa = search.split(' ')
if sa.size > 1
case sa.first
when 'u'
sa.shift
@sc_client.User.find(sa.join('-')).tracks
when 't'
sa.shift
@sc_client.Track.find(:all,sa.join('-'))
else
@sc_client.Track.find(:all,sa.join('-'))
end
else
@sc_client.Track.find(:all,search)
end
end
def the_loop
while search=question and search.downcase != 'q'
begin
tracks = get_tracks( search )
if tracks.length > 0
# submit to googlf
form = @page.forms.first
form.fields.first.value = search.split(' ').last
@page = @agent.submit(form,form.buttons.first)
puts "Track count: #{tracks.length}"
tracks.each do |track|
puts "#{track.title} @#{track.user.permalink}"
system "#{@PLAYER} #{track.stream_url}"
end
end
rescue Exception => e
puts e.message
end
end
end
end
Music.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment