Skip to content

Commit

Permalink
Fix #1 Support sub-tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Jul 20, 2024
1 parent b77b763 commit 2efe332
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ An audio tagger based on Discogs metadata.

[![PyPI Version](https://img.shields.io/pypi/v/discogs-tag.svg)](https://pypi.org/project/discogs-tag/)

# Development
- Install [`poetry`](https://python-poetry.org/docs/#installation)
- `poetry install && poetry build && pip install .`

# Usage
```shell
NAME
Expand Down Expand Up @@ -46,6 +42,8 @@ DESCRIPTION
The SKIP flag can take one or more of the following values, comma-separated:
artist, composer, title, position, date, subtrack, album, genre, albumartist

If subtracks are skipped, subtrack titles get appended to their parent track.

POSITIONAL ARGUMENTS
RELEASE

Expand Down Expand Up @@ -75,6 +73,8 @@ DESCRIPTION
The SKIP flag can take one or more of the following values, comma-separated:
artist, composer, title, position, date, subtrack, album, genre, albumartist

If subtracks are skipped, subtrack titles get appended to their parent track.

POSITIONAL ARGUMENTS
SRC

Expand Down Expand Up @@ -128,3 +128,7 @@ FLAGS
NOTES
You can also use flags syntax for POSITIONAL ARGUMENTS
```
# Development
- Install [`poetry`](https://python-poetry.org/docs/#installation)
- `poetry install && poetry build && pip install .`

6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 = "1.1.1"
version = "1.2.0"
description = "An audio tagger based on Discogs metadata."
authors = ["infojunkie <[email protected]>"]
repository = "https://github.com/infojunkie/discogs-tag.py"
Expand Down
19 changes: 14 additions & 5 deletions src/discogs_tag/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

AUDIO_EXTENSIONS = ['flac', 'mp3']

VARIOUS_ARTISTS = [
'Various Artists'
]

def tag(
release,
dir='./',
Expand All @@ -49,6 +53,8 @@ def tag(
The SKIP flag can take one or more of the following values, comma-separated:
artist, composer, title, position, date, subtrack, album, genre, albumartist
If subtracks are skipped, subtrack titles get appended to their parent track.
"""
options = parse_options(locals())
response = get_release(release)
Expand All @@ -68,6 +74,8 @@ def copy(
The SKIP flag can take one or more of the following values, comma-separated:
artist, composer, title, position, date, subtrack, album, genre, albumartist
If subtracks are skipped, subtrack titles get appended to their parent track.
"""
options = parse_options(locals())
src_files = list_files(src)
Expand Down Expand Up @@ -347,6 +355,8 @@ def artist_name(artist):

if not options['skip_title']:
audio['title'] = track['title']
if options['skip_subtrack'] and 'sub_tracks' in track:
audio['title'] += ': ' + ' / '.join([subtrack['title'] for subtrack in track['sub_tracks'] if subtrack['type_'] == 'track'])

if not options['skip_artist']:
artists = []
Expand Down Expand Up @@ -377,8 +387,8 @@ def artist_name(artist):
if 'title' in release:
audio['album'] = release['title']

if not options['skip_composer']:
composers = [artist_name(composer) for composer in filter(lambda a: a['role'].casefold() in [(lambda c: c.casefold())(c) for c in COMPOSER_TAGS], track['extraartists'])] if 'extraartists' in track else None
if not options['skip_composer'] and 'extraartists' in track:
composers = [artist_name(composer) for composer in track['extraartists'] if composer['role'].casefold() in [(lambda c: c.casefold())(c) for c in COMPOSER_TAGS]]
if composers:
audio['composer'] = ', '.join(composers)

Expand All @@ -388,9 +398,8 @@ def artist_name(artist):
if len(positions) > 1:
audio['discnumber'] = positions[0]

if not options['skip_date']:
if 'year' in release and release['year']:
audio['date'] = str(release['year'])
if not options['skip_date'] and 'year' in release:
audio['date'] = str(release['year'])

return audio

Expand Down
46 changes: 46 additions & 0 deletions tests/test_discogs_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_read_metadata():
assert release['artists'] == [{ 'anv': 'Album Artist' }]
assert release['tracklist'][0]['position'] == '1-2'
assert release['tracklist'][0]['title'] == 'Title'

release = read_metadata([{
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand All @@ -50,6 +51,7 @@ def test_read_metadata():
'date': ['2024']
}], {})
assert release['tracklist'][0]['position'] == '2'

release = read_metadata([{
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand Down Expand Up @@ -125,6 +127,42 @@ def test_merge_metadata():
assert audio['composer'] == 'Composer 1, Composer 2'
assert audio['date'] == '2002'

audio = merge_metadata({
'year': 2002,
'artists': [{
'anv': 'Artist 1'
}, {
'name': 'Artist 2'
}, {
'anv': '',
'name': 'Artist 3 (56)'
}],
}, {
'title': 'Title',
'position': '1-02',
'sub_tracks': [{
'type_': 'track',
"title": "Sub track 1",
}, {
'type_': 'track',
'title': 'Sub track 2',
}, {
'type_': 'data',
'title': "Not a track",
}],
'extraartists': [{
'role': 'Guitar',
'name': 'Guitarist'
}, {
'role': 'Written-By',
'name': 'Composer 1'
}, {
'role': 'Composed By',
'name': 'Composer 2'
}]
}, { 'title': 'Some other title' }, parse_options({ 'skip': ['subtrack'] }))
assert audio['title'] == 'Title: Sub track 1 / Sub track 2'

def test_apply_metadata():
with open('tests/18051880.json') as release:
data = json.load(release)
Expand Down Expand Up @@ -155,6 +193,7 @@ def test_rename_component():
'title': ['Title'],
'date': ['2024']
}, '%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == '1-02 Title'

assert rename_component({
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand All @@ -165,6 +204,7 @@ def test_rename_component():
'title': ['Title'],
'date': ['2024']
}, '%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == '02 Title'

with pytest.raises(IndexError) as error:
rename_component({
'artist': ['Artist'],
Expand All @@ -191,6 +231,7 @@ def test_rename_path():
'title': ['Title'],
'date': ['2024']
}, '%z - (%y) %b/%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == ('/src/path/Album Artist - (2024) Album', '/src/path/Album Artist - (2024) Album')

assert rename_path('/src/path/from', {
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand All @@ -202,6 +243,7 @@ def test_rename_path():
'title': ['Title'],
'date': ['2024']
}, '%z - (%y) %b/%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == ('/src/path/Album Artist - (2024) Album1 - Album2', '/src/path/Album Artist - (2024) Album1 - Album2')

assert rename_path('/src/path/from', {
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand All @@ -212,6 +254,7 @@ def test_rename_path():
'tracknumber': [2],
'title': ['Title']
}, '%z - (%y) %b/%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == ('/src/path/Album Artist - Album1 - Album2', '/src/path/Album Artist - Album1 - Album2')

assert rename_path('/src/path/from', {
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand All @@ -223,6 +266,7 @@ def test_rename_path():
'title': ['Title'],
'date': ['2024']
}, '%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == ('/src/path/from', '/src/path/from')

assert rename_path('/src/path/from', {
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand All @@ -247,6 +291,7 @@ def test_rename_file():
'title': ['Title'],
'date': ['2024']
}, '%z - (%y) %b/%d-%n %t', parse_options({ 'dry': True, 'ignore': False })) == '/dest/path/to/1-02 Title.flac'

assert rename_file('/src/path/from/test.flac', '/dest/path/to', {
'artist': ['Artist1 / Artist2'],
'albumartist': ['Album Artist'],
Expand All @@ -258,6 +303,7 @@ def test_rename_file():
'title': ['Title1 / Title2'],
'date': ['2024']
}, '%z - (%y) %b/%d-%n %a - %t', parse_options({ 'dry': True, 'ignore': False })) == '/dest/path/to/1-02 Artist1 - Artist2 - Title1 - Title2.flac'

assert rename_file('/src/path/from/test.flac', '/dest/path/to', {
'artist': ['Artist'],
'albumartist': ['Album Artist'],
Expand Down

0 comments on commit 2efe332

Please sign in to comment.