You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

#!/usr/bin/env python3
"""Generate a CSV usable with the import:releases command from a directory.
This script assumes your have the same format for your release directories as
me, which is "Artist/Year - Release".
"""
import argparse
import csv
import os
import re
release_re = re.compile(r"(?P<year>\d{4})[\d\.]*(?: -)? (?P<title>.+)")
argparser = argparse.ArgumentParser()
argparser.add_argument('directory', help='directory to explore')
args = argparser.parse_args()
root = args.directory
with open('releases.csv', 'w', newline='') as output_file:
csv_writer = csv.writer(output_file)
for artist in os.listdir(root):
artist_dir = os.path.join(root, artist)
if not os.path.isdir(artist_dir):
continue
for release in os.listdir(os.path.join(root, artist)):
release_dir = os.path.join(artist_dir, release)
if not os.path.isdir(release_dir):
continue
match = release_re.match(release)
if not match:
continue
match_dict = match.groupdict()
csv_writer.writerow([
artist,
match_dict["title"],
match_dict["year"],
])