musicbrainzngs

repository·master·Indexed 43 days ago

https://github.com/alastair/python-musicbrainzngs

A Python library providing bindings for the MusicBrainz NGS webservice (/ws/2) and the Cover Art Archive. It allows developers to fetch music metadata as dictionaries, search for entities, browse linked data, and submit updates to the MusicBrainz database.

Tokens
3.7K
Snippets
14
Records
21
Agent score
53%

What's inside musicbrainzngs

  1. Configure logging for musicbrainzngs

    master

    The library uses Python's standard logging module. All logs are sent to a logger named musicbrainzngs. You can enable debug output for your entire application or specifically restrict musicbrainzngs to INFO level to reduce noise.

    import logging
    logging.basicConfig(level=logging.DEBUG)
    # optionally restrict musicbrainzngs output to INFO messages
    logging.getLogger("musicbrainzngs").setLevel(logging.INFO)
  2. Authenticate for data submission

    master

    If you need to submit data to MusicBrainz, you must authenticate using your MusicBrainz website credentials. Use the musicbrainzngs.auth method to set your username and password. Once called, these credentials are saved and automatically applied to all subsequent functions that require authentication.

    Errors:

    • If you call an authentication-required method without authenticating, a musicbrainzngs.UsageError is raised.
    • If the credentials provided are incorrect (server returns 401), a musicbrainzngs.AuthenticationError is raised.
    import musicbrainzngs
    
    musicbrainzngs.auth("your_username", "your_password")
  3. Identify your application with set_useragent

    master

    To comply with MusicBrainz webservice access rules, you must identify your application by setting a unique useragent header. Use musicbrainzngs.set_useragent to automatically format a useragent string based on your application's name, version, and contact information.

    Note: If you attempt to make a request without setting a useragent first, the library will raise a musicbrainzngs.UsageError.

    import musicbrainzngs
    
    musicbrainzngs.set_useragent(
        appname="My Awesome App",
        appversion="1.0.0",
        contact="my@email.com"
    )
  4. Initialize musicbrainzngs for use

    master

    To use the library, import musicbrainzngs. Before making requests, you must identify your application using set_useragent to comply with MusicBrainz webservice rate limiting rules. If you intend to submit data (write operations) rather than just querying, you must also authenticate using auth.

    import musicbrainzngs
    
    # Identify your application (Required by MusicBrainz)
    musicbrainzngs.set_useragent("Example music app", "0.1", "http://example.com/music")
    
    # Authenticate if submitting data
    musicbrainzngs.auth("user", "password")
  5. Submit data to the test server

    master

    When testing data submission logic, you should point the library to the MusicBrainz test server instead of the production server. Use musicbrainzngs.set_hostname("test.musicbrainz.org") to switch hosts.

    Example for submitting barcodes:

    import musicbrainzngs
    
    # Switch to test server
    musicbrainzngs.set_hostname("test.musicbrainz.org")
    
    # Authenticate with test credentials
    musicbrainzngs.auth("test", "mb")
    
    barcodes = {
        "174a5513-73d1-3c9d-a316-3c1c179e35f8": "5099749534728",
        "838952af-600d-3f51-84d5-941d15880400": "602517737280"
    }
    
    musicbrainzngs.submit_barcodes(barcodes)
  6. Configure application identity with set_useragent

    master

    The set_useragent method is required by MusicBrainz webservice access rules to prevent rate limiting issues. It allows the service to identify your application and contact you if necessary.

    Signature: musicbrainzngs.set_useragent(name, version, contact)

    musicbrainzngs.set_useragent("Example music app", "0.1", "http://example.com/music")
  7. Fetch MusicBrainz data as dictionaries

    master

    The library provides functions to fetch specific MusicBrainz entities by ID or other identifiers. All data-fetching functions return the results as a Python dict. Attributes and XML elements are mapped directly to dictionary keys. List entities are returned as list types.

    When fetching data, you can use:

    • includes: A list of additional data to include in the response.
    • release_status: Filter by status (see musicbrainz.VALID_RELEASE_STATUSES).
    • release_type: Filter by type (see musicbrainz.VALID_RELEASE_TYPES).

    Common functions include:

    • get_artist_by_id(artist_id)
    • get_release_by_id(release_id)
    • get_recording_by_id(recording_id)
    • get_recordings_by_isrc(isrc)
    • get_work_by_id(work_id)
    • get_release_group_by_id(release_group_id)
    • get_label_by_id(label_id)
    • get_area_by_id(area_id)
    • get_place_by_id(place_id)
    • get_event_by_id(event_id)
    • get_instrument_by_id(instrument_id)
    • get_series_by_id(series_id)
    • get_url_by_id(url_id)
    • get_releases_by_discid(discid)
    • get_works_by_iswc(iswc)
    • get_collections()
    • get_releases_in_collection(collection_id)
  8. Configure musicbrainzngs general settings

    master

    Before making requests, you should configure the library's environment. This includes setting your user agent (required by MusicBrainz), rate limits, and hostnames. Use the following functions to manage these settings:

    • set_useragent(useragent): Sets the user agent string. This is required by MusicBrainz webservice access rules.
    • set_rate_limit(rate_limit): Configures the rate limiting behavior.
    • set_hostname(hostname): Sets the primary MusicBrainz hostname.
    • set_caa_hostname(hostname): Sets the Cover Art Archive hostname.
    • set_parser(parser): Configures the XML parser.
    • set_format(format): Sets the desired response format.