mirror of
https://github.com/iluvcapra/mfbatch.git
synced 2025-12-31 17:00:50 +00:00
Compare commits
3 Commits
iluvcapra-
...
v0.5.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef1377a616 | ||
|
|
b7535618e6 | ||
|
|
dfa2a2a5ad |
2
.github/workflows/python-publish.yml
vendored
2
.github/workflows/python-publish.yml
vendored
@@ -35,4 +35,4 @@ jobs:
|
||||
- name: Build package
|
||||
run: python -m build
|
||||
- name: Publish package
|
||||
uses: pypa/gh-action-pypi-publish@v1.8.6
|
||||
uses: pypa/gh-action-pypi-publish@v1.12.4
|
||||
|
||||
@@ -8,9 +8,8 @@ from subprocess import CalledProcessError, run
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
import shlex
|
||||
from typing import Callable, List, Tuple
|
||||
from typing import Callable
|
||||
import inspect
|
||||
from io import StringIO
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
@@ -30,83 +29,70 @@ def execute_batch_list(batch_list_path: str, dry_run: bool, interactive: bool):
|
||||
parser.eval(line, line_no, interactive)
|
||||
|
||||
|
||||
def sort_flac_files(file_list, mode):
|
||||
"Sort flac files"
|
||||
if mode == 'path':
|
||||
return sorted(file_list)
|
||||
if mode == 'mtime':
|
||||
return sorted(file_list, key=os.path.getmtime)
|
||||
if mode == 'ctime':
|
||||
return sorted(file_list, key=os.path.getctime)
|
||||
if mode == 'name':
|
||||
return sorted(file_list, key=os.path.basename)
|
||||
|
||||
return file_list
|
||||
|
||||
|
||||
def write_batchfile_entries_for_file(path, metadatums) -> Tuple[dict, str]:
|
||||
"Create batchfile entries for `path`"
|
||||
buffer = StringIO()
|
||||
|
||||
try:
|
||||
this_file_metadata = metadata_funcs.read_metadata(path)
|
||||
|
||||
except CalledProcessError as e:
|
||||
buffer.write(f"# !!! METAFLAC ERROR ({e.returncode}) while reading "
|
||||
f"metadata from the file {path}\n\n")
|
||||
return metadatums, buffer.getvalue()
|
||||
|
||||
for this_key, this_value in this_file_metadata.items():
|
||||
if this_key not in metadatums:
|
||||
buffer.write(f":set {this_key} "
|
||||
f"{shlex.quote(this_value)}\n")
|
||||
metadatums[this_key] = this_value
|
||||
else:
|
||||
if this_value != metadatums[this_key]:
|
||||
buffer.write(f":set {this_key} "
|
||||
f"{shlex.quote(this_value)}"
|
||||
"\n")
|
||||
metadatums[this_key] = this_value
|
||||
|
||||
keys = list(metadatums.keys())
|
||||
for key in keys:
|
||||
if key not in this_file_metadata:
|
||||
buffer.write(f":unset {key}\n")
|
||||
del metadatums[key]
|
||||
|
||||
buffer.write(path + "\n\n")
|
||||
|
||||
return metadatums, buffer.getvalue()
|
||||
|
||||
|
||||
def create_batch_list(flac_files: List[str], command_file: str,
|
||||
sort_mode='path'):
|
||||
def create_batch_list(command_file: str, recursive=True, sort_mode='path'):
|
||||
"""
|
||||
Read all FLAC files in the cwd and create a batchfile that re-creates all
|
||||
of their metadata.
|
||||
|
||||
:param flac_files: Paths of files to create batchfile from
|
||||
:param command_file: Name of new batchfile
|
||||
:param recursive: Recursively enter directories
|
||||
:param sort_mode: Order of paths in the batch list. Either 'path',
|
||||
'mtime', 'ctime', 'name'
|
||||
:param input_files: FLAC files to scan
|
||||
"""
|
||||
|
||||
flac_files = sort_flac_files(flac_files, sort_mode)
|
||||
|
||||
with open(command_file, mode='w', encoding='utf-8') as f:
|
||||
metadatums = {}
|
||||
|
||||
f.write("# mfbatch\n\n")
|
||||
# f.write("""
|
||||
# # :set DESCRIPTION ""
|
||||
# # :set TITLE ""
|
||||
# # :set VERSION ""
|
||||
# # :set ALBUM ""
|
||||
# # :set ARTIST ""
|
||||
# # :set TRACKNUMBER ""
|
||||
# # :set COPYRIGHT ""
|
||||
# # :set LICENSE ""
|
||||
# # :set CONTACT ""
|
||||
# # :set ORGAIZATION ""
|
||||
# # :set LOCATION ""
|
||||
# # :set MICROPHONE ""
|
||||
# """)
|
||||
metadatums = {}
|
||||
flac_files = glob('./**/*.flac', recursive=recursive)
|
||||
|
||||
for path in tqdm(flac_files, unit='File',
|
||||
desc='Scanning with metaflac...'):
|
||||
if sort_mode == 'path':
|
||||
flac_files = sorted(flac_files)
|
||||
elif sort_mode == 'mtime':
|
||||
flac_files = sorted(flac_files, key=os.path.getmtime)
|
||||
elif sort_mode == 'ctime':
|
||||
flac_files = sorted(flac_files, key=os.path.getctime)
|
||||
elif sort_mode == 'name':
|
||||
flac_files = sorted(flac_files, key=os.path.basename)
|
||||
|
||||
metadatums, buffer = write_batchfile_entries_for_file(path,
|
||||
metadatums)
|
||||
f.write(buffer)
|
||||
for path in tqdm(flac_files, unit='File', desc='Scanning FLAC files'):
|
||||
try:
|
||||
this_file_metadata = metadata_funcs.read_metadata(path)
|
||||
except CalledProcessError as e:
|
||||
f.write(f"# !!! METAFLAC ERROR ({e.returncode}) while reading "
|
||||
f"metadata from the file {path}\n\n")
|
||||
continue
|
||||
|
||||
f.write("# mfbatch: create batchlist operation complete\n")
|
||||
for this_key, this_value in this_file_metadata.items():
|
||||
if this_key not in metadatums:
|
||||
f.write(f":set {this_key} "
|
||||
f"{shlex.quote(this_value)}\n")
|
||||
metadatums[this_key] = this_value
|
||||
else:
|
||||
if this_value != metadatums[this_key]:
|
||||
f.write(f":set {this_key} "
|
||||
f"{shlex.quote(this_value)}"
|
||||
"\n")
|
||||
metadatums[this_key] = this_value
|
||||
|
||||
keys = list(metadatums.keys())
|
||||
for key in keys:
|
||||
if key not in this_file_metadata:
|
||||
f.write(f":unset {key}\n")
|
||||
del metadatums[key]
|
||||
|
||||
f.write(path + "\n\n")
|
||||
|
||||
|
||||
def main():
|
||||
@@ -119,10 +105,6 @@ def main():
|
||||
op.add_argument('-c', '--create', default=False,
|
||||
action='store_true',
|
||||
help='create a new list')
|
||||
op.add_argument('-F', '--from-file', metavar='FILE_LIST', action='store',
|
||||
default=None, help="get file paths from FILE_LIST when "
|
||||
"creating, instead of scanning directory"
|
||||
"a new list")
|
||||
op.add_argument('-e', '--edit', action='store_true',
|
||||
help="open batch file in the default editor",
|
||||
default=False)
|
||||
@@ -170,18 +152,7 @@ def main():
|
||||
|
||||
if options.create:
|
||||
mode_given = True
|
||||
flac_files: List[str] = []
|
||||
|
||||
if options.from_file:
|
||||
with open(options.from_file, mode='r',
|
||||
encoding='utf-8') as from_file:
|
||||
flac_files = [line.strip() for line in from_file.readlines()]
|
||||
else:
|
||||
flac_files = glob('./**/*.flac', recursive=True)
|
||||
|
||||
# print(flac_files)
|
||||
create_batch_list(flac_files, options.batchfile,
|
||||
sort_mode=options.sort)
|
||||
create_batch_list(options.batchfile, sort_mode=options.sort)
|
||||
|
||||
if options.edit:
|
||||
mode_given = True
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "mfbatch"
|
||||
version = "0.5.1"
|
||||
version = "0.5.2"
|
||||
description = "MetaFlac batch editor"
|
||||
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
||||
readme = "README.md"
|
||||
|
||||
Reference in New Issue
Block a user