Skip to content

Instantly share code, notes, and snippets.

@cgallagher
Created October 2, 2009 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgallagher/199823 to your computer and use it in GitHub Desktop.
Save cgallagher/199823 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'scrobbler'
require 'net/http'
require 'uri'
require 'hpricot'
require 'cgi'
require 'dbi'
require 'fastercsv'
API_KEY = "2c7cfb186a5cc451e6bc2e76e5d70108"
SECRET_KEY = "0da91e4af813f82984dce67cdd17e131"
@artist_name = "Avril Lavigne"
@track_artist_name = "coldplay"
@track_name = "viva la vida"
@artist = Scrobbler::Artist.new(@artist_name)
def connect_to_live_mysql()
puts "\nConnecting to MySQL..."
return DBI.connect('DBI:Mysql:muzudbv2', 'root', '')
end
def findTopTracks
puts 'Coldplay Top Tracks'
puts "=" * 10
@artist.top_tracks.each { |t| puts "(#{t.reach}) #{t.name}" }
end
def findSimilarArtists
puts 'Coldplay Similar Artists'
puts "=" * 15
@artist.similar.first { |a| puts "(#{a.match}%) #{a.name}" }
end
def artistPrimaryGenre
newdbh = connect_to_live_mysql()
p "getting channels"
networkIds = Array.new
networkNames = Array.new
getNetworkList = newdbh.prepare("SELECT n.NetworkIdentity, p.Name FROM networks n INNER JOIN profiles p on p.NetworkIdentity = n.NetworkIdentity WHERE statusid = 1 AND networktypeid not in(6, 11) LIMIT 5")
getNetworkList.execute()
while row = getNetworkList.fetch()
networkIds << row["NetworkIdentity"]
networkNames << row["Name"]
end
names = Array.new
primary_genres = Array.new
for name in networkNames
if name != nil && name != ""
@artist = Scrobbler::Artist.new(name)
if @artist != nil
if @artist.top_tags != nil && @artist.top_tags.length > 0
primary_genre = @artist.top_tags.first.name
p "#{name} has a primary genre of #{primary_genre}"
names << name
primary_genres << primary_genre
sleep 1.0
end
end
end
end
row_counter = 0
#create a csv file for the networks that were successfully tagged
FasterCSV.open("captured_data.csv", "w") do |csv|
for name in names
csv << [name, primary_genres[row_counter]]
row_counter = row_counter + 1
end
end
end
def fetch(uri_str, limit=10)
fail 'http redirect too deep' if limit.zero?
#puts 'trying #{uri_str}'
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess
response
when Net::HTTPRedirection
fetch(response['location'], limit-1)
else
response.error!
end
return response.body
end
def getArtistInfo
response = fetch("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=#{CGI.escape(@artist_name)}&api_key=" + API_KEY)
doc = Hpricot::XML(response)
artist_description = doc.search("//summary")
p "ARTIST DESCRIPTION"
p "="*50
#doc_string = html2text(artist_description[0])
doc_string = artist_description.first
converted_doc = doc_string.inspect
doc_text = textConv(converted_doc.to_s)
#get text between '\ \'
doc_text_front_removed = doc_text.gsub("{elem ", "")
#split string on second brace and take the first part
doc_text_end_removed = doc_text_front_removed.split('}')[0]
#remove the slashes
doc_first_slash_removed = doc_text_end_removed.tr('\"', '')
p doc_first_slash_removed
#finally remove the square brackets containing numbers
# p final_string
end
def getTrackInfo
puts "Fetching track information"
p "="*50
response = fetch("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=#{API_KEY}&artist=#{CGI.escape(@track_artist_name)}&track=#{CGI.escape(@track_name)}")
doc = Hpricot::XML(response)
track_description = doc.search("//summary")
track_description_string = track_description.first
converted_doc = track_description_string.inspect
doc_text = textConv(converted_doc.to_s)
#get text between '\ \'
doc_text_front_removed = doc_text.gsub("{elem ", "")
#split string on second brace and take the first part
doc_text_end_removed = doc_text_front_removed.split('}')[0]
#remove the slashes
doc_first_slash_removed = doc_text_end_removed.tr('\"\"', '')
p doc_first_slash_removed
end
def similarTracks
puts "Fetching similar tracks"
p "="*50
response = fetch("http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=#{CGI.escape(@track_artist_name)}&track=#{CGI.escape("clocks")}&api_key=#{API_KEY}")
doc = Hpricot::XML(response)
track_names = Array.new
#two concurrent hashes of artist names and track names - will have the same index.
artist_names = Array.new
track_counter = 0
doc.search('//track').each do |track|
track_name = textConv(track.search('//name').first.inspect.to_s).gsub("{elem ", "").split('}')[0].tr('\"\"', '')
artist_name = textConv(track.search('//artist//name').inspect.to_s).gsub("{elem ", "").split('}')[0].tr('\"\"', '').gsub("# ", "")
track_names << track_name
artist_names << artist_name
track_counter = track_counter + 1
end
p artist_names
p track_names
end
def textConv(html)
text = html.gsub(/(&nbsp;|\n|\s)+/im, ' ').squeeze(' ').strip.gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i, '\4')
links = []
linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i
while linkregex.match(text)
links << $~[3]
text.sub!(linkregex, "[#{links.size}]")
end
text = CGI.unescapeHTML(text.gsub(/<(script|style)[^>]*>.*<\/\1>/im, '').gsub(/<!--.*-->/m, '').gsub(/<hr(| [^>]*)>/i, "___\n").gsub(/<li(| [^>]*)>/i, "\n* ").gsub(/<blockquote(| [^>]*)>/i, '> ').gsub(/<(br)(| [^>]*)>/i, "\n").gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n").gsub(/<[^>]*>/, '') ).lstrip.gsub(/\n[ ]+/, "\n") + "\n"
for i in (0...links.size).to_a
text = text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless links[i].nil?
end
links = nil
text
end
def getEventsForArea
location = "ireland"
response = fetch("http://ws.audioscrobbler.com/2.0/?method=geo.getevents&api_key=#{API_KEY}&location=#{location}")
p response
end
def main()
# srand(1)
# newdbh = connect_to_live_mysql()
#get artists primary genres
# artistPrimaryGenre()
#scrobbler calls
# findTopTracks()
# findSimilarArtists()
#artistPrimaryGenre()
getEventsForArea
#homemade calls
#getArtistInfo()
# getTrackInfo()
# similarTracks()
#newdbh.disconnect
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment