From 4ff20e03039c9e4abda2adf838b3ed323c766532 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 28 Nov 2018 21:28:42 -0500 Subject: [PATCH] Add tool to fetch album art from the Cover Art Archive --- caa-fetcher.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 caa-fetcher.py diff --git a/caa-fetcher.py b/caa-fetcher.py new file mode 100755 index 0000000..5c23107 --- /dev/null +++ b/caa-fetcher.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# +# Copyright © 2018 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 sys +import os + +progname = "caa-fetcher" +version = "0" +musicbrainzngs.set_useragent(progname, version) + +parser = argparse.ArgumentParser( + description='Download album artwork from the Cover Art Archive' +) + +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 © 2018 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() + +# TODO: allow the naming scheme to be configured... +name_format = "{num:02d}" + +covers = musicbrainzngs.get_image_list(args.release_mbid) +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 = tempfile.NamedTemporaryFile(suffix=ext, dir=".") + 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) -- 2.43.0