All pastes #265877 Raw Edit

Something

public text v1 · immutable
·published 2006-12-03 18:53 UTC
rendered paste body
#!/usr/bin/env ruby
#Lorenz Röhrl 2006
#hurra@hurra.homelinux.org

require "net/http"

puts "fetching from last.fm"
track=`dcop amarok player title`
artist=`dcop amarok player artist`

artist=artist[0...artist.length-1]
track=track[0...track.length-1]
puts artist
puts track

query="select tags.url from tags,artist where tags.title='#{track}' AND tags.artist=artist.id AND artist.name='#{artist}';"
#puts query


message=`dcop amarok collection query "select tags.url from tags,artist where tags.title='#{track}' AND tags.artist=artist.id AND artist.name='#{artist}';"`
puts message
message=message[0..message.length-2]


#ar2= `dcop amarok collection query "select artist.name, tags.title from tags, artist  where tags.artist=artist.id and url='#{message}';"`
#artist=ar2.split("\n")[0]
#track=ar2.split("\n")[1]
	


track.gsub!( " ", "+" )
artist.gsub!( " ", "+" )
#http://www.last.fm/music/Belle+and+Sebastian/_/Expectations/+tags

proxy_host = nil
proxy_port = nil
if ( ENV['http_proxy'] && proxy_uri = URI.parse( ENV['http_proxy'] ) )
	proxy_host = proxy_uri.host
	proxy_port = proxy_uri.port
end

h = Net::HTTP.new( "www.last.fm", 80, proxy_host, proxy_port )
response = h.get( "/music/#{artist}/_/#{track}/+tags")
	

erg=response.body()
puts "extracting infos"
erg.gsub!( "\n", "" ) # No need for LF, just complicates our RegExps
erg.gsub!( "\r", "" ) # No need for CR, just complicates our RegExps


erg=/<div style="line-height:28px;" id="tagcloud" class="tagBox">(.+)\?<\/a>/.match(erg)

if erg!=nil
	erg=erg[1]



	ergar=Array.new()
	
	erg.split("<span").each do |arg|
		arg.gsub!( " ", "" ) # No need for LF, just complicates our RegExps
		if arg!=""
			#puts arg + "_"
			t= /<ahref="\/tag\/(.+)"rel="tag">/.match(arg)[1].gsub("+"," ")
			ergar.push(t)
			#puts t
		end
	end
	
	puts "found tags:"
	puts ergar.join(" - ")
	
	
	r=`dcop amarok collection query "select deviceid,uniqueid from statistics where url='#{message}';"`
	deviceid=r.split("\n")[0]
	uniqueid=r.split("\n")[1]
	
	
	puts "adding data to database tables"
	ergar.each do |e|
		test=`dcop amarok collection query "select count(name) from labels where name='#{e}';"`
		test=test[0..test.length-1]
		
		if test.to_i==0
			#puts "#{e} not in"
			`dcop amarok collection query "INSERT INTO labels (name,type) VALUES('#{e}',1);"`
		else
			#puts "#{e} already in labels"
		end
		
		lid=`dcop amarok collection query "select id from labels where name='#{e}'"`
		
		
		
		test=`dcop amarok collection query "select count(tags_labels.url) from tags_labels,labels where tags_labels.deviceid='#{deviceid}' and tags_labels.url='#{message}' and tags_labels.uniqueid='#{uniqueid}' and tags_labels.labelid=#{lid}"`
		test=test[0..test.length-1]
		
		if test.to_i==0
			#puts message + "_" + artist + "_" + track + "_" + lid.to_s + "_" + deviceid.to_s + "_" + uniqueid.to_s
			#puts "#{e} not in, adding .."
			`dcop amarok collection query "INSERT INTO tags_labels (deviceid,url,uniqueid,labelid) VALUES(#{deviceid},'#{message}','#{uniqueid}',#{lid});"`
			
		else
			#puts message + "_" + artist + "_" + track + "_" + lid.to_s + "_" + deviceid.to_s + "_" + uniqueid.to_s
			#puts "already in"
		end
		#puts ""
		
	end

else
	puts "no tags found"
end