Last active
January 14, 2016 01:48
-
-
Save WGH-/2ab69115c325fe689e00 to your computer and use it in GitHub Desktop.
Useful TLMC scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # encoding: utf-8 | |
| import sys | |
| import os | |
| import codecs | |
| import errno | |
| import re | |
| try: | |
| from scandir import walk | |
| except ImportError: | |
| from os import walk | |
| ALBUM_RE = re.compile( | |
| r"(?P<date>\d{4}\.\d{2}\.\d{2}\ )" | |
| r"(?P<id>\[.*\]\ )?" | |
| r"(?P<name>[^\[]*)" | |
| r"(?P<event>\[.*\])?" | |
| ) | |
| def split_path(path): | |
| l = [] | |
| separators = set((os.path.altsep, os.path.sep)) | |
| while path and path not in separators: | |
| path, tail = os.path.split(path) | |
| l.insert(0, tail) | |
| if path: | |
| l.insert(0, path) | |
| return l | |
| def process_file_everything(filename): | |
| parts = split_path(filename) | |
| while not parts[0].startswith("["): | |
| parts.pop(0) | |
| group, album = parts[:2] | |
| group = re.search(r"\[(.*?)\]", group).group(1) | |
| try: | |
| d = ALBUM_RE.search(album).groupdict("") | |
| except AttributeError: | |
| print("bad directory naming %r" % filename, file=sys.stderr) | |
| return | |
| d = {k: v.strip() for k, v in d.items()} | |
| date_inserted = False | |
| with open(filename, 'r+', encoding='utf_8_sig') as f: | |
| lines = f.readlines() | |
| f.seek(0) | |
| f.truncate() | |
| for line in lines: | |
| if line.startswith("REM DATE"): | |
| line = "REM DATE %s\r\n" % d['date'] | |
| date_inserted = True | |
| if line.startswith("FILE") and not date_inserted: | |
| f.write("REM DATE %s\r\n" % d['date']) | |
| date_inserted = True | |
| if line.startswith("PERFORMER"): | |
| line = 'PERFORMER "%s"\r\n' % group | |
| f.write(line) | |
| def main(): | |
| process_file = process_file_everything | |
| print("Using walk from %s" % walk.__module__, file=sys.stderr) | |
| for root, dirs, files in walk('.'): | |
| for filename in files: | |
| if filename.endswith('.cue'): | |
| filename = os.path.join(root, filename) | |
| try: | |
| process_file(filename) | |
| except IOError as e: | |
| if e.errno == errno.EACCES: | |
| print("RO-flag?", file=sys.stderr) | |
| raise | |
| elif e.errno == errno.ENOENT: | |
| print("File not found? (happens with long names on Windows)", file=sys.stderr) | |
| pass | |
| else: | |
| raise | |
| except UnicodeDecodeError as e: | |
| print("UnicodeDecodeError on %r" % filename, file=sys.stderr) | |
| raise | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import sys | |
| # use scandir for faster os.walk, if available | |
| # built-in in Python 3.5 | |
| try: | |
| from scandir import walk as oswalk | |
| except ImportError: | |
| from os import walk as oswalk | |
| def get_audio_from_cue(filename): | |
| with open(filename, encoding='utf_8_sig') as f: | |
| for line in f: | |
| if line.startswith("FILE"): | |
| return line[line.index('"')+1:line.rindex('"')] | |
| raise ValueError("couldn't find FILE in %r" % filename) | |
| def process_cue_file(cue): | |
| directory = os.path.dirname(cue) | |
| audio = os.path.join(directory, get_audio_from_cue(cue)) | |
| if not os.path.exists(audio): | |
| extra_files = {f for f in os.listdir(directory) | |
| if f != os.path.basename(cue) and | |
| not f.endswith('.cue') and | |
| not f.endswith('.part') | |
| } | |
| if extra_files: | |
| print("%r: audio doesn't exists, but there's something else: %r" % (cue, extra_files)) | |
| else: | |
| os.unlink(cue) | |
| def main(): | |
| print("Using walk from %s" % oswalk.__module__, file=sys.stderr) | |
| for root, dirs, files in oswalk('.'): | |
| for filename in files: | |
| if filename.endswith('.cue'): | |
| process_cue_file(os.path.join(root, filename)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment