build-site.py 3.57 KB
Newer Older
lunasorcery's avatar
lunasorcery committed
1
2
3
#!/usr/bin/env python3

import os
4
import json
lunasorcery's avatar
lunasorcery committed
5
6
import shutil
import chevron
7
import binascii
8
import datetime
lunasorcery's avatar
lunasorcery committed
9
10


lunasorcery's avatar
lunasorcery committed
11
def maybe_mkdir(path):
lunasorcery's avatar
lunasorcery committed
12
13
14
15
	if not os.path.exists(path):
		os.mkdir(path)


16
17
18
19
20
21
def crc32_file(filename):
	buf = open(filename,'rb').read()
	buf = (binascii.crc32(buf) & 0xffffffff)
	return f"{buf:08x}"


22
23
24
# load prods
with open('prods.json') as file:
	prods = json.load(file)
lunasorcery's avatar
lunasorcery committed
25

lunasorcery's avatar
lunasorcery committed
26
prods = [prod for prod in prods if (not ('hidden' in prod)) or (not prod['hidden'])]
27
28
29
30
31
32

# figure out the meteorik prods
for idx,prod in enumerate(prods):
	if 'meteorik-type' in prod:
		prods[idx]['meteorik-winner']  = (prod['meteorik-type'] == 'winner')
		prods[idx]['meteorik-nominee'] = (prod['meteorik-type'] == 'nominee')
lunasorcery's avatar
lunasorcery committed
33
34
35
36
meteorikProds = sorted(
	[prod for prod in prods if 'meteorik-year' in prod],
	key = lambda x: (x['meteorik-year'], x['meteorik-type']),
	reverse=True)
37
38


lunasorcery's avatar
lunasorcery committed
39
40
maybe_mkdir('gen/')
maybe_mkdir('gen/img/')
lunasorcery's avatar
lunasorcery committed
41

42
43

print("copying static assets...")
44
45
46
47
48
staticAssets = [
	'fonts.css',
	'style.css',
	'script.js',
	'favicon.ico',
lunasorcery's avatar
lunasorcery committed
49
50
51
	'Muli-Regular.ttf',
	'Muli-Bold.ttf',
	'Muli-ExtraBoldItalic.ttf',
52
53
54
]
for asset in staticAssets:
	shutil.copyfile(asset, f'gen/{asset}')
lunasorcery's avatar
lunasorcery committed
55

56
57

print("converting images...")
lunasorcery's avatar
lunasorcery committed
58
59
60
61
62
63
64
for idx,prod in enumerate(prods):
	slug = prod['slug']
	src_jpg = f"raw-images/{slug}.jpg"
	src_png = f"raw-images/{slug}.png"
	dst_jpg = f"gen/img/{slug}.jpg"
	dst_png = f"gen/img/{slug}.png"

65
66
	print(f"- {slug}")

lunasorcery's avatar
lunasorcery committed
67
68
	if os.path.exists(dst_jpg):
		prods[idx]['image_url'] = f"img/{slug}.jpg"
69
		print("   already exists, skipping...")
lunasorcery's avatar
lunasorcery committed
70
71
72
		continue
	elif os.path.exists(dst_png):
		prods[idx]['image_url'] = f"img/{slug}.png"
73
		print("   already exists, skipping...")
lunasorcery's avatar
lunasorcery committed
74
75
76
		continue

	if os.path.exists(src_jpg):
77
78
79
80
81
		# if the source is a jpg, use that
		#shutil.copyfile(src_jpg, dst_jpg)

		# actually, let's make sure it's progressive
		os.system(f"convert -interlace Plane -quality 95 {src_jpg} {dst_jpg}")
lunasorcery's avatar
lunasorcery committed
82
		prods[idx]['image_url'] = f"img/{slug}.jpg"
83
84
85
86
87
88
89
90

		# if we've made it bigger, ditch the quality=95 flag
		if os.path.getsize(dst_jpg) > os.path.getsize(src_jpg):
			os.system(f"convert -interlace Plane {src_jpg} {dst_jpg}")

		print(f"   src_jpg: {os.path.getsize(src_jpg): 10,}b")
		print(f"   dst_jpg: {os.path.getsize(dst_jpg): 10,}b")

lunasorcery's avatar
lunasorcery committed
91
92
	elif os.path.exists(src_png):
		# if the source is a png, resave it as a jpg
93
		os.system(f"convert -interlace Plane -quality 95 {src_png} {dst_jpg}")
lunasorcery's avatar
lunasorcery committed
94
95

		# ...and use the smaller one
96
		print(f"   src_png: {os.path.getsize(src_png): 10,}b")
lunasorcery's avatar
lunasorcery committed
97
98
		if os.path.getsize(dst_jpg) < os.path.getsize(src_png):
			prods[idx]['image_url'] = f"img/{slug}.jpg"
99
			print(f"   dst_jpg: {os.path.getsize(dst_jpg): 10,}b")
lunasorcery's avatar
lunasorcery committed
100
101
102
103
		else:
			os.remove(dst_jpg)
			shutil.copyfile(src_png, dst_png)
			prods[idx]['image_url'] = f"img/{slug}.png"
104
			print(f"   dst_png: {os.path.getsize(dst_png): 10,}b")
lunasorcery's avatar
lunasorcery committed
105
	else:
106
		print("   missing raw image!")
lunasorcery's avatar
lunasorcery committed
107
108
109
		raise hell


110
sharedTemplate = {
111
112
113
	'current-year': datetime.datetime.now().year,
	'meteoriks-juror-application-open': False,
	'meteoriks-nominations-open': False,
114
115
116
117
118
	'hash-fonts-css': crc32_file('fonts.css'),
	'hash-style-css': crc32_file('style.css'),
}


119
print("applying templates...")
lunasorcery's avatar
lunasorcery committed
120
121
with open('index.mustache', 'r') as f:
	with open('gen/index.html', 'w') as fout:
122
		fout.write(chevron.render(f, sharedTemplate | {
lunasorcery's avatar
lunasorcery committed
123
124
			'page-gallery': True,
			'entries': prods }))
lunasorcery's avatar
lunasorcery committed
125

126
127
128
129
130
with open('meteoriks.mustache', 'r') as f:
	with open('gen/meteoriks.html', 'w') as fout:
		fout.write(chevron.render(f, sharedTemplate | {
			'page-meteoriks': True,
			'entries': meteorikProds }))
131
132
133

with open('about.mustache', 'r') as f:
	with open('gen/about.html', 'w') as fout:
134
		fout.write(chevron.render(f, sharedTemplate | {
lunasorcery's avatar
lunasorcery committed
135
			'page-about': True }))