diff --git a/dl.py b/dl.py new file mode 100755 index 0000000..6b431e7 --- /dev/null +++ b/dl.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +""" +Use cURL and ImageMagick to download and extract a model zip-file. + +Downloads the URL (which must be a zip-file) and extracts it to +model/.obj and model/.bmp. + +If the zip-file contains a README, it is printed to standard output. +If the zip-file contains a PNG file, it is converted to BMP using +the convert command from ImageMagick. + +If the zip-file contains multiple .obj or image files, +the last one is used. +""" + +import argparse +import os +import shutil +import subprocess +import sys +import tempfile +import zipfile + + +parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter +) +parser.add_argument("url") +parser.add_argument("name") + + +def main(): + args = parser.parse_args() + with tempfile.NamedTemporaryFile() as fp: + subprocess.check_call(("curl", "-Lo", fp.name, args.url)) + zf = zipfile.ZipFile(fp.name) + for zi in zf.infolist(): # type: zipfile.ZipInfo + if zi.filename.endswith(".obj"): + targetpath = os.path.join("model", args.name + ".obj") + with zf.open(zi) as source, open(targetpath, "wb") as target: + shutil.copyfileobj(source, target) + elif "readme" in zi.filename.lower(): + with zf.open(zi) as source: + shutil.copyfileobj(source, sys.stdout.buffer) + print("") + elif zi.filename.endswith(".bmp"): + targetpath = os.path.join("model", args.name + ".bmp") + with zf.open(zi) as source, open(targetpath, "wb") as target: + shutil.copyfileobj(source, target) + elif zi.filename.endswith(".png"): + targetpath = os.path.join("model", args.name + ".bmp") + target = open(targetpath, "wb") + with target, zf.open(zi) as source: + proc = subprocess.Popen( + ("convert", "png:-", "bmp:-"), + stdin=subprocess.PIPE, + stdout=target, + ) + with proc: + shutil.copyfileobj(source, proc.stdin) + proc.stdin.close() + proc.wait() + + +if __name__ == "__main__": + main() diff --git a/dl.sh b/dl.sh new file mode 100755 index 0000000..09dab1a --- /dev/null +++ b/dl.sh @@ -0,0 +1,7 @@ +#!/bin/sh +python3 dl.py https://www.models-resource.com/download/593/ salesman +python3 dl.py https://www.models-resource.com/download/4909/ dk +python3 dl.py https://www.models-resource.com/download/677/ gohma +python3 dl.py https://www.models-resource.com/download/4956/ charizard +python3 dl.py https://www.models-resource.com/download/470/ saria +python3 dl.py https://www.models-resource.com/download/689/ sheik diff --git a/model/.empty b/model/.empty new file mode 100644 index 0000000..e69de29