Skip to content

Instantly share code, notes, and snippets.

@aliekens
Created December 2, 2021 10:09
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 aliekens/f5dcb2f512aa958ec85253bce6bdf2a0 to your computer and use it in GitHub Desktop.
Save aliekens/f5dcb2f512aa958ec85253bce6bdf2a0 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'json'
apikey = "YOURLASTFMAPIKEY" # your last.fm API key created from https://www.last.fm/api/account/create
apiroot = "http://ws.audioscrobbler.com/2.0/"
username = "aliekens" # your last.fm username
period = "12month" # overall | 7day | 1month | 3month | 6month | 12month
limit = "100" # how many results do you want?
url = apiroot + "?method=user.gettopartists&user=#{username}&limit=#{limit}&period=#{period}&api_key=#{apikey}&format=json"
puts url
uri = URI(url)
response = Net::HTTP.get(uri)
json = JSON.parse(response)
json["topartists"]["artist"].each do |artist|
puts "#{artist["name"]}\t#{artist["playcount"]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment