summaryrefslogtreecommitdiff
path: root/build.py
blob: eb9fdbb8cfcee40d2d0c4a9e889def104509d8f4 (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
#!/usr/bin/python

import os
from jinja2 import Template

def get_memes(directory):
    memes = []
    files = os.listdir(directory + "/orig/")
    files.sort()
    for f in files:
        meme = {
                "orig": "./{}/orig/{}".format(directory, f),
                "thumb": "./{}/thumb/{}".format(directory, f),
                }
        memes.append(meme)
    return memes

def bake_thumbnails(memes):
    for meme in memes:
        if not os.access(meme.get("thumb"), os.F_OK):
            os.system("convert {} -resize 320x320^\> {}"
                    .format(meme.get("orig"), meme.get("thumb")))

def render_template(template_fname, output_fname, memes):
    template_file = open(template_fname, "r")
    output_file = open(output_fname, "w")
    template = Template(template_file.read())
    output_file.write(template.render(memes=memes))
    output_file.close()


outdir = "."
memes_dir = "oreily-memes"
# The "om_" prefix means "oreily memes"
om_template_fname = "templates/oreily-memes.html.j2"
om_outdir = outdir
om_output_fname = om_outdir + "/" + "oreily-memes.html"

if not os.access(outdir, os.F_OK):
    os.mkdir(outdir, int('0775', 8))
memes = get_memes(memes_dir)
bake_thumbnails(memes)
render_template(om_template_fname, om_output_fname, memes)