bgneal@1189: #!/usr/bin/env python bgneal@1189: # Copyright (C) 2021 by Brian Neal. bgneal@1189: bgneal@1189: """This file contains the main routine for the SG101 MP3 Comp Tool.""" bgneal@1189: bgneal@1189: import argparse bgneal@1189: import eyed3 bgneal@1189: import json bgneal@1189: import os.path bgneal@1189: import re bgneal@1189: bgneal@1189: bgneal@1189: DESC = """SG101 MP3 Comp Tool""" bgneal@1189: TITLE_RE = re.compile(r'^(\d+)\s+-\s+(.+)"(.+)"$') bgneal@1189: bgneal@1189: def process_mp3(mp3, args): bgneal@1189: title = mp3['fields']['title'] bgneal@1190: print(f'Processing {title}...') bgneal@1189: m = TITLE_RE.match(title) bgneal@1189: track_num = int(m[1]) bgneal@1189: artist = m[2].strip() bgneal@1189: title = m[3] bgneal@1189: filename = mp3['fields']['file'].split('/')[-1] bgneal@1189: filepath = os.path.join(args.mp3_dir, filename) bgneal@1189: bgneal@1189: mp3file = eyed3.load(filepath) bgneal@1190: if mp3file.tag is None: bgneal@1190: mp3file.tag = eyed3.id3.tag.Tag() bgneal@1189: mp3file.tag.clear() bgneal@1189: mp3file.tag.title = title bgneal@1189: mp3file.tag.artist = artist bgneal@1189: mp3file.tag.album = args.album bgneal@1189: mp3file.tag.recording_date = args.recording_date bgneal@1189: mp3file.tag.track_num = track_num bgneal@1189: mp3file.tag.genre = 'Instrumental Rock' bgneal@1189: mp3file.tag.save() bgneal@1189: bgneal@1189: bgneal@1189: def main(argv=None): bgneal@1189: parser = argparse.ArgumentParser(description=DESC) bgneal@1189: parser.add_argument('json', metavar='JSON', bgneal@1189: help='JSON file to control the tagging process') bgneal@1189: parser.add_argument('-d', '--mp3-dir', default='.', bgneal@1189: help='directory to find MP3 files') bgneal@1189: parser.add_argument('-a', '--album', bgneal@1189: default='SurfGuitar101.com 2021 MP3 Compilation', bgneal@1189: help='album name tag value') bgneal@1189: parser.add_argument('-r', '--recording-date', default='2021', bgneal@1189: help='recording date tag value') bgneal@1189: bgneal@1189: args = parser.parse_args(args=argv) bgneal@1189: bgneal@1189: with open(args.json, 'r') as fp: bgneal@1189: mp3data = json.load(fp) bgneal@1189: bgneal@1189: for mp3 in mp3data: bgneal@1189: process_mp3(mp3, args) bgneal@1189: bgneal@1189: if __name__ == '__main__': bgneal@1189: main()