-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-content.py
executable file
·127 lines (102 loc) · 4.03 KB
/
process-content.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import sys
import os
import json
import io
from lxml.html import soupparser
from PIL import Image
if len(sys.argv) != 2:
print("usage: ./process-content.py pregen/<CONTENT_DIRECTORY>")
exit(1)
pregenContentPath = sys.argv[1]
if not os.path.isdir(pregenContentPath):
print("error: %s is not a content directory" % pregenContentPath)
print("usage: ./process-content pregen/<CONTENT_DIRECTORY>")
exit(1)
metadataPath = os.path.join(pregenContentPath, "metadata.json")
if not os.path.isfile(metadataPath):
print("error: %s does not have a metadata.json file" % pregenContentPath)
exit(1)
def hyphensToCamelcase(s):
return "".join(map(lambda p: p.capitalize(), s.strip(os.path.sep).split("-")))
def areasToTypescript(areas):
typescript = "[\n"
areaLines = []
for area in areas:
areaLines.append(" { name: \"%s\", shape: \"%s\", coords: \"%s\" }" % (area["name"], area["shape"], area["coords"]))
typescript += ",\n".join(areaLines)
typescript += "\n ]\n"
return typescript
def adsToTypescript(ads):
adLines = [" " + json.dumps(ad) for ad in ads]
typescript = ",\n".join(adLines)
return typescript
contentName = pregenContentPath.split("pregen" + os.path.sep)[-1].strip(os.path.sep)
imageDir = "public/static/images"
contentImgPath = os.path.join(imageDir, "%s.jpg" % contentName)
thumbnailImgPath = os.path.join(imageDir, "%s_thumbnail.jpg" % contentName)
if not os.path.isfile(contentImgPath):
print("error: %s content image file does not exist" % contentImgPath)
exit(1)
# TODO: add other image-checking errors like resolution and file size maximum
im = Image.open(contentImgPath)
if im.width > 1920 or im.height > 1920:
print("error: %s content file is too large, maximum of 1920x1920" % contentImgPath)
exit(1)
if not os.path.isfile(thumbnailImgPath):
print("error: %s content thumbnail file does not exist" % thumbnailImgPath)
exit(1)
# create the thumbnail
THUMB_WIDTH = 300
THUMB_HEIGHT = 168
im = Image.open(thumbnailImgPath)
if im.width != THUMB_WIDTH or im.height != THUMB_HEIGHT:
print("error: %s content thumbnail file must be %ix%i" % (thumbnailImgPath, THUMB_WIDTH, THUMB_HEIGHT))
exit(1)
with open(metadataPath) as f:
metadata = json.load(f)
className = hyphensToCamelcase(contentName)
areas = []
for areaPath in os.listdir(pregenContentPath):
if areaPath.endswith(".areas"):
areaName = os.path.splitext(areaPath)[0]
print("processing areas file: " + areaPath)
if ":" in areaName:
print("error: area files like '%s' cannot contain a colon" % areaName)
exit(1)
with open(os.path.join(pregenContentPath, areaPath)) as a:
# super hacky way to parse HTML
contents = u"<areas>\n" + a.read() + "</areas>\n"
tree = soupparser.parse(io.StringIO(contents))
areasRoot = [ child for child in tree.getroot() ][0]
for child in areasRoot:
areas.append({
"name" : areaName,
"shape" : child.attrib["shape"],
"coords" : child.attrib["coords"]
})
print(areas)
if "ads" in metadata:
ads = metadata["ads"]
else:
ads = []
typescript = """
import { Metadata } from '../Metadata'
export default {
"title": "%s",
"subpath": "%s",
"creator": "%s",
"creatorLink": "%s",
"ads": [
%s
],
"areas": %s
} as Metadata;
""" % (metadata["title"], contentName, metadata["creator"], metadata["creator-link"], adsToTypescript(ads), areasToTypescript(areas))
print(typescript)
processedPath = "src/autogenerated"
typescriptPath = os.path.join(processedPath, "%s.ts" % className)
with open(typescriptPath, "w") as o:
o.write(typescript)
print("*** ADD THIS LINE TO 'src/MetadataStore.ts' ***\n")
print("import %s from './autogenerated/%s';" % (className, className))