From 4a98996b9f10517ad915a20284f4c7b12fb01255 Mon Sep 17 00:00:00 2001 From: Oliver Matthews Date: Fri, 8 Nov 2019 12:13:34 +0000 Subject: [PATCH] add thing downloading --- README.md | 20 ++++++++++++++++++-- thingy_grabber.py | 30 +++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ddc207c..85f87d8 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,25 @@ Script for archiving thingiverse things. Due to this being a glorified webscraper, it's going to be very fragile. ## Usage: -`thingy_grabber.py [-v] user_name collection_name` +```` +thingy_grabber.py [-h] [-v] {collection,thing} ... +positional arguments: + {collection,thing} Type of thing to download + collection Download an entire collection + thing Download a single thing. + +optional arguments: + -h, --help show this help message and exit + -v, --verbose Be more verbose +```` +### Things +`thingy_grabber.py thing thingid` +This will create a directory named after the title of the thing with the given ID and download the files into it. + + +### Collections +`thingy_grabber collection user_name collection_name` Where `user_name` is the name of the creator of the collection (not nes. your name!) and `collection_name` is the name of the collection you want. This will create a series of directorys `user-collection/thing-name` for each thing in the collection. @@ -20,7 +37,6 @@ CAVEAT: This script will *not delete files*. So if there has been an update and ## Todo features (maybe): -- download a single thing - download things by designer - less perfunctory error checking / handling - attempt to use -failed dirs for resuming diff --git a/thingy_grabber.py b/thingy_grabber.py index 587d47e..570a1df 100755 --- a/thingy_grabber.py +++ b/thingy_grabber.py @@ -4,6 +4,7 @@ Thingiverse bulk downloader """ import re +import sys import os import argparse import unicodedata @@ -124,8 +125,8 @@ def download_thing(thing): last_time = None try: - with open('timestamp.txt', 'r') as fh: - last_time = fh.readlines()[0] + with open('timestamp.txt', 'r') as timestamp_handle: + last_time = timestamp_handle.readlines()[0] if VERBOSE: print("last downloaded version: {}".format(last_time)) except FileNotFoundError: @@ -159,8 +160,8 @@ def download_thing(thing): with open(name, 'wb') as handle: handle.write(data_req.content) # now write timestamp - with open('timestamp.txt', 'w') as fh: - fh.write(new_last_time) + with open('timestamp.txt', 'w') as timestamp_handle: + timestamp_handle.write(new_last_time) except Exception as exception: print("Failed to download {} - {}".format(name, exception)) os.chdir(base_dir) @@ -173,16 +174,27 @@ def download_thing(thing): def main(): """ Entry point for script being run as a command. """ parser = argparse.ArgumentParser() - parser.add_argument("owner", help="The owner of the collection to get") - parser.add_argument("collection", help="The name of the collection to get") parser.add_argument("-v", "--verbose", help="Be more verbose", action="store_true") + subparsers = parser.add_subparsers(help="Type of thing to download", dest="subcommand") + collection_parser = subparsers.add_parser('collection', help="Download an entire collection") + collection_parser.add_argument("owner", help="The owner of the collection to get") + collection_parser.add_argument("collection", help="The name of the collection to get") + thing_parser = subparsers.add_parser('thing', help="Download a single thing.") + thing_parser.add_argument("thing", help="Thing ID to download") + args = parser.parse_args() + if not args.subcommand: + parser.print_help() + sys.exit(1) global VERBOSE VERBOSE = args.verbose + if args.subcommand.startswith("collection"): + collection = Collection(args.owner, args.collection) + print(collection.get_collection()) + collection.download() + if args.subcommand == "thing": + download_thing(args.thing) - collection = Collection(args.owner, args.collection) - print(collection.get_collection()) - collection.download() if __name__ == "__main__": main() -- 2.20.1