Last active
April 19, 2024 12:34
-
-
Save jackdpage/16e3515bab09ade2a8978bd64b4bd123 to your computer and use it in GitHub Desktop.
Fetch a Discogs collection and print album artist and format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
USER = 'USERNAME' | |
TOKEN = 'TOKEN' | |
def get_discogs_release_page(page): | |
url = '/'.join(['https://api.discogs.com/users', USER, 'collection/folders/0/releases']) | |
params = {'page': page, 'sort': 'artist'} | |
headers = {'Authorization': 'Discogs token='+TOKEN} | |
request = requests.get(url, params=params, headers=headers) | |
return request.json() | |
def append_discogs_release_page(response): | |
exp = re.compile('(?P<clean>.*) \([0-9]*\)') | |
for release in response['releases']: | |
format = release['basic_information']['formats'][0]['name'] | |
artist = release['basic_information']['artists'][0]['name'] | |
# Check if an artist has a number in parentheses at the end, then | |
# remove that number, leaving just the artist name. | |
match = exp.match(artist) | |
if match: | |
artist = match.group('clean') | |
title = release['basic_information']['title'] | |
entry = [artist, title, format] | |
print(':'.join(entry)) | |
def iterate_discogs_release_page(page): | |
"""Recursively append all Discogs release pages, beginning at *page*.""" | |
response = get_discogs_release_page(page) | |
append_discogs_release_page(response) | |
if 'next' in response['pagination']['urls']: | |
page += 1 | |
iterate_discogs_release_page(page) | |
iterate_discogs_release_page(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment