Overview
TheCoverArtManager class provides methods to search for and download album cover art from multiple sources including MusicBrainz Cover Art Archive and iTunes. It includes automatic image processing, resizing, and retry logic.
Constructor
requests.Session with automatic retry configuration:
- Max retries: 3 attempts
- Backoff factor: 1 second (exponential)
- Retry on: 500, 502, 503, 504 status codes
Methods
download_and_process_cover()
Downloads cover art from a URL and automatically processes it (resize to 500x500, convert to JPEG).Direct URL to the cover art image
Processed JPEG image data (500x500, quality 90), or None on error
- Download image with 15 second timeout
- Convert to RGB color mode
- Resize to exactly 500x500 using LANCZOS resampling
- Save as JPEG with 90% quality
- Return raw bytes
- Up to 3 download attempts
- Exponential backoff (2^attempt seconds)
- Handles network errors and request exceptions
search_cover()
Searches for cover art using multiple sources. Tries MusicBrainz first, then falls back to iTunes.Artist name
Album/release name
URL to the cover art image, or None if not found
- MusicBrainz Cover Art Archive (via
search_cover_musicbrainz()) - iTunes API (via
search_cover_itunes()) if MusicBrainz fails
search_cover_musicbrainz()
Searches MusicBrainz for cover art and returns Cover Art Archive URL.Artist name
Album/release name
Cover Art Archive URL (format:
https://coverartarchive.org/release-group/{id}/front), or None if not found- Searches MusicBrainz for release groups matching artist + album
- Extracts release group ID from first result
- Constructs Cover Art Archive URL
- Returns URL (does not verify image exists)
search_cover_itunes()
Searches iTunes API for cover art. Returns high-resolution URL (1000x1000).Artist name
Album name
iTunes artwork URL (1000x1000 resolution), or None if not found
- Searches iTunes with combined “artist album” query
- Filters for music albums
- Returns first result’s artwork URL
- Automatically upgrades URL from 100x100 to 1000x1000
search_cover_candidates()
Returns multiple cover art candidates from both sources for user selection.Artist name
Album name
List of cover art candidates from multiple sourcesEach candidate dictionary contains:
album(str): Album name from sourceartist(str): Artist name from sourceurl(str): Direct image URLsource(str): Either “iTunes” or “MusicBrainz”size(str): Image dimensions (e.g., “1000x1000” or “Unknown”)
- Searches iTunes for up to 5 results
- Searches MusicBrainz for 1 result
- Returns combined list (may be empty if no results)
- Does not download or verify images
Complete Example
Multiple Candidates Example
Source Comparison
MusicBrainz Cover Art Archive
Advantages:- High quality official releases
- Community-curated
- Multiple image types (front, back, booklet)
- Free and open
- May not have covers for all releases
- Requires exact artist/album match
iTunes API
Advantages:- Large catalog
- High resolution (1000x1000)
- Fast response
- Good for popular music
- Limited to iTunes catalog
- May return remastered/different versions
- Watermarked in some regions
Error Handling
All methods handle errors gracefully and returnNone or empty list:
- Network timeouts
- Invalid image data
- 404 Not Found responses
- API rate limiting
- Image processing failures
Best Practices
- Try multiple sources: Use
search_cover()which tries both sources - Verify downloads: Always check if returned data is not
None - Use candidates for UI: Show multiple options to users with
search_cover_candidates() - Cache results: Store downloaded covers to avoid repeated API calls
- Handle missing metadata: Provide fallback artist/album values if metadata is incomplete
API Configuration
MusicBrainz
- Base URL: https://musicbrainz.org/ws/2/
- User Agent: “tagqt/1.0 ( [email protected] )”
- Timeout: 10 seconds
iTunes
- Base URL: https://itunes.apple.com/search
- Media: music
- Entity: album
- Timeout: 10 seconds
