Skip to content

Commit

Permalink
Accept release URI for tag command
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Jun 30, 2024
1 parent 2aa7aaf commit 64c020c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ SYNOPSIS
discogs-tag tag RELEASE <flags>

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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down
30 changes: 21 additions & 9 deletions src/discogs_tag/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_discogs_tag.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 64c020c

Please sign in to comment.