summaryrefslogtreecommitdiff
path: root/prepdemoimages.py
blob: 6afa89edb180a6e0fb87859030dacf72dcd4adde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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')

Demo = namedtuple('Demo', ['name', 'images'])

demos = [
    Demo(name='Core', images=[]),
    Demo(name='With activity indication', images=[]),
    Demo(name='With overlay & activity indication', images=[]),
    Demo(name='With "close" button & activity indication', images=[]),
    Demo(name='With caption & activity indication', images=[]),
    Demo(name='With navigation & activity indication', images=[]),
    Demo(name='Combination', images=[]),
]

maxnum = len(demos)*3

def put_image(img):
    done = False
    for d in demos:
        if len(d.images) == 3:
            done = True
        else:
            d.images.append(img)
            if len(d.images) == 4:
                done = True
            else:
                return False
    return done


headers = {
    'Authorization': 'Client-ID 3004ee20c6b4822a4ab148506fef3be12eab826823b6d15a84dcdb4dec086f7c'
}

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)

print(template.render(demos=demos))