3 Thingiverse bulk downloader
13 import multiprocessing
15 from shutil
import copyfile
16 from bs4
import BeautifulSoup
18 URL_BASE
= "https://www.thingiverse.com"
19 URL_COLLECTION
= URL_BASE
+ "/ajax/thingcollection/list_collected_things"
20 USER_COLLECTION
= URL_BASE
+ "/ajax/user/designs"
22 ID_REGEX
= re
.compile(r
'"id":(\d*),')
23 TOTAL_REGEX
= re
.compile(r
'"total":(\d*),')
24 LAST_PAGE_REGEX
= re
.compile(r
'"last_page":(\d*),')
25 # This appears to be fixed at 12, but if it changes would screw the rest up.
26 PER_PAGE_REGEX
= re
.compile(r
'"per_page":(\d*),')
27 NO_WHITESPACE_REGEX
= re
.compile(r
'[-\s]+')
34 class State(enum
.Enum
):
37 ALREADY_DOWNLOADED
= enum
.auto()
41 """ Remove whitespace from a string """
42 return str(NO_WHITESPACE_REGEX
.sub('-', value
))
47 Normalizes string, converts to lowercase, removes non-alpha characters,
48 and converts spaces to hyphens.
50 value
= unicodedata
.normalize('NFKD', value
).encode(
51 'ascii', 'ignore').decode()
52 value
= str(re
.sub(r
'[^\w\s-]', '', value
).strip())
53 value
= str(NO_WHITESPACE_REGEX
.sub('-', value
))
54 #value = str(re.sub(r'[-\s]+', '-', value))
57 class Downloader(multiprocessing
.Process
):
59 Class to handle downloading the things we have found to get.
62 def __init__(self
, thing_queue
, download_directory
):
63 multiprocessing
.Process
.__init
__(self
)
64 # TODO: add parameters
65 self
.thing_queue
= thing_queue
66 self
.download_directory
= download_directory
69 """ actual download loop.
72 thing_id
= self
.thing_queue
.get()
74 logging
.info("Shutting download queue")
75 self
.thing_queue
.task_done()
77 logging
.info("Handling id {}".format(thing_id
))
78 Thing(thing_id
).download(self
.download_directory
)
79 self
.thing_queue
.task_done()
87 """ Holds details of a group of things for download
88 This is effectively (although not actually) an abstract class
89 - use Collection or Designs instead.
92 def __init__(self
, quick
):
98 # Should we stop downloading when we hit a known datestamp?
100 # These should be set by child classes.
102 self
.download_dir
= None
103 self
.collection_url
= None
105 def _get_small_grouping(self
, req
):
106 """ Handle small groupings """
107 soup
= BeautifulSoup(req
.text
, features
='lxml')
108 links
= soup
.find_all('a', {'class': 'card-img-holder'})
109 self
.things
= [x
['href'].split(':')[1] for x
in links
]
110 self
.total
= len(self
.things
)
115 """ retrieve the things of the grouping. """
117 # We've already done it.
120 # Check for initialisation:
122 logging
.error("No URL set - object not initialised properly?")
123 raise ValueError("No URL set - object not initialised properly?")
125 # Get the internal details of the grouping.
126 logging
.debug("Querying {}".format(self
.url
))
127 c_req
= requests
.get(self
.url
)
128 total
= TOTAL_REGEX
.search(c_req
.text
)
130 # This is a small (<13) items grouping. Pull the list from this req.
131 return self
._get
_small
_grouping
(c_req
)
132 self
.total
= total
.groups()[0]
133 self
.req_id
= ID_REGEX
.search(c_req
.text
).groups()[0]
134 self
.last_page
= int(LAST_PAGE_REGEX
.search(c_req
.text
).groups()[0])
135 self
.per_page
= PER_PAGE_REGEX
.search(c_req
.text
).groups()[0]
137 'base_url': self
.url
,
142 for current_page
in range(1, self
.last_page
+ 1):
143 parameters
['page'] = current_page
144 req
= requests
.post(self
.collection_url
, parameters
)
145 soup
= BeautifulSoup(req
.text
, features
='lxml')
146 links
= soup
.find_all('a', {'class': 'card-img-holder'})
147 self
.things
+= [x
['href'].split(':')[1] for x
in links
]
152 """ Downloads all the files in a collection """
156 if not self
.download_dir
:
158 "No download_dir set - invalidly initialised object?")
160 base_dir
= os
.getcwd()
162 os
.mkdir(self
.download_dir
)
163 except FileExistsError
:
164 logging
.info("Target directory {} already exists. Assuming a resume."
165 .format(self
.download_dir
))
166 logging
.info("Downloading {} thing(s).".format(self
.total
))
167 for idx
, thing
in enumerate(self
.things
):
168 logging
.info("Downloading thing {}".format(idx
))
169 RC
= Thing(thing
).download(self
.download_dir
)
170 if self
.quick
and RC
==State
.ALREADY_DOWNLOADED
:
171 logging
.info("Caught up, stopping.")
175 class Collection(Grouping
):
176 """ Holds details of a collection. """
178 def __init__(self
, user
, name
, directory
, quick
):
179 Grouping
.__init
__(self
, quick
)
182 self
.url
= "{}/{}/collections/{}".format(
183 URL_BASE
, self
.user
, strip_ws(self
.name
))
184 self
.download_dir
= os
.path
.join(directory
,
185 "{}-{}".format(slugify(self
.user
), slugify(self
.name
)))
186 self
.collection_url
= URL_COLLECTION
189 class Designs(Grouping
):
190 """ Holds details of all of a users' designs. """
192 def __init__(self
, user
, directory
, quick
):
193 Grouping
.__init
__(self
, quick
)
195 self
.url
= "{}/{}/designs".format(URL_BASE
, self
.user
)
196 self
.download_dir
= os
.path
.join(
197 directory
, "{} designs".format(slugify(self
.user
)))
198 self
.collection_url
= USER_COLLECTION
202 """ An individual design on thingiverse. """
204 def __init__(self
, thing_id
):
205 self
.thing_id
= thing_id
206 self
.last_time
= None
208 self
._needs
_download
= True
211 self
.download_dir
= None
213 def _parse(self
, base_dir
):
214 """ Work out what, if anything needs to be done. """
218 url
= "{}/thing:{}/files".format(URL_BASE
, self
.thing_id
)
220 req
= requests
.get(url
)
221 except requests
.exceptions
.ConnectionError
as error
:
222 logging
.error("Unable to connect for thing {}: {}".format(
223 self
.thing_id
, error
))
227 soup
= BeautifulSoup(self
.text
, features
='lxml')
229 #code.interact(local=dict(globals(), **locals()))
231 self
.title
= slugify(soup
.find_all('h1')[0].text
.strip())
234 "No title found for thing {}".format(self
.thing_id
))
235 self
.title
= self
.thing_id
237 if req
.status_code
== 404:
239 "404 for thing {} - DMCA or invalid number?".format(self
.thing_id
))
242 if req
.status_code
> 299:
244 "bad status code {} for thing {} - try again later?".format(req
.status_code
, self
.thing_id
))
247 self
.download_dir
= os
.path
.join(base_dir
, self
.title
)
249 logging
.debug("Parsing {} ({})".format(self
.thing_id
, self
.title
))
251 if not os
.path
.exists(self
.download_dir
):
256 timestamp_file
= os
.path
.join(self
.download_dir
, 'timestamp.txt')
257 if not os
.path
.exists(timestamp_file
):
258 # Old download from before
260 "Old-style download directory found. Assuming update required.")
265 with
open(timestamp_file
, 'r') as timestamp_handle
:
266 self
.last_time
= timestamp_handle
.readlines()[0]
267 logging
.info("last downloaded version: {}".format(self
.last_time
))
268 except FileNotFoundError
:
269 # Not run on this thing before.
271 "Old-style download directory found. Assuming update required.")
272 self
.last_time
= None
276 # OK, so we have a timestamp, lets see if there is anything new to get
277 file_links
= soup
.find_all('a', {'class': 'file-download'})
278 for file_link
in file_links
:
279 timestamp
= file_link
.find_all('time')[0]['datetime']
280 logging
.debug("Checking {} (updated {})".format(
281 file_link
["title"], timestamp
))
282 if timestamp
> self
.last_time
:
284 "Found new/updated file {}".format(file_link
["title"]))
285 self
._needs
_download
= True
288 # Got here, so nope, no new files.
289 self
._needs
_download
= False
292 def download(self
, base_dir
):
293 """ Download all files for a given thing.
294 Returns True iff the thing is now downloaded (not iff it downloads the thing!)
297 self
._parse
(base_dir
)
301 "Unable to parse {} - aborting download".format(self
.thing_id
))
304 if not self
._needs
_download
:
305 print("{} - {} already downloaded - skipping.".format(self
.thing_id
, self
.title
))
306 return State
.ALREADY_DOWNLOADED
308 # Have we already downloaded some things?
309 timestamp_file
= os
.path
.join(self
.download_dir
, 'timestamp.txt')
311 if os
.path
.exists(self
.download_dir
):
312 if not os
.path
.exists(timestamp_file
):
313 # edge case: old style dir w/out timestamp.
315 "Old style download dir found for {}".format(self
.title
))
317 target_dir
= "{}_old".format(self
.download_dir
)
318 while os
.path
.exists(target_dir
):
319 prev_count
= prev_count
+ 1
320 target_dir
= "{}_old_{}".format(self
.download_dir
, prev_count
)
321 os
.rename(self
.download_dir
, target_dir
)
323 prev_dir
= "{}_{}".format(self
.download_dir
, self
.last_time
)
324 os
.rename(self
.download_dir
, prev_dir
)
326 # Get the list of files to download
327 soup
= BeautifulSoup(self
.text
, features
='lxml')
328 file_links
= soup
.find_all('a', {'class': 'file-download'})
334 if not self
.last_time
:
335 # If we don't have anything to copy from, then it is all new.
336 new_file_links
= file_links
338 new_last_time
= file_links
[0].find_all('time')[0]['datetime']
341 code
.interact(local
=dict(globals(), **locals()))
343 for file_link
in file_links
:
344 timestamp
= file_link
.find_all('time')[0]['datetime']
345 logging
.debug("Found file {} from {}".format(
346 file_link
["title"], timestamp
))
347 if timestamp
> new_last_time
:
348 new_last_time
= timestamp
350 for file_link
in file_links
:
351 timestamp
= file_link
.find_all('time')[0]['datetime']
352 logging
.debug("Checking {} (updated {})".format(
353 file_link
["title"], timestamp
))
354 if timestamp
> self
.last_time
:
355 new_file_links
.append(file_link
)
357 old_file_links
.append(file_link
)
358 if not new_last_time
or timestamp
> new_last_time
:
359 new_last_time
= timestamp
361 logging
.debug("new timestamp {}".format(new_last_time
))
363 # OK. Time to get to work.
364 logging
.debug("Generating download_dir")
365 os
.mkdir(self
.download_dir
)
366 # First grab the cached files (if any)
367 logging
.info("Copying {} unchanged files.".format(len(old_file_links
)))
368 for file_link
in old_file_links
:
369 old_file
= os
.path
.join(prev_dir
, file_link
["title"])
370 new_file
= os
.path
.join(self
.download_dir
, file_link
["title"])
372 logging
.debug("Copying {} to {}".format(old_file
, new_file
))
373 copyfile(old_file
, new_file
)
374 except FileNotFoundError
:
376 "Unable to find {} in old archive, redownloading".format(file_link
["title"]))
377 new_file_links
.append(file_link
)
379 # Now download the new ones
380 files
= [("{}{}".format(URL_BASE
, x
['href']), x
["title"])
381 for x
in new_file_links
]
382 logging
.info("Downloading {} new files of {}".format(
383 len(new_file_links
), len(file_links
)))
385 for url
, name
in files
:
386 file_name
= os
.path
.join(self
.download_dir
, name
)
387 logging
.debug("Downloading {} from {} to {}".format(
388 name
, url
, file_name
))
389 data_req
= requests
.get(url
)
390 with
open(file_name
, 'wb') as handle
:
391 handle
.write(data_req
.content
)
392 except Exception as exception
:
393 logging
.error("Failed to download {} - {}".format(name
, exception
))
394 os
.rename(self
.download_dir
, "{}_failed".format(self
.download_dir
))
398 image_dir
= os
.path
.join(self
.download_dir
, 'images')
399 imagelinks
= soup
.find_all('span', {'class': 'gallery-slider'})[0] \
400 .find_all('div', {'class': 'gallery-photo'})
401 logging
.info("Downloading {} images.".format(len(imagelinks
)))
404 for imagelink
in imagelinks
:
405 url
= next(filter(None, [imagelink
[x
] for x
in ['data-full',
408 'data-thumb']]), None)
411 "Unable to find any urls for {}".format(imagelink
))
414 filename
= os
.path
.basename(url
)
415 if filename
.endswith('stl'):
416 filename
= "{}.png".format(filename
)
417 image_req
= requests
.get(url
)
418 with
open(os
.path
.join(image_dir
, filename
), 'wb') as handle
:
419 handle
.write(image_req
.content
)
420 except Exception as exception
:
421 print("Failed to download {} - {}".format(filename
, exception
))
422 os
.rename(self
.download_dir
, "{}_failed".format(self
.download_dir
))
425 # instructions are good too.
426 logging
.info("Downloading readme")
428 readme_txt
= soup
.find('meta', property='og:description')[
430 with
open(os
.path
.join(self
.download_dir
, 'readme.txt'), 'w') as readme_handle
:
431 readme_handle
.write("{}\n".format(readme_txt
))
432 except (TypeError, KeyError) as exception
:
433 logging
.warning("No readme? {}".format(exception
))
434 except IOError as exception
:
435 logging
.warning("Failed to write readme! {}".format(exception
))
437 # Best get some licenses
438 logging
.info("Downloading license")
440 license_txt
= soup
.find('div', {'class': 'license-text'}).text
442 with
open(os
.path
.join(self
.download_dir
, 'license.txt'), 'w') as license_handle
:
443 license_handle
.write("{}\n".format(license_txt
))
444 except AttributeError as exception
:
445 logging
.warning("No license? {}".format(exception
))
446 except IOError as exception
:
447 logging
.warning("Failed to write license! {}".format(exception
))
450 # Now write the timestamp
451 with
open(timestamp_file
, 'w') as timestamp_handle
:
452 timestamp_handle
.write(new_last_time
)
453 except Exception as exception
:
454 print("Failed to write timestamp file - {}".format(exception
))
455 os
.rename(self
.download_dir
, "{}_failed".format(self
.download_dir
))
457 self
._needs
_download
= False
458 logging
.debug("Download of {} finished".format(self
.title
))
462 def do_batch(batch_file
, download_dir
, quick
):
463 """ Read a file in line by line, parsing each as a set of calls to this script."""
464 with
open(batch_file
) as handle
:
467 logging
.info("Handling instruction {}".format(line
))
468 command_arr
= line
.split()
469 if command_arr
[0] == "thing":
471 "Handling batch thing instruction: {}".format(line
))
472 Thing(command_arr
[1]).download(download_dir
)
474 if command_arr
[0] == "collection":
476 "Handling batch collection instruction: {}".format(line
))
477 Collection(command_arr
[1], command_arr
[2],
478 download_dir
, quick
).download()
480 if command_arr
[0] == "user":
482 "Handling batch collection instruction: {}".format(line
))
483 Designs(command_arr
[1], download_dir
, quick
).download()
485 logging
.warning("Unable to parse current instruction. Skipping.")
489 """ Entry point for script being run as a command. """
490 parser
= argparse
.ArgumentParser()
491 parser
.add_argument("-l", "--log-level", choices
=[
492 'debug', 'info', 'warning'], default
='info', help="level of logging desired")
493 parser
.add_argument("-d", "--directory",
494 help="Target directory to download into")
495 parser
.add_argument("-f", "--log-file",
496 help="Place to log debug information to")
497 parser
.add_argument("-q", "--quick", action
="store_true",
498 help="Assume date ordering on posts")
500 subparsers
= parser
.add_subparsers(
501 help="Type of thing to download", dest
="subcommand")
502 collection_parser
= subparsers
.add_parser(
503 'collection', help="Download one or more entire collection(s)")
504 collection_parser
.add_argument(
505 "owner", help="The owner of the collection(s) to get")
506 collection_parser
.add_argument(
507 "collections", nargs
="+", help="Space seperated list of the name(s) of collection to get")
508 thing_parser
= subparsers
.add_parser(
509 'thing', help="Download a single thing.")
510 thing_parser
.add_argument(
511 "things", nargs
="*", help="Space seperated list of thing ID(s) to download")
512 user_parser
= subparsers
.add_parser(
513 "user", help="Download all things by one or more users")
514 user_parser
.add_argument(
515 "users", nargs
="+", help="A space seperated list of the user(s) to get the designs of")
516 batch_parser
= subparsers
.add_parser(
517 "batch", help="Perform multiple actions written in a text file")
518 batch_parser
.add_argument(
519 "batch_file", help="The name of the file to read.")
520 subparsers
.add_parser("version", help="Show the current version")
522 args
= parser
.parse_args()
523 if not args
.subcommand
:
526 if not args
.directory
:
527 args
.directory
= os
.getcwd()
529 logger
= logging
.getLogger()
530 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
531 logger
.setLevel(logging
.DEBUG
)
532 console_handler
= logging
.StreamHandler()
533 console_handler
.setLevel(args
.log_level
.upper())
535 logger
.addHandler(console_handler
)
537 file_handler
= logging
.FileHandler(args
.log_file
)
538 file_handler
.setLevel(logging
.DEBUG
)
539 file_handler
.setFormatter(formatter
)
540 logger
.addHandler(file_handler
)
544 thing_queue
= multiprocessing
.JoinableQueue()
545 logging
.debug("starting {} downloader(s)".format(DOWNLOADER_COUNT
))
546 downloaders
= [Downloader(thing_queue
, args
.directory
) for _
in range(DOWNLOADER_COUNT
)]
547 for downloader
in downloaders
:
551 if args
.subcommand
.startswith("collection"):
552 for collection
in args
.collections
:
553 Collection(args
.owner
, collection
, args
.directory
, args
.quick
).download()
554 if args
.subcommand
== "thing":
555 for thing
in args
.things
:
556 thing_queue
.put(thing
)
557 if args
.subcommand
== "user":
558 for user
in args
.users
:
559 Designs(user
, args
.directory
, args
.quick
).download()
560 if args
.subcommand
== "version":
561 print("thingy_grabber.py version {}".format(VERSION
))
562 if args
.subcommand
== "batch":
563 do_batch(args
.batch_file
, args
.directory
, args
.quick
)
565 # Stop the downloader processes
566 for downloader
in downloaders
:
567 thing_queue
.put(None)
569 if __name__
== "__main__":