Skip to content

Instantly share code, notes, and snippets.

@greenarrow
Created December 2, 2011 17:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenarrow/1424180 to your computer and use it in GitHub Desktop.
Save greenarrow/1424180 to your computer and use it in GitHub Desktop.
Auto slice favourites from thingiverse
#!/usr/bin/env python
import urllib
import re
import os
BASE = "http://www.thingiverse.com"
USER = "greenarrow"
# Find all things on a page
RE_THINGS = re.compile(
r'<div class="thing_name"><a href=".*?/thing:(\d+)">\s*(.+?)\s*</a>'
)
# Find all downloads on a page
RE_DOWNLOAD = re.compile(
r'<h3 class="file_info">\s*(.+?)\s*</h3>.+?<a href="/download:(\d+)">',
re.DOTALL
)
def get_thing(thing, name):
files = 0
# Create directory for the thing
path = "%s - %s" % (thing, name)
os.mkdir(path)
# Download the page to see files
data = urllib.urlopen("%s/thing:%s" % (BASE, thing)).read()
# Download all stl files
for name, download in RE_DOWNLOAD.findall(data):
if not name.endswith(".stl"):
continue
open(os.path.join(path, name), "w").write(
urllib.urlopen("%s/download:%s" % \
(BASE, download)).read()
)
files += 1
return files
if __name__ == "__main__":
# Find the things on the first page
data = urllib.urlopen("%s/%s/favorites" % (BASE, USER)).read()
things = RE_THINGS.findall(data)
for thing, name in things:
if os.path.isdir("%s - %s" % (thing, name)):
continue
files = get_thing(thing, name)
if files > 0:
print "%s - %s: %d files" % (thing, name, files)
#!/usr/bin/env python
import os
import subprocess
ROOT = os.getcwd()
SF = os.environ["SKEINFORGE"]
VERBOSE = False
def forge(filename):
# Check to see if gcode exists
if os.path.exists("%s_export.gcode" % filename[:-4]):
if VERBOSE:
print "skipping completed %s" % filename
return False
print "slicing %s" % filename
log = open("%slog" % filename[:-3], "w")
cmd = [SF, filename]
error = subprocess.Popen(cmd, stdout=log, stderr=log).wait()
log.close()
if error != 0:
open(os.path.join(os.path.split(filename)[0], "ERRORS"))
return False
return True
if __name__ == "__main__":
for path, dirs, files in os.walk(ROOT):
for f in files:
if not f.endswith(".stl"):
continue
forge(os.path.join(path, f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment