#!/usr/bin/env python # # Copyright © 2018-2019 Nick Bowler # # Tool to fetch album art for a release from the Cover Art Archive. # # License WTFPL2: Do What The Fuck You Want To Public License, version 2. # This is free software: you are free to do what the fuck you want to. # There is NO WARRANTY, to the extent permitted by law. from pathlib import Path import musicbrainzngs import argparse import tempfile import urllib import sys import os import re umask = os.umask(0) os.umask(umask) progname = "caa-fetcher" version = "1" musicbrainzngs.set_useragent(progname, version) parser = argparse.ArgumentParser( description='Download album artwork from the Cover Art Archive' ) def errmsg(msg, prog=parser.prog): print("%s: %s" % (prog, msg), file=sys.stderr) # NamedTemporaryFile workalike which allows control of the file mode... def open_tmp(prefix="tmp", suffix="", dir=".", mode=0o600): names = tempfile._get_candidate_names() for seq in range(100): name = os.path.join(dir, "%s%s%s" % (prefix, next(names), suffix)) try: f = open(name, "x+", mode) except FileExistsError: continue return tempfile._TemporaryFileWrapper(f, name) return None # Given an arbitrary string, return the first substring that looks like an # mbid, or None if no such substring is found. def extract_mbid(arg): xdigit = r'[0-9abcdefABCDEF]' m = re.search(r'{0}{{8}}(-{0}{{4}}){{3}}-{0}{{12}}'.format(xdigit), arg) if m: return m.group() class VersionAction(argparse.Action): def __init__(self, **kw): super().__init__(nargs=0, help="show a version message and exit", **kw) def __call__(self, parser, namespace, values, option_string=None): print("%s %s" % (progname, version)) print('''Copyright © 2019 Nick Bowler License WTFPL2: Do What The Fuck You Want To Public License, version 2. This is free software: you are free to do what the fuck you want to. There is NO WARRANTY, to the extent permitted by law.''') sys.exit(0) parser.add_argument('--version', action=VersionAction) parser.add_argument('-o', '--output-directory', metavar='DIR', help='''downloaded files are written to DIR, which is created if it does not exists. Default: .''') parser.add_argument('-r', '--release-mbid', metavar='MBID', required=True) args = parser.parse_args() release_mbid = extract_mbid(args.release_mbid) if release_mbid is None: parser.error("invalid release MBID: %s" % (args.release_mbid)) # TODO: allow the naming scheme to be configured... name_format = "{num:02d}" try: covers = musicbrainzngs.get_image_list(release_mbid) except musicbrainzngs.ResponseError as e: errmsg("error: cannot retrieve image list for release") errmsg(e) if isinstance(e.cause, urllib.error.HTTPError) and e.cause.code == 404: errmsg("is %s a release MBID?" % (release_mbid)) sys.exit(1) if len(covers["images"]) == 0: print("release has no cover art, nothing to do") sys.exit() if args.output_directory: Path(args.output_directory).mkdir(parents=True, exist_ok=True) os.chdir(args.output_directory) failed = False for c in covers["images"]: (_,ext) = os.path.splitext(c["image"]) outname = (name_format + "{0}").format(ext, num = (covers["images"].index(c) + 1) ) if Path(outname).exists(): print("%s already exists, skipping..." % outname) continue outfile = open_tmp(suffix=ext, dir=".", mode=0o666) rc = os.spawnlp(os.P_WAIT, 'wget', 'wget', '-O', outfile.name, c["image"]) if rc: failed = True os.link(outfile.name, outname) outfile.close() if failed: sys.exit(1)