Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

rtzui

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Audioscrobbler

"now_playing" won't have any effect to the statistics, for that use "submit". Use "submit" AFTER playing the song.

   1  
   2  require 'digest/md5'
   3  require 'fileutils'
   4  require 'md5'
   5  require 'net/http'
   6  require 'thread'
   7  require 'uri'
   8  
   9  
  10  
  11  Config={}
  12  Config[:user]="yourlastfmusername"
  13  Config[:password]="yourpassword"
  14  
  15  Submission_settings={}
  16  
  17  
  18  def do_handshake
  19  		timestamp = Time.now.gmtime.to_i.to_s
  20  
  21  		md5 = Digest::MD5.hexdigest(Config[:password])
  22  		md5 = Digest::MD5.hexdigest(md5 + timestamp)
  23  
  24  # replace c=tst and v=1.0 to a vaild id of a last.fm client.
  25  
  26  		query = "/?hs=true&p=1.2&c=tst&v=1.0&u=#{Config[:user]}&t=#{timestamp}&a=#{md5}"
  27  
  28  		begin
  29  			Net::HTTP.start("post.audioscrobbler.com", 80) do |http|
  30  				resp = http.get(query).body.strip
  31  
  32  				case resp
  33  				when /^OK\n([0-9a-z]+)\n(.+)\n(.+)/
  34  					puts("handshake succeeded")
  35  					Submission_settings[:session_id] = $1
  36  					Submission_settings[:now_playing_url] = URI.parse($2)
  37  					Submission_settings[:submission_url] = URI.parse($3)
  38  				when /^BANNED$/
  39  					puts("banned")
  40  				when /^BADTIME$/
  41  					puts("bad time, go fix your clock")
  42  				when /^FAILED (.+)$/
  43  					puts("handshake failed - #{resp}")
  44  				when /^BADAUTH$/
  45  					puts("handshake failed - bad username/password")
  46  				else
  47  					puts("bad response in handshake - #{resp}")
  48  				end
  49  			end
  50  		rescue SocketError => err
  51  			puts("socket error: #{err}")
  52  		rescue SystemCallError => err
  53  			puts("system call error: #{err}")
  54  		rescue IOError => err
  55  			puts("io error: #{err}")
  56  		rescue Timeout::Error
  57  			puts("timeout during handshake")
  58  		end
  59  end
  60  
  61  def now_playing(a)
  62  	[:band,:song,:album,:lenght,:nr,:mb].each do |i|
  63  		a[i]="" unless a[i]
  64  		a[i]=a[i].to_s.gsub(/ /,'_')
  65  	end
  66  
  67  	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]  })
  68  end
  69  
  70  
  71  def submit(a)
  72  	[:band,:song,:album,:lenght,:rating,:nr,:mb].each do |i|
  73  		a[i]="" unless a[i]
  74  		a[i]=a[i].to_s.gsub(/ /,'_')
  75  	end
  76  
  77  	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]  })
  78  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS