Skip to content

Commit

Permalink
Parse extra artists, remove duplicate artists count, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Jan 11, 2024
1 parent a6f705f commit 9e77305
Show file tree
Hide file tree
Showing 4 changed files with 841 additions and 21 deletions.
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.1.7"
version = "0.1.8"
description = "A rudimentary audio tagger based on Discogs metadata."
authors = ["infojunkie <[email protected]>"]
readme = "README.md"
Expand Down
50 changes: 35 additions & 15 deletions src/discogs_tag/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import os
from pprint import pprint
import glob
import sys
import re
from discogs_tag import __NAME__, __VERSION__

def tag(release, dir = './', dry = False):
def tag(release, dir = './', dry = False, ignore = False):
"""Tag the audio files in dir with the given Discogs release."""
request = urllib.request.Request(f'https://api.discogs.com/releases/{release}', headers = {
'User-Agent': f'{__NAME__} {__VERSION__}'
Expand All @@ -18,39 +20,57 @@ def tag(release, dir = './', dry = False):
glob.glob(os.path.join(glob.escape(dir), '**', '*.flac'), recursive=True) +
glob.glob(os.path.join(glob.escape(dir), '**', '*.mp3'), recursive=True)
)
tracks = list(filter(lambda t: t['type_'] == 'track', data['tracklist']))
if (len(files) != len(tracks)):
apply_metadata(data, files, dry, ignore)

def apply_metadata(data, files, dry, ignore):
tracks = list(filter(lambda t: t['type_'] == 'track', data['tracklist']))
if (len(files) != len(tracks)):
if (not ignore):
raise Exception(f'Expecting {len(tracks)} files but found {len(files)}. Aborting.')
for n, track in enumerate(tracks):
else:
print(f'Expecting {len(tracks)} files but found {len(files)}. Ignoring.', file=sys.stderr)

for n, track in enumerate(tracks):
try:
audio = mutagen.File(files[n], easy=True)
apply_metadata(track, audio)
merge_metadata(track, audio)
if (dry):
pprint(audio)
else:
audio.save()
except Exception as e:
if (not ignore):
raise e
else:
print(e, file=sys.stderr)

if (not dry):
print(f'Processed {len(files)} audio files.')
if (not dry):
print(f'Processed {len(files)} audio files.')

def apply_metadata(track, audio):
def merge_metadata(track, audio):
audio['title'] = track['title']
artists = []
if 'artists' in track:
audio['artist'] = ', '.join([artist_name(artist) for artist in track['artists']])
artists += [artist_name(artist) for artist in track['artists']]
if 'extraartists' in track:
artists += [artist_name(artist) for artist in filter(lambda a: a['role'].casefold() != 'Written-By'.casefold(), track['extraartists'])]
if (artists):
audio['artist'] = ', '.join(artists)
positions = track['position'].split('-')
audio['tracknumber'] = positions[-1]
if (len(positions) > 1):
audio['discnumber'] = positions[0]
composers = ', '.join([artist_name(composer) for composer in filter(lambda a: a['role'].casefold() == 'Written-By'.casefold(), track['extraartists'])]) if 'extraartists' in track else None
composers = [artist_name(composer) for composer in filter(lambda a: a['role'].casefold() == 'Written-By'.casefold(), track['extraartists'])] if 'extraartists' in track else None
if (composers):
audio['composer'] = composers
audio['composer'] = ', '.join(composers)

def artist_name(artist):
name = None
if 'anv' in artist and artist['anv']:
return artist['anv']
name = artist['anv']
elif 'name' in artist and artist['name']:
return artist['name']
else:
return None
name = artist['name']
return re.sub(r"\s+\(\d+\)$", '', name) if name else None

def cli():
fire.Fire(tag)
Loading

0 comments on commit 9e77305

Please sign in to comment.