Skip to content

Commit

Permalink
Support brackets when removing blank tags
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Jul 7, 2024
1 parent 2cd1ff4 commit f6b47d4
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
discogs-tag
===========

A rudimentary audio tagger based on Discogs metadata.
An audio tagger based on Discogs metadata.

# Installation
- Install [`poetry`](https://python-poetry.org/docs/#installation)
Expand Down
112 changes: 100 additions & 12 deletions poetry.lock

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

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
[tool.poetry]
name = "discogs-tag"
version = "0.5.1"
description = "A rudimentary audio tagger based on Discogs metadata."
version = "1.0.0"
description = "An audio tagger based on Discogs metadata."
authors = ["infojunkie <[email protected]>"]
readme = "README.md"
packages = [{include = "discogs_tag", from = "src"}]

[tool.poetry.dependencies]
python = "^3.10"
fire = "^0.5.0"
mutagen = "^1.46.0"
importlib-metadata = "^6.8.0"
pathvalidate = "^3.2.0"
fire = "^0.6.0"
regex = "^2024.5.15"

[tool.poetry.group.dev.dependencies]
pytest = "^7.0.0"
Expand Down
27 changes: 19 additions & 8 deletions src/discogs_tag/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import glob
import sys
import re
import regex as re
from urllib.parse import urlparse
from pprint import pprint
from functools import reduce
Expand Down Expand Up @@ -233,8 +233,7 @@ def reduce_track(tracks, track):

def rename_component(audio, format, options):
""" Rename a path component based on format string with tags from the audio metadata. """
component = format
for tag, fn in {
tags = {
'%a': (lambda audio: audio.get('artist', [''])[0]),
'%z': (lambda audio: audio.get('albumartist', [''])[0]),
'%b': (lambda audio: audio.get('album', [''])[0]),
Expand All @@ -244,20 +243,32 @@ def rename_component(audio, format, options):
'%n': (lambda audio: '%02d' % int(audio.get('tracknumber', [0])[0])),
'%t': (lambda audio: audio.get('title', [''])[0]),
'%y': (lambda audio: audio.get('date', [''])[0])
}.items():
if tag in component:
}

# First, remove from format string all empty tags and neighbouring characters.
for tag, fn in tags.items():
if tag in format:
try:
replace = fn(audio).strip()
# If replacement is empty, also remove format chars until next tag.
if not replace:
component = re.sub(re.escape(tag) + r"[^%]*", '', component)
else:
component = component.replace(tag, replace)
format = re.sub(r"\p{Ps}?" + re.escape(tag) + r"[^%]*", '', format)
except Exception as e:
if options['ignore']:
print(e, file=sys.stderr)
else:
raise e

# Now replace tags with metadata values.
component = format
for tag, fn in tags.items():
if tag in format:
try:
replace = fn(audio).strip()
component = component.replace(tag, replace)
except Exception as e:
pass

return component

def rename_path(src_root, audio, format, options):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_discogs_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ 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'],
'album': ['Album1 / Album2'],
'composer': ['Composer'],
'discnumber': ['1'],
'genre': ['Genre'],
'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 Down

0 comments on commit f6b47d4

Please sign in to comment.