Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Split track number in suggest_filename for audio-metadata/mutagen met…
Browse files Browse the repository at this point in the history
…adata
  • Loading branch information
thebigmunch committed Jan 16, 2019
1 parent 63a9cb8 commit 81bd4f4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org).

[Commits](https://github.com/thebigmunch/google-music-utils/compare/2.0.0...master)

### Fixed

* Split track number in ``suggest_filename`` for audio-metadata/mutagen metadata.


## [2.0.0](https://github.com/thebigmunch/google-music-utils/releases/tag/2.0.0) (2019-01-15)

Expand Down
26 changes: 18 additions & 8 deletions src/google_music_utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def _replace_invalid_characters(filepath):
return filepath


def _split_track_number(field):
match = re.match(r'(\d+)(?:/\d+)?', field)

return match.group(1) if match else field


def suggest_filename(metadata):
"""Generate a filename like Google for a song based on metadata.
Expand All @@ -29,7 +35,11 @@ def suggest_filename(metadata):
elif 'title' in metadata and 'trackNumber' in metadata: # Mobile.
suggested_filename = f"{metadata['trackNumber']:0>2} {metadata['title']}"
elif 'title' in metadata and 'tracknumber' in metadata: # audio-metadata/mutagen.
track_number = list_to_single_value(metadata['tracknumber'])
track_number = _split_track_number(
list_to_single_value(
metadata['tracknumber']
)
)
title = list_to_single_value(metadata['title'])

suggested_filename = f"{track_number:0>2} {title}"
Expand All @@ -39,12 +49,6 @@ def suggest_filename(metadata):
return _replace_invalid_characters(suggested_filename)


def _split_track_number(field):
match = re.match(r'(\d+)(?:/\d+)?', field)

return match.group(1) if match else field


def template_to_filepath(template, metadata, template_patterns=None):
"""Create directory structure and file name based on metadata template.
Expand Down Expand Up @@ -94,7 +98,13 @@ def template_to_filepath(template, metadata, template_patterns=None):
if any(
template_patterns[key] == tracknumber_field for tracknumber_field in ['tracknumber', 'track_number']
):
track_number = _split_track_number(str(list_to_single_value(metadata[template_patterns[key]])))
track_number = _split_track_number(
str(
list_to_single_value(
metadata[template_patterns[key]]
)
)
)
metadata[template_patterns[key]] = track_number.zfill(2)

part = part.replace(
Expand Down

0 comments on commit 81bd4f4

Please sign in to comment.