the other jake

Export Last.fm Scrobbles to MySQL Database

Recently I was pretty bummed to see that even API access will be gated behind the pro subscription. This makes sense from one perspective to increase the value of the subscription, but because I have always relied on open access to the API as a means of getting my data out of Last.fm. This no longer is possible, and I don't really feel the desire to develop apps on the platform now that it's no longer as open. All this is to say I was extra motivated to get my scrobbles out of the site and into a local MariaDB table that I could play with at my leisure.

The first step is to export your scrobbles. The first export was massive in my case since my history goes back to 2007. The upshot is after loading the first huge export, subsequent ones will be much smaller. Since the API access change has gone into effect, some Last.fm export apps in the wild won't work... because the developer isn't Pro. This isn't always the case-- there is a pretty strong overlap between fans of Last.fm and people who want to build apps on its API.

One such export site is https://lastfm.ghan.nl/export/.

After you've got your scrobbles, and assuming you already have a MySQL or MariaDB database up and running, the following will create a scrobbles table with the correct schema. Based on the schema, we need at least track and artist, but all Musicbrainz *_mbids and album can be NULL.

Create the table:

CREATE TABLE `scrobbles` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `uts` INT UNSIGNED NOT NULL,
  `utc_time` DATETIME NOT NULL,
  `artist` VARCHAR(255) NOT NULL,
  `artist_mbid` CHAR(36) NULL,
  `album` VARCHAR(255) NULL,
  `album_mbid` CHAR(36) NULL,
  `track` VARCHAR(255) NOT NULL,
  `track_mbid` CHAR(36) NULL,
  UNIQUE KEY `uq_scrobble` (`uts`, `artist`, `track`),
  KEY `idx_artist` (`artist`),
  KEY `idx_album` (`album`),
  KEY `idx_utc_time` (`utc_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Insert the export:

LOAD DATA LOCAL INFILE 'scrobbles.csv'
INTO TABLE `scrobbles`
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(@uts, @utc, artist, @artist_mbid, @album, @album_mbid, track, @track_mbid)
SET `uts` = @uts,
    `utc_time` = STR_TO_DATE(@utc, '%d %b %Y, %H:%i'),
    `artist_mbid` = NULLIF(@artist_mbid, ''),
    `album` = NULLIF(@album, ''),
    `album_mbid` = NULLIF(@album_mbid, ''),
    `track_mbid` = NULLIF(@track_mbid, '');

Examples:

Number of times I've listened to Radiohead albums. Due to tagging inconsistencies, some of these could be merged. Cleaning the data remains a future exercise.

MariaDB [db]> select album, count(album)
              from scrobbles
              where artist = 'Radiohead' and album is not null
              group by album;
+--------------------------------+--------------+
| album                          | count(album) |
+--------------------------------+--------------+
| A Moon Shaped Pool             |           80 |
| Amnesiac                       |           83 |
| Amnesiac (Collector's Edition) |           11 |
| B-Sides                        |            1 |
| Blockbuster Movie Hits         |            1 |
| Drill                          |            1 |
| Hail to the Thief              |           20 |
| Hail To The Thief (Retail)     |            1 |
| In Rainbows                    |          120 |
| In Rainbows (Disk 2)           |            1 |
| Karma Police                   |            1 |
| Kid A                          |          350 |
| KID A MNESIA                   |           15 |
| My Iron Lung                   |           19 |
| OK Computer                    |           92 |
| OK Computer OKNOTOK 1997 2017  |            1 |
| Pablo Honey                    |           15 |
| Street Spirit (Fade Out)       |           25 |
| The Bends                      |            4 |
| The Best Of                    |            2 |
| The Daily Mail / Staircase     |           36 |
| The Golden Unplugged Album     |            1 |
| The King of Limbs              |           13 |
| These Are My Twisted Words     |            5 |
| TKOL RMX 1234567               |           13 |
| TKOL RMX 8                     |            2 |
| TKOL RMX1234567                |           17 |
+--------------------------------+--------------+
27 rows in set (0.005 sec)

My top 20 albums based on listening history.

MariaDB [db]> select artist, album, count(album) plays
              from scrobbles
              group by artist, album
              order by plays desc
              limit 20;
+--------------------+------------------------------------------------------------+-------+
| artist             | album                                                      | plays |
+--------------------+------------------------------------------------------------+-------+
| Boards of Canada   | Music Has the Right to Children                            |   408 |
| Gorillaz           | Demon Days                                                 |   382 |
| Boards of Canada   | Geogaddi                                                   |   370 |
| Radiohead          | Kid A                                                      |   350 |
| Madvillain         | Madvillainy                                                |   345 |
| Gorillaz           | Gorillaz                                                   |   316 |
| Deltron 3030       | Deltron 3030                                               |   286 |
| Theodore Shapiro   | Severance: Season 1 (Apple TV+ Original Series Soundtrack) |   253 |
| Flying Lotus       | Until the Quiet Comes                                      |   241 |
| Boards of Canada   | Twoism                                                     |   225 |
| Blak Forest        | treehouse beats (95-98)                                    |   210 |
| J Dilla            | Donuts                                                     |   202 |
| BLKSMIITH          | 「DIGITAL TWIN」                                           |   199 |
| DJ Krush           | Kakusei                                                    |   198 |
| Neutral Milk Hotel | In the Aeroplane Over the Sea                              |   197 |
| BLKSMIITH          | A MESSAGE FROM MY DIGITAL TWIN                             |   195 |
| MF DOOM            | Operation: Doomsday (Complete)                             |   195 |
| DJ Shadow          | Endtroducing.....                                          |   193 |
| Jonwayne           | Bowser                                                     |   193 |
| Man or Astro-Man?  | Experiment Zero                                            |   188 |
+--------------------+------------------------------------------------------------+-------+
20 rows in set (0.711 sec)

And the top 20 tracks individually.

MariaDB [db]> select artist, album, track, count(track) plays
              from scrobbles
              group by track
              order by plays desc
              limit 20;
+----------------------+----------------------------------------------+-----------------------------+-------+
| artist               | album                                        | track                       | plays |
+----------------------+----------------------------------------------+-----------------------------+-------+
| Gorillaz             | Demon Days                                   | Intro                       |   157 |
| Boards of Canada     | Music Has the Right to Children              | Roygbiv                     |    92 |
| DJ Shadow            | Endtroducing.....                            | Midnight in a Perfect World |    85 |
| bôa                  | Twilight                                     | Duvet                       |    80 |
| Sofie Royer          | Cult Survivor                                | Baby                        |    79 |
| Trashiii             | Bluescreen                                   | Bluescreen                  |    78 |
| Cibo Matto           | Super Relax                                  | Spoon                       |    77 |
| eightiesheadachetape | for a while                                  | the bowling alley           |    74 |
| XXYYXX               | XXYYXX                                       | About You                   |    73 |
| The Chantays         | Pipeline                                     | Pipeline                    |    73 |
| Four Tet             | New Energy                                   | Two Thousand And Seventeen  |    73 |
| Pizza Hotline        | LEVEL SELECT                                 | LEVEL SELECT                |    69 |
| Beach House          | 7                                            | Lemon Glow                  |    65 |
| Beach House          | 7                                            | Black Car                   |    63 |
| Gorillaz             | Gorillaz                                     | Tomorrow Comes Today        |    62 |
| Boards of Canada     | Twoism                                       | Sixtyniner                  |    61 |
| strxwberrymilk       | The Escape from Blue Marble (Deluxe Edition) | Fragments Of Love           |    60 |
| Car Seat Headrest    | Teens of Style                               | Maud Gone                   |    59 |
| Aphex Twin           | ...I Care Because You Do                     | Alberto Balsalm             |    59 |
| Boards of Canada     | Music Has the Right to Children              | Olson                       |    59 |
+----------------------+----------------------------------------------+-----------------------------+-------+
20 rows in set (0.955 sec)

sql, music