This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_html.py
97 lines (75 loc) · 3.28 KB
/
build_html.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
# Console Latest Build Static HTML Script
#
# Generates the static HTML version of the newsletter from the interesting
# tools and beta programs JSON provided as input. Designed to be run as part
# of a build process.
import argparse
import json
from datetime import date
import jinja2
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta, TH
# Parse args
parser = argparse.ArgumentParser()
parser.add_argument('--tools-json', help='Path to the file containing the'
' interesting tools list as a JSON object', required=True)
parser.add_argument('--beta-json', help='Path to the file containing the beta'
' list as a JSON object', required=True)
parser.add_argument('--template', help='Path to the file containing the table'
' template within the templates/ directory', required=True)
parser.add_argument('--output', help='Path to the location of the output file'
' (will be created if it does not exist, overwritten if it'
' does)', required=True)
parser.add_argument('--ignore-date', help='Output every item in the JSON'
' regardless of the date. Used for testing',
type=bool, default=False, required=False)
args = parser.parse_args()
print('Starting build')
# Get last Thursday
today = date.today()
last_thursday = today - relativedelta(weekday=TH(-1))
# Open and parse the JSON file to get the list of interesting tools.
# See example-tool-list.json for what this should look like.
# It will be generated as part of the build steps using the GitHub
# gsheet.action from the Interesting Tools Google Sheet (see README.md).
print('Loading tools JSON...')
interesting = []
with open(args.tools_json, 'r') as f:
tools = json.load(f)
for tool in tools['results'][0]['result']['formatted']:
if tool['Scheduled for'] == "":
continue
scheduled_for = parse(tool['Scheduled for'])
# Only pull out things scheduled for the last newsletter
if args.ignore_date \
or scheduled_for.isocalendar() == last_thursday.isocalendar():
interesting.append(tool)
print('Loaded tools JSON')
# Same for the betas
print('Loading beta JSON...')
programs = []
with open(args.beta_json, 'r') as f:
betas = json.load(f)
for program in betas['results'][0]['result']['formatted']:
if program['Scheduled for'] == '':
continue
scheduled_for = parse(program['Scheduled for'])
# Only pull out things scheduled for the last newsletter
if args.ignore_date \
or scheduled_for.isocalendar() == last_thursday.isocalendar():
programs.append(program)
print('Loaded beta JSON')
# Load the template file, providing the list for it to loop through
print('Rendering template...')
template_loader = jinja2.FileSystemLoader(searchpath='templates')
template_env = jinja2.Environment(loader=template_loader, autoescape=True)
template = template_env.get_template(args.template)
rendered = template.render(tools=interesting, betas=programs)
print('Rendered template')
# Output the rendered template
print('Outputting template')
output = open(args.output, "w")
output.write(rendered)
output.close()
print('Output template')
print('Build table complete')