diff options
author | Victor Häggqvist <[email protected]> | 2016-01-16 23:57:05 +0100 |
---|---|---|
committer | Victor Häggqvist <[email protected]> | 2016-01-16 23:57:05 +0100 |
commit | c79e99181bca83d046d6940e50355277dd0f9caf (patch) | |
tree | b521392dee891353715c10d3edb167fc4dbac62c | |
parent | db7d4eaaf13277d76ca0f243434eb89ec2fe58d3 (diff) |
cache demo images
-rwxr-xr-x | prepdemoimages.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/prepdemoimages.py b/prepdemoimages.py index da881c1..6afa89e 100755 --- a/prepdemoimages.py +++ b/prepdemoimages.py @@ -1,7 +1,9 @@ #!/usr/bin/env python +import os import requests from collections import namedtuple from jinja2 import FileSystemLoader, Environment +import json template_env = Environment(loader=FileSystemLoader('.')) template = template_env.get_template('demotemplate.html.jinja') @@ -37,9 +39,18 @@ def put_image(img): headers = { 'Authorization': 'Client-ID 3004ee20c6b4822a4ab148506fef3be12eab826823b6d15a84dcdb4dec086f7c' } -r = requests.get('https://api.unsplash.com/photos?per_page='+str(maxnum), headers=headers) -images = r.json() +cache_file = '.imgcache.json' +if os.path.isfile(cache_file): + with open(cache_file, 'r') as f: + images = json.load(f) +else: + r = requests.get('https://api.unsplash.com/photos?per_page='+str(maxnum), headers=headers) + images = r.json() + + with open(cache_file, 'w') as f: + json.dump(images, f) + for img in images: put_image(img) |