Skip to content

Instantly share code, notes, and snippets.

@whinette
Last active March 11, 2019 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whinette/52fd2ed712205ae267589b4a6d46847d to your computer and use it in GitHub Desktop.
Save whinette/52fd2ed712205ae267589b4a6d46847d to your computer and use it in GitHub Desktop.
update cuesheet date tag from date in folder name
# -*- coding: utf-8 -*-
import os, sys
import logging
from datetime import datetime
import re
g_logger = logging.getLogger()
g_logger.setLevel(logging.INFO)
if __name__ == '__main__':
formatter = logging.Formatter('%(message)s')
fh = logging.FileHandler('taging_{:%Y-%m-%d}.log'.format(datetime.now()))
ch = logging.StreamHandler(sys.stdout)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
g_logger.addHandler(fh)
g_logger.addHandler(ch)
g_logger.info('-' * 80)
re_date = re.compile('^.*\\'+os.sep+'(\d{4})\.(\d{2})\.(\d{2}) .+$')
re_cuedate = re.compile('REM DATE (.+)\n')
for path, _, files in os.walk(os.getcwd()):
g_logger.info('\n{}'.format(path))
for fname in [x for x in files if x.endswith('.cue')]:
fullpath = os.path.join(path, fname)
res = re_date.match(path)
with open(fullpath, 'r') as f:
cuedata = f.read()
match = re_cuedate.search(cuedata)
newdata = None
if match and res:
newdata = cuedata.replace(str(match.group(0)), 'REM DATE {}-{}-{}\n'.format(res.group(1), res.group(2), res.group(3)))
g_logger.info('\t{} OK'.format(fname))
elif res:
with open(fullpath, 'r') as f:
for i, line in enumerate(f):
if i == 0:
newdata = line
continue
elif i == 1:
newdata += 'REM DATE {}-{}-{}\n'.format(res.group(1), res.group(2), res.group(3))
newdata += line
g_logger.info('\t{} OK (warning: added REM DATE)'.format(fname))
else:
g_logger.info('\t{} NO DATE FOUND (error)'.format(fname))
if newdata:
with open(fullpath, 'w') as f:
f.write(newdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment