python3-discogs-client

repository·master·Indexed 19 days ago

https://github.com/joalla/discogs_client

A Python client for the Discogs REST-API used to query the Discogs database for artists, releases, labels, and users. It supports managing Marketplace inventory and user data (profiles, collections, wantlists, and orders) via OAuth 1.0a or User-token authentication.

Tokens
7.8K
Snippets
39
Records
41
Agent score
64%

What's inside python3-discogs-client

  1. Overview of python3-discogs-client capabilities

    master

    The python3-discogs-client is a Python module used to interact with the Discogs REST-API. It allows developers to:

    • Query the Discogs database: Retrieve information regarding artists, releases, labels, users, and Marketplace listings.
    • Perform authenticated actions: Using OAuth 1.0a authorization, the client can modify user data, including profile information, collections, wantlists, inventory, and orders.
  2. Manage Collection Data

    master

    To manage a user's collection, you must use an authenticated Client object. You can access the user's identity via d.identity(), which returns a User object containing collection_folders.

    Collection Folders

    • Folder 0: A special folder containing all releases in the user's collection.
    • Folder 1: The "Uncategorized" folder.
    • Folders 2 to n: Manually created folders.

    Collection Items

    Items in a collection are returned as CollectionItemInstance objects. These objects link a specific instance of a release to a folder and provide metadata like date_added and rating.

    To find all copies of a specific release across all folders, use the collection_items(release_id) method. This is often faster than iterating through folders manually.

    me = d.identity()
    
    # Iterate through all items in the entire collection (Folder 0)
    for item in me.collection_folders[0].releases:
        print(item)
    
    # Get all instances of a specific release ID across all folders
    release_instances = me.collection_items(22155985)
    for instance in release_instances:
        print(instance.folder_id)
        print(instance.release.title)
  3. Access properties on Discogs objects

    master

    When you fetch data (e.g., via d.release(id)), the returned objects contain various properties and methods representing the Discogs data model.

    If you need to access raw data that has not been explicitly mapped to a property in the client, you can use the .data property, which contains the underlying dictionary from the API response.

    To discover available properties on an object, you can use Python's built-in dir() function or inspect the discogs_client.models module.

    release = d.release(1293022)
    print(release.title)
    artists = release.artists
    
    # Accessing unmapped raw data
    print(release.data.keys())
  4. Use OAuth Authentication

    master

    OAuth is used when building applications that act as a proxy for other users, allowing them to manage their profile, collection, wantlist, and marketplace via your app.

    To use OAuth, you first need a consumer key and consumer secret, which you obtain from the Discogs developer settings by creating an application.

    1. Initialize the Client

    You can initialize the Client with your consumer credentials. If you already have an OAuth token and secret saved, you can provide them immediately to skip the authorization flow.

    import discogs_client
    
    # Initialize with consumer credentials only
    d = discogs_client.Client(
        'my_user_agent/1.0',
        consumer_key='my_consumer_key',
        consumer_secret='my_consumer_secret'
    )
    
    # OR initialize with existing tokens
    d = discogs_client.Client(
        'my_user_agent/1.0',
        consumer_key='my_consumer_key',
        consumer_secret='my_consumer_secret',
        token='my_token',
        secret='my_token_secret'
    )
  5. Query Release Data

    master

    You can access almost all data available on a Discogs Release page using the release method on a Client object. Most release data can be queried without authentication, but certain features like searching, collection querying, or fetching album art require an authenticated client.

    Commonly available attributes on a Release object include:

    • artists: A list of Artist objects.
    • formats: A list of dictionaries containing release types (e.g., Vinyl, CD) and descriptions.
    • genres: A list of genre strings.
    • images: A list of dictionaries containing image URLs (requires authentication for URLs).
    • tracklist: A list of Track objects.
    • community: A CommunityDetails object containing ratings, wants, haves, and contributors.
    • data: A dictionary containing the release ID and its resource_url.
    # Authenticate using your Client object
    d = discogs_client.Client(config.agent, user_token=config.my_token)
    
    # Access a specific release
    release = d.release(20017387)
    
    print(release.artists[0].name)
    print(release.formats)
    print(release.genres)
    print(release.tracklist[0].title)
    print(release.community.rating)
  6. Update an existing marketplace listing

    master

    To update a listing, retrieve it from an inventory (optionally using .sort() to find a specific item), modify its attributes directly, and then call .save() to persist the changes to the Discogs API.

    Sort criteria and order are managed via discogs_client.utils.Sort.

    inventory = me.inventory    # Get up to date inventory
    inventory.sort(             # Sort by price in descending order
        Sort.By.PRICE,          # == 'price'
        Sort.Order.DESCENDING)  # == 'desc'
    listing = inventory[0]      # Get the first item, i.e. most expensive
    listing.price = 34.99       # Update its price
    listing.save()              # Save changes made to listing
  7. Read a user's public inventory

    master

    You can read a user's public inventory without authentication. Access the inventory via the inventory attribute of a user object, then use .page(index) to retrieve paginated results.

    user = d.user('username')         # gets a user with username
    inventory = user.inventory        # get that user's inventory
    first_page = inventory.page(0)    # get the first page
    first_listing = first_page[0]     # get the first listing from that page
    release = first_listing.release   # get the release from the release
  8. Build the documentation locally

    master

    The documentation is built using Sphinx. To generate the HTML version of the documentation:

    1. Navigate to the docs/ directory.
    2. Run make html.
    3. View the output in docs/build/html/index.html using a web browser.

    If you encounter issues with stale builds, clean the build directory before rebuilding using make clean; make html.

    Recommended Python versions: 3.9 or 3.11.

    Note: You may ignore warnings regarding unexpected indentation in docstrings (e.g., WARNING: Unexpected indentation).

    cd docs
    make html
    # Or to clean and rebuild:
    make clean; make html
  9. Add, Remove, and Move Collection Items

    master

    The following methods are used to manipulate a user's collection. Note that these methods require CollectionItemInstance objects as arguments.

    • Add a release: Use folder.add_release(release_id) or pass a Release object.
    • Remove a release entirely: Use folder.remove_release(instance). This deletes the item from the collection completely.
    • Uncategorize a release: Use folder.uncategorize_release(instance). This removes the item from a specific folder but keeps it in the user's collection (moving it to the Uncategorized folder).
    • Move a release: Use folder.move_release(instance, target_folder_id) to move an item from one folder to another.
    # Add a release to a specific folder
    me.collection_folders[2].add_release(17392219)
    
    # Remove a specific instance from a folder (deletes from collection)
    folder = me.collection_folders[2]
    folder.remove_release(folder.releases[0])
    
    # Move a release to a different folder
    # First, find the target folder ID
    target_folder_id = 1 
    for instance in me.collection_items(22155985):
        current_folder = me.collection_folders[instance.folder_id]
        current_folder.move_release(instance, target_folder_id)
  10. Use User-token Authentication

    master

    User-token Authentication is a simple method for scripts or applications that only represent a single user (e.g., a personal store-front). You must first generate a token in your Discogs account settings under Settings > Developers > Generate new token.

    When using this method, you are limited to information accessible by your specific user account and cannot make requests on behalf of other users.

    import discogs_client
    d = discogs_client.Client('my_user_agent/1.0', user_token='my_user_token')