Since Spotify does not include genres for individual songs or albums, these will be obtained from Last.fm, a website that provides an API for retrieving album data based on the artist or song. To make the data more thorough, genres at song level were needed, as many albums can span a wide range of genres. Due to a limited knowledge in coding, this project was done using Claude and prompt engineering

To pull from the API, Last.fm requires a key, which is used as a parameter to request authorisation. This was relatively straight forward as it was also quick to obtain.

To do all of this in VS Code, the first step is to create a .venv, which stores the keys and the libraries that allow for web scraping. This will live within a new repository, which has a .gitignore containing all the available libraries. It is left as it originally comes, with the small addition of

Then, within the terminal, the virtual environment is activated.

.venv\Scripts\activate
pip install requests beautifulsoup4 pandas
pip freeze > requirements.txt

Commands are then run in the terminal to activate the venv, install the libraries, and save the list of installed packages.

Now with this done this is the repository created 

From here it is possible to start the api call and download the data. As a sanity check, the command shown is written in the terminal.

That's because if the venv hadn't been set up properly, it would just show an error message. This is what then allows the virtual environment to be connected to the scrape.py file.

In order to retrieve data from this specific API, it is necessary to provide a dataset structured with one song per artist per album. This data will then be joined within the bronze layer, as the Last.fm API holds the genre information (stored as ‘toptags’).

The next phase involves extracting the data from the website using Python, starting with a small batch of songs before gradually scaling up the requests. For this process to work, a file is required that represents the unique values of the songs, with different variations of song titles grouped into a single entry. This was achieved using Alteryx by building upon the existing flow that a colleague had previously created, incorporating new additions where the initial step was to clean up the fields and rename them. The main steps taken ensured that the data went from this;

To this, all lower case and the distinct values of the songs.

As mentioned previously, much of the heavy lifting in building this code was done by the free version of Claude. I made some tweaks, but overall it was very useful for this part.

The code is split into sections. It begins with the most common Python commands and libraries (installed beforehand) so the data can be manipulated and used.

The next part of the code reads the input file, which in this case is the aggregated data. The API then uses it to match genres from Last.fm. The API needs the artist and song name, as set out in its documentation, so that it can add the tags. This means the columns being matched must appear exactly as they do in the CSV.

Max and min are the settings for tags. Max is the maximum number of tags the code will return, and min is the number of tags a song must have before its genres are considered. In this case, a minimum of 50 tags was too high, because lesser-known songs may have fewer tags and would have been excluded. This was fixed later on.

Once the parameters have been defined, the API can be called. As with any API method, the URL must be stated and the number of retries set. The API documentation listed the different errors that could arise, and these were added to the code so that it can handle them if they occur.

  • 11 : Service Offline - This service is temporarily offline. Try again later.
  • 16 : The service is temporarily unavailable, please try again.
  • 29 : Rate Limit Exceeded - Your IP has made too many requests in a short period, exceeding our API guidelines
  • 6 : Invalid parameters - Your request is missing a required parameter
  • 7 : Invalid resource specified

Now that the parameters and constraints have been set up, the code can define the specific sections the API will pull information from. In this use case, it gets the genre information from the song's tags. If a song has no tags, it falls back to the tags of the artist associated with the song, so that the result doesn't come back as "no tags". There is also a fail-safe that retries the request if the internet connection is lost or the website is temporarily unavailable.

The section that pulls the artist tags follows the same structure, including the fail-safe that retries the request if the connection is lost or the site is unavailable.

Finally, the last section of the code adds a new genre column to the data, fills in the tags for each row, and writes the result to a CSV file.

This is functioning code that was built through prompt engineering and some tweaks. It shows how important it is to have a clear idea of what you expect and to sanity check the code. Something that was extremely useful early on was running a small test on 50 rows of data to check that the code worked.

Claude Prompt Summary