Overview
TheMusicBrainzClient class provides methods to search and retrieve music metadata from the MusicBrainz database. It includes automatic retry logic, rate limiting, and intelligent title matching.
All methods are class methods (
@classmethod) and can be called directly without instantiating the class.Configuration
The client is pre-configured with:- User Agent: “TagQt/1.0”
- Rate Limiting: 1 request per second (respects MusicBrainz rate limits)
- Auto-retry: Up to 3 attempts on network errors with exponential backoff
Methods
normalize_title()
Normalizes a title string for fuzzy matching by removing accents, punctuation, and extra formatting.Title string to normalize
Normalized title with:
- Accents removed (NFKD normalization)
- Parentheses and brackets content removed
- Punctuation and special characters removed
- Lowercase
- Extra whitespace collapsed
- Unicode NFKD normalization
- Remove trailing parentheses:
(...)and brackets:[...] - Remove quotes/apostrophes:
',',` - Remove all non-alphanumeric characters except spaces
- Convert to lowercase and collapse whitespace
titles_match()
Fuzzy title matching using normalization. Returns True if titles are considered equivalent.First title to compare
Second title to compare
True if titles match exactly or one contains the other after normalization
- Exact match after normalization
- Substring match (one normalized title contains the other)
search_release()
Searches for album releases matching the given artist and album name. Returns the best matching release with metadata.Artist name to search for
Album/release name to search for
Optional track title for future use (currently not used in search)
Dictionary containing release metadata, or None if no results found or error occurred.Response fields:
id(str): MusicBrainz release ID (UUID)title(str): Album titleartist(str): Primary artist nameartist_id(str): MusicBrainz artist IDdate(str): Full release date (YYYY-MM-DD)year(str): Release year (first 4 characters of date)country(str): Release country codestatus(str): Release status (e.g., “Official”)genres(list[str]): Up to 3 top genres (from release, release group, or artist)release_group_id(str): MusicBrainz release group IDdisc_count(int): Number of discs (default: 1)track_disc(None): Track disc number (only set bylookup_release())track_position(None): Track position (only set bylookup_release())track_count(None): Total tracks (only set bylookup_release())
- Searches up to 10 releases
- Prioritizes “Official” releases over other statuses
- Returns the first official release, or first result if no official releases found
- Attempts to fetch genres from release tags, then release group, then artist
lookup_release()
Fetches detailed release information by MusicBrainz release ID, including track positions and genre data.MusicBrainz release ID (UUID)
Optional track title to find disc/track position within the release
Dictionary containing detailed release information, or None on error.Response fields:
genres(list[str]): Up to 3 top genres (prioritizes recording-level genres)disc_count(int): Total number of discs/media in the releaserelease_group_id(str): MusicBrainz release group IDtrack_disc(int | None): Disc number containing the track (iftrack_titleprovided and found)track_position(int | None): Track position on disc (if found)track_count(int | None): Total tracks on the disc (if found)
- Uses fuzzy matching via
titles_match()to find the track - Searches all discs sequentially until a match is found
- Prioritizes recording-level genres over release-level genres when track is found
lookup_release_group()
Fetches genre tags for a MusicBrainz release group.MusicBrainz release group ID (UUID)
List of up to 3 top genre names, sorted by tag count (most popular first). Returns empty list on error or if no tags found.
lookup_artist()
Fetches genre tags for a MusicBrainz artist.MusicBrainz artist ID (UUID)
List of up to 3 top genre names, sorted by tag count. Returns empty list on error or if no tags found.
Complete Example
Error Handling
The client includes robust error handling:- Network Errors: Automatic retry with exponential backoff (2^attempt seconds)
- API Errors: Caught and logged, returns
Noneor empty list - Rate Limiting: Automatically enforced at 1 request/second
Best Practices
- Check return values: Always verify results are not
Nonebefore accessing fields - Use track matching: Pass
track_titletolookup_release()for accurate track positions - Genre fallback: The client automatically tries release → release group → artist for genres
- Respect rate limits: The client handles this automatically, but avoid rapid successive calls
API Dependencies
Requires themusicbrainzngs Python package:
