A Last.fm Tag Extractor is a simple program or script designed to query the Last.fm API and fetch the genre, mood, or style tags that users have assigned to specific artists, albums, or tracks. Building one is an excellent project for learning how to interact with Web APIs, handle JSON data, and manage rate limits.
Here is a simple guide to building your own Last.fm Tag Extractor using Python. Step 1: Get a Last.fm API Key
Before writing code, you need permission from Last.fm to fetch their data. Go to the Last.fm API Account Creation Page.
Fill out the application form (you can leave the “Callback URL” blank). Copy your unique API Key and keep it secure. Step 2: Set Up Your Project Environment
Create a folder for your project and install the requests library to handle HTTP communication.
mkdir lastfm-extractor cd lastfm-extractor pip install requests Use code with caution. Step 3: Write the Extractor Script
Create a file named extractor.py and insert the following code. This script uses the public track.getTopTags API endpoint to pull user-generated music genres or descriptors.
import requests # 1. Configuration Constants API_KEY = “YOUR_LASTFM_API_KEY” # Replace with your actual Last.fm API key USER_AGENT = “TagExtractorBot/1.0 ([email protected])” BASE_URL = “http://ws.audioscrobbler.com/2.0/” def get_track_tags(artist, track, limit=5): “”“Fetches the top crowdsourced tags for a specific song.”“” # 2. Build the query payload payload = { “method”: “track.getTopTags”, “artist”: artist, “track”: track, “api_key”: API_KEY, “format”: “json” } # Last.fm requires an identifiable User-Agent in the headers headers = {“User-Agent”: USER_AGENT} try: # 3. Make the API Request response = requests.get(BASE_URL, headers=headers, params=payload) response.raise_for_status() # Raise error for bad responses (404, 500, etc.) data = response.json() # 4. Parse the JSON Data tag_list = data.get(“toptags”, {}).get(“tag”, []) # If no tags are found if not tag_list: print(f”No tags found for ‘{track}’ by {artist}.“) return [] # Extract and print top tag names up to the specified limit extracted_tags = [tag[“name”] for tag in tag_list[:limit]] return extracted_tags except requests.exceptions.RequestException as e: print(f”An error occurred: {e}“) return [] # — Example Usage — if name == “main”: search_artist = “Pink Floyd” search_track = “Comfortably Numb” print(f”Extracting tags for: {search_track} - {search_artist}…“) tags = get_track_tags(search_artist, search_track, limit=5) if tags: print(” Top Last.fm Tags:“) for index, tag in enumerate(tags, 1): print(f”{index}. {tag}“) Use code with caution. Step 4: Run and Test Run your script from the terminal: python extractor.py Use code with caution. Expected Output:
Extracting tags for: Comfortably Numb - Pink Floyd… Top Last.fm Tags: 1. classic rock 2. progressive rock 3. psychedelic rock 4. rock 5. 70s Use code with caution. Key Technical Guidelines
When scaling this simple project into a larger data analytics tool, always remember Last.fm’s API rules:
Leave a Reply