Ruby is the interpreted scripting language for quick and easy object-oriented programming. It is simple, straight-forward, extensible, dynamic, and portable. Similar in scope to Perl and Python, it has high-level data types, automatic memory management, dynamic typing, a module system, exceptions, and a rich standard library.
To help address performance issues, virtual machines for Ruby have been developed, such as JRuby and YARV, which is being integrated into the Ruby distribution.
If you’re investigating Ruby, and building webapps, you’ll want to take a close look at Rails.
You can download Ruby
Visit RubyForge to see some related projects building on or extending the capabilities of Ruby.
Read documentation
And get a 20-minute tutorial
require 'digest/md5'
require 'fileutils'
require 'md5'
require 'net/http'
require 'thread'
require 'uri'
Config={}
Config[:user]="yourlastfmusername"
Config[:password]="yourpassword"
Submission_settings={}
def do_handshake
timestamp = Time.now.gmtime.to_i.to_s
md5 = Digest::MD5.hexdigest(Config[:password])
md5 = Digest::MD5.hexdigest(md5 + timestamp)
# replace c=tst and v=1.0 to a vaild id of a last.fm client.
query = "/?hs=true&p=1.2&c=tst&v=1.0&u=#{Config[:user]}&t=#{timestamp}&a=#{md5}"
begin
Net::HTTP.start("post.audioscrobbler.com", 80) do |http|
resp = http.get(query).body.strip
case resp
when /^OK\n([0-9a-z]+)\n(.+)\n(.+)/
puts("handshake succeeded")
Submission_settings[:session_id] = $1
Submission_settings[:now_playing_url] = URI.parse($2)
Submission_settings[:submission_url] = URI.parse($3)
when /^BANNED$/
puts("banned")
when /^BADTIME$/
puts("bad time, go fix your clock")
when /^FAILED (.+)$/
puts("handshake failed - #{resp}")
when /^BADAUTH$/
puts("handshake failed - bad username/password")
else
puts("bad response in handshake - #{resp}")
end
end
rescue SocketError => err
puts("socket error: #{err}")
rescue SystemCallError => err
puts("system call error: #{err}")
rescue IOError => err
puts("io error: #{err}")
rescue Timeout::Error
puts("timeout during handshake")
end
end
def now_playing(a)
[:band,:song,:album,:lenght,:nr,:mb].each do |i|
a[i]="" unless a[i]
a[i]=a[i].to_s.gsub(/ /,'_')
end
Net::HTTP.post_form(Submission_settings[:now_playing_url],{"s"=>Submission_settings[:session_id],"a"=>a[:band], "t"=>a[:song], "b"=>a[:album],"l"=>a[:lenght], "n"=>a[:nr], "m"=>a[:mb] })
end
def submit(a)
[:band,:song,:album,:lenght,:rating,:nr,:mb].each do |i|
a[i]="" unless a[i]
a[i]=a[i].to_s.gsub(/ /,'_')
end
Net::HTTP.post_form(Submission_settings[:now_playing_url],{"s"=>Submission_settings[:session_id],"a[0]"=>a[:band], "t[0]"=>a[:song],"i[0]" =>Time.now.to_i,"o[0]" => 'P', "r[0]"=>a[:rating], "b[0]"=>a[:album],"l[0]"=>a[:lenght], "n[0]"=>a[:nr], "m[0]"=>a[:mb] })
end