3 Thingiverse bulk downloader
13 from shutil
import copyfile
14 from bs4
import BeautifulSoup
16 URL_BASE
= "https://www.thingiverse.com"
17 URL_COLLECTION
= URL_BASE
+ "/ajax/thingcollection/list_collected_things"
18 USER_COLLECTION
= URL_BASE
+ "/ajax/user/designs"
20 ID_REGEX
= re
.compile(r
'"id":(\d*),')
21 TOTAL_REGEX
= re
.compile(r
'"total":(\d*),')
22 LAST_PAGE_REGEX
= re
.compile(r
'"last_page":(\d*),')
23 # This appears to be fixed at 12, but if it changes would screw the rest up.
24 PER_PAGE_REGEX
= re
.compile(r
'"per_page":(\d*),')
25 NO_WHITESPACE_REGEX
= re
.compile(r
'[-\s]+')
30 """ Remove whitespace from a string """
31 return str(NO_WHITESPACE_REGEX
.sub('-', value
))
36 Normalizes string, converts to lowercase, removes non-alpha characters,
37 and converts spaces to hyphens.
39 value
= unicodedata
.normalize('NFKD', value
).encode(
40 'ascii', 'ignore').decode()
41 value
= str(re
.sub(r
'[^\w\s-]', '', value
).strip())
42 value
= str(NO_WHITESPACE_REGEX
.sub('-', value
))
43 #value = str(re.sub(r'[-\s]+', '-', value))
48 """ Holds details of a group of things for download
49 This is effectively (although not actually) an abstract class
50 - use Collection or Designs instead.
59 # These should be set by child classes.
61 self
.download_dir
= None
62 self
.collection_url
= None
64 def _get_small_grouping(self
, req
):
65 """ Handle small groupings """
66 soup
= BeautifulSoup(req
.text
, features
='lxml')
67 links
= soup
.find_all('a', {'class': 'card-img-holder'})
68 self
.things
= [x
['href'].split(':')[1] for x
in links
]
69 self
.total
= len(self
.things
)
74 """ retrieve the things of the grouping. """
76 # We've already done it.
79 # Check for initialisation:
81 logging
.error("No URL set - object not initialised properly?")
82 raise ValueError("No URL set - object not initialised properly?")
84 # Get the internal details of the grouping.
85 logging
.debug("Querying {}".format(self
.url
))
86 c_req
= requests
.get(self
.url
)
87 total
= TOTAL_REGEX
.search(c_req
.text
)
89 # This is a small (<13) items grouping. Pull the list from this req.
90 return self
._get
_small
_grouping
(c_req
)
91 self
.total
= total
.groups()[0]
92 self
.req_id
= ID_REGEX
.search(c_req
.text
).groups()[0]
93 self
.last_page
= int(LAST_PAGE_REGEX
.search(c_req
.text
).groups()[0])
94 self
.per_page
= PER_PAGE_REGEX
.search(c_req
.text
).groups()[0]
101 for current_page
in range(1, self
.last_page
+ 1):
102 parameters
['page'] = current_page
103 req
= requests
.post(self
.collection_url
, parameters
)
104 soup
= BeautifulSoup(req
.text
, features
='lxml')
105 links
= soup
.find_all('a', {'class': 'card-img-holder'})
106 self
.things
+= [x
['href'].split(':')[1] for x
in links
]
111 """ Downloads all the files in a collection """
115 if not self
.download_dir
:
117 "No download_dir set - invalidly initialised object?")
119 base_dir
= os
.getcwd()
121 os
.mkdir(self
.download_dir
)
122 except FileExistsError
:
123 logging
.info("Target directory {} already exists. Assuming a resume."
124 .format(self
.download_dir
))
125 logging
.info("Downloading {} thing(s).".format(self
.total
))
126 for idx
, thing
in enumerate(self
.things
):
127 logging
.info("Downloading thing {}".format(idx
))
128 Thing(thing
).download(self
.download_dir
)
131 class Collection(Grouping
):
132 """ Holds details of a collection. """
134 def __init__(self
, user
, name
, directory
):
135 Grouping
.__init
__(self
)
138 self
.url
= "{}/{}/collections/{}".format(
139 URL_BASE
, self
.user
, strip_ws(self
.name
))
140 self
.download_dir
= os
.path
.join(directory
,
141 "{}-{}".format(slugify(self
.user
), slugify(self
.name
)))
142 self
.collection_url
= URL_COLLECTION
145 class Designs(Grouping
):
146 """ Holds details of all of a users' designs. """
148 def __init__(self
, user
, directory
):
149 Grouping
.__init
__(self
)
151 self
.url
= "{}/{}/designs".format(URL_BASE
, self
.user
)
152 self
.download_dir
= os
.path
.join(
153 directory
, "{} designs".format(slugify(self
.user
)))
154 self
.collection_url
= USER_COLLECTION
158 """ An individual design on thingiverse. """
160 def __init__(self
, thing_id
):
161 self
.thing_id
= thing_id
162 self
.last_time
= None
164 self
._needs
_download
= True
167 self
.download_dir
= None
169 def _parse(self
, base_dir
):
170 """ Work out what, if anything needs to be done. """
174 url
= "{}/thing:{}/files".format(URL_BASE
, self
.thing_id
)
176 req
= requests
.get(url
)
177 except requests
.exceptions
.ConnectionError
as error
:
178 logging
.error("Unable to connect for thing {}: {}".format(self
.thing_id
, error
))
182 soup
= BeautifulSoup(self
.text
, features
='lxml')
184 #code.interact(local=dict(globals(), **locals()))
186 self
.title
= slugify(soup
.find_all('h1')[0].text
.strip())
188 logging
.warning("No title found for thing {}".format(self
.thing_id
))
189 self
.title
= self
.thing_id
191 if req
.status_code
== 404:
192 logging
.warning("404 for thing {} - DMCA or invalid number?".format(self
.thing_id
))
195 if req
.status_code
> 299:
196 logging
.warning("bad status code {} for thing {} - try again later?".format(req
.status_code
, self
.thing_id
))
199 self
.download_dir
= os
.path
.join(base_dir
, self
.title
)
201 logging
.debug("Parsing {} ({})".format(self
.thing_id
, self
.title
))
203 if not os
.path
.exists(self
.download_dir
):
208 timestamp_file
= os
.path
.join(self
.download_dir
, 'timestamp.txt')
209 if not os
.path
.exists(timestamp_file
):
210 # Old download from before
212 "Old-style download directory found. Assuming update required.")
217 with
open(timestamp_file
, 'r') as timestamp_handle
:
218 self
.last_time
= timestamp_handle
.readlines()[0]
219 logging
.info("last downloaded version: {}".format(self
.last_time
))
220 except FileNotFoundError
:
221 # Not run on this thing before.
223 "Old-style download directory found. Assuming update required.")
224 self
.last_time
= None
228 # OK, so we have a timestamp, lets see if there is anything new to get
229 file_links
= soup
.find_all('a', {'class': 'file-download'})
230 for file_link
in file_links
:
231 timestamp
= file_link
.find_all('time')[0]['datetime']
232 logging
.debug("Checking {} (updated {})".format(
233 file_link
["title"], timestamp
))
234 if timestamp
> self
.last_time
:
236 "Found new/updated file {}".format(file_link
["title"]))
237 self
._needs
_download
= True
240 # Got here, so nope, no new files.
241 self
._needs
_download
= False
244 def download(self
, base_dir
):
245 """ Download all files for a given thing. """
247 self
._parse
(base_dir
)
250 logging
.error("Unable to parse {} - aborting download".format(self
.thing_id
))
253 if not self
._needs
_download
:
254 print("{} already downloaded - skipping.".format(self
.title
))
257 # Have we already downloaded some things?
258 timestamp_file
= os
.path
.join(self
.download_dir
, 'timestamp.txt')
260 if os
.path
.exists(self
.download_dir
):
261 if not os
.path
.exists(timestamp_file
):
262 # edge case: old style dir w/out timestamp.
264 "Old style download dir found for {}".format(self
.title
))
266 target_dir
= "{}_old".format(self
.download_dir
)
267 while os
.path
.exists(target_dir
):
268 prev_count
= prev_count
+ 1
269 target_dir
= "{}_old_{}".format(self
.download_dir
, prev_count
)
270 os
.rename(self
.download_dir
, target_dir
)
272 prev_dir
= "{}_{}".format(self
.download_dir
, self
.last_time
)
273 os
.rename(self
.download_dir
, prev_dir
)
275 # Get the list of files to download
276 soup
= BeautifulSoup(self
.text
, features
='lxml')
277 file_links
= soup
.find_all('a', {'class': 'file-download'})
283 if not self
.last_time
:
284 # If we don't have anything to copy from, then it is all new.
285 new_file_links
= file_links
287 new_last_time
= file_links
[0].find_all('time')[0]['datetime']
290 code
.interact(local
=dict(globals(), **locals()))
292 for file_link
in file_links
:
293 timestamp
= file_link
.find_all('time')[0]['datetime']
294 logging
.debug("Found file {} from {}".format(
295 file_link
["title"], timestamp
))
296 if timestamp
> new_last_time
:
297 new_last_time
= timestamp
299 for file_link
in file_links
:
300 timestamp
= file_link
.find_all('time')[0]['datetime']
301 logging
.debug("Checking {} (updated {})".format(
302 file_link
["title"], timestamp
))
303 if timestamp
> self
.last_time
:
304 new_file_links
.append(file_link
)
306 old_file_links
.append(file_link
)
307 if not new_last_time
or timestamp
> new_last_time
:
308 new_last_time
= timestamp
310 logging
.debug("new timestamp {}".format(new_last_time
))
312 # OK. Time to get to work.
313 logging
.debug("Generating download_dir")
314 os
.mkdir(self
.download_dir
)
315 # First grab the cached files (if any)
316 logging
.info("Copying {} unchanged files.".format(len(old_file_links
)))
317 for file_link
in old_file_links
:
318 old_file
= os
.path
.join(prev_dir
, file_link
["title"])
319 new_file
= os
.path
.join(self
.download_dir
, file_link
["title"])
321 logging
.debug("Copying {} to {}".format(old_file
, new_file
))
322 copyfile(old_file
, new_file
)
323 except FileNotFoundError
:
325 "Unable to find {} in old archive, redownloading".format(file_link
["title"]))
326 new_file_links
.append(file_link
)
328 # Now download the new ones
329 files
= [("{}{}".format(URL_BASE
, x
['href']), x
["title"])
330 for x
in new_file_links
]
331 logging
.info("Downloading {} new files of {}".format(
332 len(new_file_links
), len(file_links
)))
334 for url
, name
in files
:
335 file_name
= os
.path
.join(self
.download_dir
, name
)
336 logging
.debug("Downloading {} from {} to {}".format(
337 name
, url
, file_name
))
338 data_req
= requests
.get(url
)
339 with
open(file_name
, 'wb') as handle
:
340 handle
.write(data_req
.content
)
341 except Exception as exception
:
342 logging
.error("Failed to download {} - {}".format(name
, exception
))
343 os
.rename(self
.download_dir
, "{}_failed".format(self
.download_dir
))
347 image_dir
= os
.path
.join(self
.download_dir
, 'images')
348 imagelinks
= soup
.find_all('span', {'class': 'gallery-slider'})[0] \
349 .find_all('div', {'class': 'gallery-photo'})
350 logging
.info("Downloading {} images.".format(len(imagelinks
)))
353 for imagelink
in imagelinks
:
354 url
= next(filter(None,[imagelink
[x
] for x
in ['data-full',
357 'data-thumb']]), None)
359 logging
.warning("Unable to find any urls for {}".format(imagelink
))
362 filename
= os
.path
.basename(url
)
363 if filename
.endswith('stl'):
364 filename
= "{}.png".format(filename
)
365 image_req
= requests
.get(url
)
366 with
open(os
.path
.join(image_dir
, filename
), 'wb') as handle
:
367 handle
.write(image_req
.content
)
368 except Exception as exception
:
369 print("Failed to download {} - {}".format(filename
, exception
))
370 os
.rename(self
.download_dir
, "{}_failed".format(self
.download_dir
))
373 # instructions are good too.
374 logging
.info("Downloading readme")
376 readme_txt
= soup
.find('meta', property='og:description')['content']
377 with
open(os
.path
.join(self
.download_dir
,'readme.txt'), 'w') as readme_handle
:
378 readme_handle
.write("{}\n".format(readme_txt
))
379 except (TypeError, KeyError) as exception
:
380 logging
.warning("No readme? {}".format(exception
))
381 except IOError as exception
:
382 logging
.warning("Failed to write readme! {}".format(exception
))
384 # Best get some licenses
385 logging
.info("Downloading license")
387 license_txt
= soup
.find('div',{'class':'license-text'}).text
389 with
open(os
.path
.join(self
.download_dir
,'license.txt'), 'w') as license_handle
:
390 license_handle
.write("{}\n".format(license_txt
))
391 except AttributeError as exception
:
392 logging
.warning("No license? {}".format(exception
))
393 except IOError as exception
:
394 logging
.warning("Failed to write license! {}".format(exception
))
398 # Now write the timestamp
399 with
open(timestamp_file
, 'w') as timestamp_handle
:
400 timestamp_handle
.write(new_last_time
)
401 except Exception as exception
:
402 print("Failed to write timestamp file - {}".format(exception
))
403 os
.rename(self
.download_dir
, "{}_failed".format(self
.download_dir
))
405 self
._needs
_download
= False
406 logging
.debug("Download of {} finished".format(self
.title
))
409 def do_batch(batch_file
, download_dir
):
410 """ Read a file in line by line, parsing each as a set of calls to this script."""
411 with
open(batch_file
) as handle
:
414 logging
.info("Handling instruction {}".format(line
))
415 command_arr
= line
.split()
416 if command_arr
[0] == "thing":
418 "Handling batch thing instruction: {}".format(line
))
419 Thing(command_arr
[1]).download(download_dir
)
421 if command_arr
[0] == "collection":
423 "Handling batch collection instruction: {}".format(line
))
424 Collection(command_arr
[1], command_arr
[2],
425 download_dir
).download()
427 if command_arr
[0] == "user":
429 "Handling batch collection instruction: {}".format(line
))
430 Designs(command_arr
[1], download_dir
).download()
432 logging
.warning("Unable to parse current instruction. Skipping.")
436 """ Entry point for script being run as a command. """
437 parser
= argparse
.ArgumentParser()
438 parser
.add_argument("-l", "--log-level", choices
=[
439 'debug', 'info', 'warning'], default
='info', help="level of logging desired")
440 parser
.add_argument("-d", "--directory",
441 help="Target directory to download into")
442 parser
.add_argument("-f", "--log-file",
443 help="Place to log debug information to")
444 subparsers
= parser
.add_subparsers(
445 help="Type of thing to download", dest
="subcommand")
446 collection_parser
= subparsers
.add_parser(
447 'collection', help="Download one or more entire collection(s)")
448 collection_parser
.add_argument(
449 "owner", help="The owner of the collection(s) to get")
450 collection_parser
.add_argument(
451 "collections", nargs
="+", help="Space seperated list of the name(s) of collection to get")
452 thing_parser
= subparsers
.add_parser(
453 'thing', help="Download a single thing.")
454 thing_parser
.add_argument("things", nargs
="*", help="Space seperated list of thing ID(s) to download")
455 user_parser
= subparsers
.add_parser(
456 "user", help="Download all things by one or more users")
457 user_parser
.add_argument("users", nargs
="+", help="A space seperated list of the user(s) to get the designs of")
458 batch_parser
= subparsers
.add_parser(
459 "batch", help="Perform multiple actions written in a text file")
460 batch_parser
.add_argument(
461 "batch_file", help="The name of the file to read.")
462 subparsers
.add_parser("version", help="Show the current version")
464 args
= parser
.parse_args()
465 if not args
.subcommand
:
468 if not args
.directory
:
469 args
.directory
= os
.getcwd()
471 logger
= logging
.getLogger()
472 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
473 logger
.setLevel(logging
.DEBUG
)
474 console_handler
= logging
.StreamHandler()
475 console_handler
.setLevel(args
.log_level
.upper())
477 logger
.addHandler(console_handler
)
479 file_handler
= logging
.FileHandler(args
.log_file
)
480 file_handler
.setLevel(logging
.DEBUG
)
481 file_handler
.setFormatter(formatter
)
482 logger
.addHandler(file_handler
)
484 if args
.subcommand
.startswith("collection"):
485 for collection
in args
.collections
:
486 Collection(args
.owner
, collection
, args
.directory
).download()
487 if args
.subcommand
== "thing":
488 for thing
in args
.things
:
489 Thing(thing
).download(args
.directory
)
490 if args
.subcommand
== "user":
491 for user
in args
.users
:
492 Designs(user
, args
.directory
).download()
493 if args
.subcommand
== "version":
494 print("thingy_grabber.py version {}".format(VERSION
))
495 if args
.subcommand
== "batch":
496 do_batch(args
.batch_file
, args
.directory
)
499 if __name__
== "__main__":