Created
October 9, 2012 16:30
-
-
Save markpasc/3859899 to your computer and use it in GitHub Desktop.
download all the Make A Face faces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import codecs | |
from datetime import datetime | |
import logging | |
import json | |
import os | |
from os.path import isdir, join | |
from urllib import urlretrieve | |
import httplib2 | |
import pystache as mustache | |
from termtool import Termtool, subcommand, argument | |
HTML_TEMPLATE = """ | |
<!doctype html> | |
<html><head> | |
<meta charset="utf-8"> | |
<title>Make A Face</title> | |
</head><body> | |
<div id="photos"> | |
{{#photos}} | |
<div id="xid-{{xid}}"> | |
<img src="{{xid}}.jpg" width="400" height="400" alt="{{displayName}} at {{published}}"> | |
{{#favorites}} | |
{{#face}} | |
<img src="{{xid}}.jpg" width="100" height="100" alt="{{displayName}} at {{published}}"> | |
{{/face}} | |
{{/favorites}} | |
</div> | |
{{/photos}} | |
</div> | |
</body></html> | |
""" | |
class Maf(Termtool): | |
description = 'export the Make A Face site' | |
def save_posts(self, path, events): | |
if not isdir(path): | |
os.makedirs(path) | |
for event in events: | |
xid = event['xid'] | |
filepath = join(path, xid + '.json') | |
with codecs.open(filepath, 'w', 'utf8') as f: | |
json.dump(event, f, indent=4, sort_keys=True) | |
filepath = join(path, xid + '.jpg') | |
urlretrieve(event['url'], filepath) | |
@argument('path', help='directory to export to') | |
@subcommand(help='export faces to disk') | |
def export(self, args): | |
h = httplib2.Http() | |
url = 'http://make-a-face.org/faces/1.json' | |
while True: | |
resp, cont = h.request(url) | |
if resp.status != 200: | |
break | |
data = json.loads(cont) | |
events = data['events'] | |
logging.debug("Found %d posts at %s", len(events), url) | |
self.save_posts(args.path, events) | |
url = data['next'] | |
@argument('path', help='path to the export for which to build a site') | |
@subcommand(help='build an HTML site from the export') | |
def build(self, args): | |
photos = list() | |
filenames = os.listdir(args.path) | |
for filename in filenames: | |
if not filename.endswith('.json'): | |
continue | |
filepath = join(args.path, filename) | |
with codecs.open(filepath, 'r', 'utf8') as f: | |
photo = json.load(f) | |
photo['published'] = datetime.strptime(photo['published'], '%Y-%m-%dT%H:%M:%S') | |
photos.append(photo) | |
data = { | |
'photos': sorted(photos, key=lambda p: p['published']), | |
} | |
html = mustache.render(HTML_TEMPLATE.strip(), data) | |
filepath = join(args.path, 'index.html') | |
with codecs.open(filepath, 'w', 'utf8') as f: | |
f.write(html) | |
if __name__ == '__main__': | |
Maf().run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python maf.py -v -v -v export out/ | |
INFO: Set log level to DEBUG | |
DEBUG: Found 34 posts at http://make-a-face.org/faces/1.json | |
DEBUG: Found 36 posts at http://make-a-face.org/faces/2.json | |
DEBUG: Found 36 posts at http://make-a-face.org/faces/3.json | |
DEBUG: Found 39 posts at http://make-a-face.org/faces/4.json | |
DEBUG: Found 43 posts at http://make-a-face.org/faces/5.json | |
DEBUG: Found 48 posts at http://make-a-face.org/faces/6.json | |
DEBUG: Found 44 posts at http://make-a-face.org/faces/7.json | |
DEBUG: Found 43 posts at http://make-a-face.org/faces/8.json | |
DEBUG: Found 24 posts at http://make-a-face.org/faces/9.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/10.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/11.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/12.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/13.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/14.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/15.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/16.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/17.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/18.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/19.json | |
DEBUG: Found 0 posts at http://make-a-face.org/faces/20.json | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment