3 # Copyright © 2018-2019 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
21 progname = "caa-fetcher"
23 musicbrainzngs.set_useragent(progname, version)
25 parser = argparse.ArgumentParser(
26 description='Download album artwork from the Cover Art Archive'
29 # NamedTemporaryFile workalike which allows control of the file mode...
30 def open_tmp(prefix="tmp", suffix="", dir=".", mode=0o600):
31 names = tempfile._get_candidate_names()
32 for seq in range(100):
33 name = os.path.join(dir, "%s%s%s" % (prefix, next(names), suffix))
35 f = open(name, "x+", mode)
36 except FileExistsError:
38 return tempfile._TemporaryFileWrapper(f, name)
41 class VersionAction(argparse.Action):
42 def __init__(self, **kw):
43 super().__init__(nargs=0, help="show a version message and exit", **kw)
44 def __call__(self, parser, namespace, values, option_string=None):
45 print("%s %s" % (progname, version))
46 print('''Copyright © 2019 Nick Bowler
47 License WTFPL2: Do What The Fuck You Want To Public License, version 2.
48 This is free software: you are free to do what the fuck you want to.
49 There is NO WARRANTY, to the extent permitted by law.''')
51 parser.add_argument('--version', action=VersionAction)
53 parser.add_argument('-o', '--output-directory', metavar='DIR',
54 help='''downloaded files are written to DIR, which is
55 created if it does not exists. Default: .''')
56 parser.add_argument('-r', '--release-mbid', metavar='MBID', required=True)
57 args = parser.parse_args()
59 # TODO: allow the naming scheme to be configured...
60 name_format = "{num:02d}"
62 covers = musicbrainzngs.get_image_list(args.release_mbid)
63 if len(covers["images"]) == 0:
64 print("release has no cover art, nothing to do")
67 if args.output_directory:
68 Path(args.output_directory).mkdir(parents=True, exist_ok=True)
69 os.chdir(args.output_directory)
72 for c in covers["images"]:
73 (_,ext) = os.path.splitext(c["image"])
74 outname = (name_format + "{0}").format(ext,
75 num = (covers["images"].index(c) + 1)
78 if Path(outname).exists():
79 print("%s already exists, skipping..." % outname)
82 outfile = open_tmp(suffix=ext, dir=".", mode=0o666)
83 rc = os.spawnlp(os.P_WAIT, 'wget', 'wget', '-O', outfile.name, c["image"])
87 os.link(outfile.name, outname)