Skip to content

Instantly share code, notes, and snippets.

@romanblanco
Last active February 27, 2017 23:15
Show Gist options
  • Save romanblanco/828a504b68c95104033a22e0dd3694ea to your computer and use it in GitHub Desktop.
Save romanblanco/828a504b68c95104033a22e0dd3694ea to your computer and use it in GitHub Desktop.
# http://chartkick.com/
require 'lastfm-client'
#require 'ap'
#require 'pry'
#require 'json'
LastFM.api_key = ''
LastFM.client_name = 'stats'
last_fm_user = 'roman_blanco'
recent_tracks = []
recent_tracks_pages_count = LastFM::User.get_recent_tracks(
:user => last_fm_user
)["recenttracks"]["@attr"]["totalPages"].to_i
print "pages to load: "
recent_tracks_pages_count.times { print '.' }
print "\npages loaded: "
recent_tracks_pages_count.times do |page|
next if page.zero?
next unless page == 1 # FIXME debug mode
tracks_on_page = LastFM::User.get_recent_tracks(
:user => last_fm_user,
:page => page
)
recent_tracks.push(*tracks_on_page["recenttracks"]["track"])
print '.'
end
puts ''
recent_tracks.each_with_index do |track, index|
track['info'] = LastFM::Track.get_info(
{
:artist => track["artist"]["#text"],
:track => track["name"]
}
)
print "loading tracks details: #{index + 1} / #{recent_tracks.count}\r"
$stdout.flush
end
#File.open('stats_2', 'w+') { |file| file.write(recent_tracks) }
#binding.pry
# tags by date
recent_tracks.map do |track|
{
:date => track["date"]["uts"],
:tags => track["info"]["track"]["toptags"]["tag"]
}
end
# most listened artist
recent_tracks.map { |track|
track["artist"]["#text"]
}.inject(Hash.new(0)) { |total, e|
total[e] += 1 ; total
}.sort_by(&:last).reverse.to_h
# dates of scrobbled tracks
recent_tracks.map { |track| DateTime.parse(track["date"]["#text"]) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment