Add batch support
authorOliver Matthews <oliver@codersoffortune.net>
Thu, 28 Nov 2019 12:41:40 +0000 (12:41 +0000)
committerOliver Matthews <oliver@codersoffortune.net>
Thu, 28 Nov 2019 12:41:40 +0000 (12:41 +0000)
README.md
thingy_grabber.py

index 98c188c..b691766 100644 (file)
--- a/README.md
+++ b/README.md
@@ -3,14 +3,15 @@ Script for archiving thingiverse things. Due to this being a glorified webscrape
 
 ## Usage:
 ````
-usage: thingy_grabber.py [-h] [-l {debug,info,warning}] [-d DIRECTORY] {collection,thing,user,version} ...
+usage: thingy_grabber.py [-h] [-l {debug,info,warning}] [-d DIRECTORY] {collection,thing,user,batch,version} ...
 
 positional arguments:
-  {collection,thing,user,version}
+  {collection,thing,user,batch,version}
                         Type of thing to download
     collection          Download an entire collection
     thing               Download a single thing.
     user                Download all things by a user
+    batch               Perform multiple actions written in a text file
     version             Show the current version
 
 optional arguments:
@@ -41,6 +42,21 @@ This will create a series of directories `user designs/thing-name` for each thin
 
 If for some reason a download fails, it will get moved sideways to `thing-name-failed` - this way if you rerun it, it will only reattmpt any failed things.
 
+### Batch mode
+`thingy_grabber.py batch batch_file`
+This will load a given text file and parse it as a series of calls to this script. The script should be of the form `command arg1 ...`.
+Be warned that there is currently NO validation that you have given a correct set of commands!
+
+An example:
+````
+thing 3670144
+collection cwoac bike
+user cwoac
+````
+
+If you are using linux, you can just add an appropriate call to the crontab. If you are using windows, it's a bit more of a faff, but at least according to [https://www.technipages.com/scheduled-task-windows](this link), you should be able to with a command something like this (this is not tested!): `schtasks /create /tn thingy_grabber /tr "c:\path\to\thingy_grabber.py -d c:\path\to\output\directory batch c:\path\to\batchfile.txt" /sc weekly /d wed /st 13:00:00`
+You may have to play with the quotation marks to make that work though.
+
 ## Requirements
 python3, beautifulsoup4, requests, lxml
 
@@ -52,13 +68,15 @@ python3, beautifulsoup4, requests, lxml
 ## Changelog
 * v0.5.0
   - better logging options
+  - batch mode
 * v0.4.0
   - Added a changelog
   - Now download associated images
   - support `-d` to specify base download directory 
 
 ## Todo features (maybe):
-- better batch mode
+- log to file support
 - less perfunctory error checking / handling
 - attempt to use -failed dirs for resuming
+- gui?
 
index 5d5a451..7ed013d 100755 (executable)
@@ -322,6 +322,27 @@ class Thing:
         self._needs_download = False
         logging.debug("Download of {} finished".format(self.title))
 
+def do_batch(batch_file, download_dir):
+    """ Read a file in line by line, parsing each as a set of calls to this script."""
+    with open(batch_file) as handle:
+        for line in handle:
+            line = line.strip()
+            logging.info("Handling instruction {}".format(line))
+            command_arr = line.split()
+            if command_arr[0] == "thing":
+                logging.debug("Handling batch thing instruction: {}".format(line))
+                Thing(command_arr[1]).download(download_dir)
+                continue
+            if command_arr[0] == "collection":
+                logging.debug("Handling batch collection instruction: {}".format(line))
+                Collection(command_arr[1], command_arr[2], download_dir).download()
+                continue
+            if command_arr[0] == "user":
+                logging.debug("Handling batch collection instruction: {}".format(line))
+                Designs(command_arr[1], download_dir).download()
+                continue
+            logging.warning("Unable to parse current instruction. Skipping.")
+
 def main():
     """ Entry point for script being run as a command. """
     parser = argparse.ArgumentParser()
@@ -335,6 +356,8 @@ def main():
     thing_parser.add_argument("thing", help="Thing ID to download")
     user_parser = subparsers.add_parser("user", help="Download all things by a user")
     user_parser.add_argument("user", help="The user to get the designs of")
+    batch_parser = subparsers.add_parser("batch", help="Perform multiple actions written in a text file")
+    batch_parser.add_argument("batch_file", help="The name of the file to read.")
     subparsers.add_parser("version", help="Show the current version")
 
     args = parser.parse_args()
@@ -347,15 +370,16 @@ def main():
 
 
     if args.subcommand.startswith("collection"):
-        collection = Collection(args.owner, args.collection, args.directory)
-        collection.download()
+        Collection(args.owner, args.collection, args.directory).download()
     if args.subcommand == "thing":
         Thing(args.thing).download(args.directory)
     if args.subcommand == "user":
-        designs = Designs(args.user, args.directory)
-        designs.download()
+        Designs(args.user, args.directory).download()
     if args.subcommand == "version":
         print("thingy_grabber.py version {}".format(VERSION))
+    if args.subcommand == "batch":
+        do_batch(args.batch_file, args.directory)
+
 
 if __name__ == "__main__":
     main()