diff --git a/README.md b/README.md index 301142a..fa62af6 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,10 @@ SYNOPSIS discogs-tag tag RELEASE DESCRIPTION - The RELEASE is the numeric portion of a Discogs release URL, e.g. 16215626 in - https://www.discogs.com/release/16215626-Pink-Floyd-Wish-You-Were-Here + The RELEASE can be one of the following: + - A full Discogs release URL, e.g. https://www.discogs.com/release/16215626-Pink-Floyd-Wish-You-Were-Here + - The numeric portion of the above, e.g. 16215626 + - A local file URI pointing to a release JSON file The SKIP flag can take one or more of the following values, comma-separated: artist, composer, title, position, date, subtrack, album, genre, albumartist diff --git a/pyproject.toml b/pyproject.toml index 8d77cb9..bd8929a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "discogs-tag" -version = "0.4.2" +version = "0.5.0" description = "A rudimentary audio tagger based on Discogs metadata." authors = ["infojunkie "] readme = "README.md" diff --git a/src/discogs_tag/cli.py b/src/discogs_tag/cli.py index 871180a..adc8d7b 100644 --- a/src/discogs_tag/cli.py +++ b/src/discogs_tag/cli.py @@ -6,6 +6,7 @@ import glob import sys import re +from urllib.parse import urlparse from pprint import pprint from functools import reduce from contextlib import suppress @@ -35,21 +36,20 @@ def tag( ): """Tag the audio files with the given Discogs release. - The RELEASE is the numeric portion of a Discogs release URL, e.g. 16215626 in - https://www.discogs.com/release/16215626-Pink-Floyd-Wish-You-Were-Here + The RELEASE can be one of the following: + - A full Discogs release URL, e.g. https://www.discogs.com/release/16215626-Pink-Floyd-Wish-You-Were-Here + - The numeric portion of the above, e.g. 16215626 + - A local file URI pointing to a release JSON file The SKIP flag can take one or more of the following values, comma-separated: artist, composer, title, position, date, subtrack, album, genre, albumartist """ options = parse_options(locals()) - request = urllib.request.Request(f'https://api.discogs.com/releases/{release}', headers = { - 'User-Agent': f'{__NAME__} {__VERSION__}' - }) - with urllib.request.urlopen(request) as response: - data = json.load(response) - files = list_files(dir) - apply_metadata(data, files, options) + response = get_release(release) + data = json.load(response) + files = list_files(dir) + apply_metadata(data, files, options) def copy( src, @@ -145,6 +145,18 @@ def rename( with suppress(OSError): os.rmdir(src_root) +def get_release(release): + """ Get release JSON from Discogs URL, file URI or Discogs release number. """ + headers = { + 'User-Agent': f'{__NAME__} {__VERSION__}' + } + try: + request = urllib.request.Request(release, headers=headers) + return urllib.request.urlopen(request) + except Exception: + request = urllib.request.Request(f'https://api.discogs.com/releases/{release}', headers=headers) + return urllib.request.urlopen(request) + def read_metadata(audios, options): """Read metadata from audio files and return data structure that mimics Discogs release.""" def safe_position(audio, n): diff --git a/tests/test_discogs_tag.py b/tests/test_discogs_tag.py index bf1c07f..20764d2 100644 --- a/tests/test_discogs_tag.py +++ b/tests/test_discogs_tag.py @@ -1,12 +1,13 @@ from discogs_tag.cli import ( + list_files, read_metadata, merge_metadata, apply_metadata, parse_options, - list_files, rename_component, rename_path, - rename_file + rename_file, + get_release ) import pytest import json @@ -213,3 +214,8 @@ def test_rename_file(): 'title': ['Title'], 'date': ['2024'] }, '%z - (%y) %b/', parse_options({ 'dry': True, 'ignore': False })) == '/dest/path/to/test.flac' + +def test_get_release(): + assert json.load(get_release('file:tests/16215626.json'))['id'] == 16215626 + assert json.load(get_release('16215626'))['id'] == 16215626 + assert json.load(get_release('https://api.discogs.com/releases/16215626'))['id'] == 16215626