Update: I will soon be publishing an online tool that can be used to generate out a Spotify playlist based on all of your historical scrobbles. Watch this space.

I have been scrobbling my music plays to last.fm since 2007 - I was 17 at the time. I used Audioscrobbler before this, I'm sure that I must also have another last.fm profile that predates my current profile, but I can't find it. Even so, last.fm has a decade's worth of my musical taste stored on their servers and I want to find out some of fucking terrible music that I used to listen to, and also hopefully be reminded of a few hidden gems that I may have forgotten.

If you are also a long-standing member of last.fm and want to get a big ole list of the tracks you've listened to (in a fairly machine readable format), then please read on. If you're non-technical, I can provide the necessary steps to run this script, and if you're a programmer, feel free to adopt it and make it better.

I've written my solution in Ruby as this is what I feel most comfortable with. It hasn't got many bells and whistles on and is fairly barebones for the meantime. If you want to run this script yourself, you will need to register for last.fm API access by going to http://www.last.fm/api/account/create and grabbing your API credentials from there (API key and API secret), and inserting them into the relevant places marked with XXX in the code below.

Prerequisities

Setting up

You will need to use the command line interface for this - this guide is intended for Mac OS X / Linux users only. For Windows, these instructions will be slightly different. Open up your terminal application (hint: it's probably called "Terminal").

Create a new directory for the project:

mkdir lastfm-importer

Change context to the directory you just created:

cd lastfm-importer

Create a new Ruby Gemfile:

touch Gemfile

Populate the Gemfile with the below Ruby gems:

source 'https://rubygems.org'

gem 'lastfm', '~> 1.27', '>= 1.27.1'

Now, back on the command line within the context of the directory you've just created, run:

bundle install

Next up, create the script file main.rb:

touch main.rb

Populate the main.rb file with the below code (ensuring that you replace api_key and api_secret with your credentials):

require 'lastfm'
require 'date'

def get_auth_url(token, api_key)
    "http://www.last.fm/api/auth/?api_key=#{api_key}&token=#{token}"
end

def get_formatted_track_name(obj)
    begin
        "#{obj["artist"]["content"]} - #{obj["name"]}\n"    
    rescue Exception => e
        return nil
    end
end

api_key = 'XXX'
api_secret = 'XXX'
lastfm = Lastfm.new(api_key, api_secret)
token = lastfm.auth.get_token

puts "Open #{get_auth_url(token, api_key)} and paste auth code back here"
puts "Press any key once done. \n"

go_ahead = gets

puts "Please enter your last.fm username:"

username = gets.chomp

puts "Your username is #{username}"

user_info = lastfm.user.get_info(:user => username)

registered_date = DateTime.strptime(user_info["registered"]["unixtime"],'%s')
play_count = user_info["playcount"].to_i

puts "#{username} has #{play_count} plays. Whoa."

week_beginning = registered_date

while week_beginning <= Date.today
    week_ending = week_beginning.next_day(7)
    tracks_for_week = lastfm.user.get_weekly_track_chart(:user => username, :from => week_beginning.to_time.to_i, :to => week_ending.to_time.to_i)

    if tracks_for_week
        tracks_for_week.each do |t|
            track = get_formatted_track_name(t)
            open("output/#{username}.txt", "a") { |f| f << track } 
        end
    end
    
    week_beginning = week_ending.next_day(1)
end

puts "Finished"

Running the script / Usage

You may run the script by running the command below:

ruby main.rb

The script will ask for your input on the command line. First off, the terminal will print out an auth url that you will need to copy and paste into your browser and authorize this application with the last.fm API. Once this authorization is complete, you can switch context back to the command line and press any key.

Next up, the command line tool will prompt you for the last.fm username you want to get the track list for. The script will take a while to process, especially if you have over 200,000 plays like myself. The track listing will be written to a txt file in the output/ directory with your last.fm username as the identifier; e.g. adaam2.txt

The dump file will be in chronological order, with your earliest played tracks at the start of the file, etc.

Any questions or need some support, then just let me know in the comments.

A