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.
## Todo features (maybe):
-- download a single thing
- download things by designer
- less perfunctory error checking / handling
- attempt to use -failed dirs for resuming
"""
import re
+import sys
import os
import argparse
import unicodedata
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:
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)
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()