How I display my top 1000 artists

Filed under: music | Python

Note: I have since updated the below code as of 4th May 2023.

I decided to create a page to display my top 1000 artists. Here’s how I did it.

1. Get the data

To get this data, you need the API and an account. With my credentials, I wrote a script that scraped the data and created a JSON file out of it:

import requests
import json
import datetime

payload = {
  'api_key': API_KEY,
  'format': 'json',
  'limit': 1000,

r = requests.get('', params=payload)

res = r.json()

for key in range(0,len(res['topartists']['artist'])):
    # Change playCount from str to int
    res['topartists']['artist'][key]['playcount'] = int(res['topartists']['artist'][key]['playcount'])

    res['last updated'] ='%d %B %Y')

with open('../data/lastfm.json', "w+") as f:
    json.dump(res, f, indent=4)

This created a lastfm.json file. I also included a key for the date when I update the file so I can display that on the page. Now there’s time to use the Fetch API.

2. Fetch the data

I use the Fetch API for my wiki pages to pull data from my other WordPress blogs via the REST API. In this case, I’m pulling from a local JSON file so I start by importing the file:

import lastfm from '../data/lastfm.json';

Then I use .map() to map the rank, artist, and playcount to a table.

<p>Last updated: {lastfm['last updated']}</p>
  { => (
      <td><a href={item.url}>{}</a></td>

A couple of things to note: gets all the artist data and item.playcount.toLocaleString() converts the playcount and adds a thousands comma seperator.

And that’s it. If you have any questions, email me or tweet me.

I tried Vicuna and it made me laugh Probably Autistic