Small project for exploring the Last.fm API: searching for an artist, pulling their top tracks, and looking up top artists by genre/tag.
- toptracks.py — CLI script. Prompts for an artist name, resolves it to the closest matching artist via
artist.search, then prints that artist's top 10 global tracks (playcount + unique listeners) viaartist.getTopTracks. - lastfmdemo.ipynb — Jupyter notebook version of the same workflow, extended with:
- a bar chart of top-track popularity (matplotlib)
- results loaded into
pandasDataFrames - a
tag.getTopArtistslookup for top artists by genre
- Python 3
requests- For the notebook:
pandas,matplotlib,numpy, and Jupyter
pip install requests pandas matplotlib numpy notebookBoth files call the Last.fm API with an API key. Get one from the Last.fm API account page, then set it as an environment variable and update the scripts to read it:
export LASTFM_API_KEY=your_key_hereNote: the API key is currently hardcoded in both
toptracks.pyandlastfmdemo.ipynb. Swap it foros.environ["LASTFM_API_KEY"](or similar) before sharing or committing this project publicly.
python toptracks.pyYou'll be prompted to enter an artist name, then the script prints their top 10 tracks with playcount and listener counts.
jupyter notebook lastfmdemo.ipynbRun the cells in order. Key functions defined in the notebook:
search_artist(query)— resolve a search query to an artist nameget_top_tracks(artist_name)— print top 10 tracks and plot popularity as a bar chartget_pandas(artist_name)— same as above, returned as apandas.DataFrameget_artists_by_top_genre(genre)— print top 10 artists for a given tag/genre