Skip to content

Instantly share code, notes, and snippets.

@sirupsen
Created September 15, 2013 16:01
Show Gist options
  • Save sirupsen/6572004 to your computer and use it in GitHub Desktop.
Save sirupsen/6572004 to your computer and use it in GitHub Desktop.
How hipster is your taste in music?
require 'lastfm'
LASTFM_API_KEY = ""
LASTFM_SECRET = ""
SIRUPSEN = "f6b7ffe9b0c87fe8a341eb7a474f4574"
def get_token
lastfm = Lastfm.new(LASTFM_API_KEY, LASTFM_SECRET)
token = lastfm.auth.get_token
end
# token = get_token
# puts token
# puts "http://www.last.fm/api/auth/?api_key=#{LASTFM_API_KEY}&token=#{token}"
score = {
(0..10_000) => 1,
(10_001..100_000) => 0.5,
(100_101..500_000) => 0.25,
(500_000..1_000_000) => 0.125
}
lastfm = Lastfm.new(LASTFM_API_KEY, LASTFM_SECRET)
lastfm.session = lastfm.auth.get_session(token: SIRUPSEN)['key']
username = "sirupsen"
artists = lastfm.user.get_top_artists(username, "6month", 10)
puts "Most listened to in the last 6 months for #{username}: "
puts artists.map { |a| a["name"] }.join(", ")
puts ""
def score(listeners)
case listeners
when 0..10_000
1
when 10_001..100_000
0.5
when 100_001..500_000
0.25
when 500_000..1_000_000
0.10
else
0
end
end
def score2(listeners)
1/(listeners.to_f)
end
def score3(listeners)
listeners.to_f/4_000_000
end
hipster_score = 1
artists.each do |artist|
artist_info = lastfm.artist.get_info(artist["name"])
listeners = artist_info["stats"]["listeners"].to_i
artist_score = score3(listeners)
puts "Hipster score for #{artist_info["name"]} with #{listeners} listeners: #{artist_score}"
hipster_score += artist_score
end
puts hipster_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment