3 # Copyright © 2018 Nick Bowler
5 # Tool to fetch album art for a release from the Cover Art Archive.
7 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
8 # This is free software: you are free to do what the fuck you want to.
9 # There is NO WARRANTY, to the extent permitted by law.
11 from pathlib import Path
18 progname = "caa-fetcher"
20 musicbrainzngs.set_useragent(progname, version)
22 parser = argparse.ArgumentParser(
23 description='Download album artwork from the Cover Art Archive'
26 class VersionAction(argparse.Action):
27 def __init__(self, **kw):
28 super().__init__(nargs=0, help="show a version message and exit", **kw)
29 def __call__(self, parser, namespace, values, option_string=None):
30 print("%s %s" % (progname, version))
31 print('''Copyright © 2018 Nick Bowler
32 License WTFPL2: Do What The Fuck You Want To Public License, version 2.
33 This is free software: you are free to do what the fuck you want to.
34 There is NO WARRANTY, to the extent permitted by law.''')
36 parser.add_argument('--version', action=VersionAction)
38 parser.add_argument('-o', '--output-directory', metavar='DIR',
39 help='''downloaded files are written to DIR, which is
40 created if it does not exists. Default: .''')
41 parser.add_argument('-r', '--release-mbid', metavar='MBID', required=True)
42 args = parser.parse_args()
44 # TODO: allow the naming scheme to be configured...
45 name_format = "{num:02d}"
47 covers = musicbrainzngs.get_image_list(args.release_mbid)
48 if len(covers["images"]) == 0:
49 print("release has no cover art, nothing to do")
52 if args.output_directory:
53 Path(args.output_directory).mkdir(parents=True, exist_ok=True)
54 os.chdir(args.output_directory)
57 for c in covers["images"]:
58 (_,ext) = os.path.splitext(c["image"])
59 outname = (name_format + "{0}").format(ext,
60 num = (covers["images"].index(c) + 1)
63 if Path(outname).exists():
64 print("%s already exists, skipping..." % outname)
67 outfile = tempfile.NamedTemporaryFile(suffix=ext, dir=".")
68 rc = os.spawnlp(os.P_WAIT, 'wget', 'wget', '-O', outfile.name, c["image"])
71 os.link(outfile.name, outname)