# ---------------------------------------------------- # Simple MusicBrainz API wrapper # ---------------------------------------------------- # 1. Install the musicbrainzngs package (pip install musicbrainzngs) # 2. Run this script – it will show how to query the API # ---------------------------------------------------- import musicbrainzngs import time # ---------------------------------------------------- # Configuration # ---------------------------------------------------- musicbrainzngs.set_useragent( name="getDiscography", version="0.3", contact="akros@voxelixia.com" ) # ---------------------------------------------------- # Rate‑limit helper # ---------------------------------------------------- # MusicBrainz allows 1 request per second for anonymous users. # The wrapper below throttles requests to stay within the limit. class RateLimitedClient: def __init__(self, client, min_interval=1.0): self.client = client self.min_interval = min_interval self.last_call = 0.0 def _wait(self): elapsed = time.time() - self.last_call if elapsed < self.min_interval: time.sleep(self.min_interval - elapsed) def get_artist(self, artist_id): self._wait() return self.client.get_artist_by_id(artist_id) def search_artists(self, query, limit=10): self._wait() return self.client.search_artists(query, limit=limit) def get_release(self, release_id): self._wait() return self.client.get_release_by_id(release_id) def search_releases(self, query, limit=10): self._wait() return self.client.search_releases(query, limit=limit) # ---------------------------------------------------- # Wrapper instance # ---------------------------------------------------- client = RateLimitedClient(musicbrainzngs) # ---------------------------------------------------- # Example usage # ---------------------------------------------------- # if __name__ == "__main__": # # 1. Search for an artist by name # print("Searching for 'Radiohead'...") # result = client.search_artists("Radiohead") # for artist in result["artist-list"]: # print(f"- {artist['name']} (ID: {artist['id']})") # # # 2. Retrieve a specific artist by MBID (example ID) # artist_id = "c8f0b6e9-73e2-4d3a-9bb2-2a0d9d1a5f9a" # Radiohead # print(f"\nGetting details for artist ID {artist_id}...") # artist = client.get_artist(artist_id) # print(artist) # # # 3. Search for releases by the artist # print("\nSearching releases for 'Radiohead'...") # releases = client.search_releases("Radiohead") # for release in releases["release-list"]: # print(f"- {release['title']} (ID: {release['id']})")