-
Notifications
You must be signed in to change notification settings - Fork 1
/
motd.py
executable file
·232 lines (182 loc) · 6.32 KB
/
motd.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
from datetime import date, datetime, timedelta
import json
import os
import codecs
import click
from PIL import Image
from jinja2 import Environment, FileSystemLoader
from jinja2.exceptions import TemplateNotFound, TemplateError
# Todo - find a replacement for this
from werkzeug.contrib.atom import AtomFeed
# Todo - add queue command - this will copy the raw image and data to a queue
# folder for later processing
# Todo - add dequeue - updat motd.json with any new things from the queue, copy
# images, and rebuild (maybe this should be build --queue?)
def get_rows():
rows = []
try:
with open("motd.json") as json_file:
rows = json.load(json_file)
except FileNotFoundError:
pass
return rows
def get_date_string():
rows = get_rows()
if len(rows):
last_date = datetime.strptime(rows[-1]["date"], "%Y-%m-%d")
next_date = last_date + timedelta(days=1)
else:
next_date = date.today()
return f"{next_date.year}-{next_date.month:02}-{next_date.day:02}"
date_string = get_date_string()
def get_date(row):
return row["date"]
def update_entries(row, rows):
replaced = False
for idx, r in enumerate(rows):
if r["date"] == row["date"]:
print("\nFound existing row:")
print(f" - Date: {r['date']}")
print(f" - Title: {r['title']}")
print(f" - Image: {r['image']}")
click.confirm("Replace existing row?", abort=True)
rows[idx] = row
replaced = True
if not replaced:
rows.append(row)
rows.sort(key=get_date)
return rows
def render(tplfile, data, outfile):
env = Environment(loader=FileSystemLoader(os.path.abspath("src/")))
tpl = env.get_template("motd/{}".format(tplfile))
try:
html = tpl.render(data)
destfile = codecs.open(outfile, "w", "utf-8")
destfile.write(html)
destfile.close()
except TemplateError as err:
print("Could not parse template: %s" % err)
def render_list(rows):
print(" - Rendering all.html...", end="")
render("_list.html.j2", {"rows": rows[::-1]}, "build/motd/all.html")
print("done.")
def render_day(row, is_today=False):
print(" - Rendering {}.html...".format(row["date"]), end="")
row_date = datetime.strptime(row["date"], "%Y-%m-%d")
delta = row_date - datetime(2018, 1, 14)
prev_date = row_date - timedelta(days=1)
motd_data = {
"motd_date": row_date.strftime("%Y, %B %d"),
"motd_uptime": f"{delta.days} days",
}
if prev_date.year >= 2020:
motd_data[
"motd_previous"
] = f"{prev_date.year}-{prev_date.month:02}-{prev_date.day:02}.html"
render(
"_date.html.j2", {**motd_data, **row}, "build/motd/{}.html".format(row["date"])
)
print("done.")
if is_today:
print(" - Rendering today.html...", end="")
render("_date.html.j2", {**motd_data, **row}, "build/motd/today.html")
render("_date.html.j2", {**motd_data, **row}, "build/motd/index.html")
print("done.")
def get_rss_html(row):
env = Environment(loader=FileSystemLoader(os.path.abspath("src/")))
tpl = env.get_template("motd/{}".format("_rss_entry.html.j2"))
try:
return tpl.render(row)
except TemplateError as err:
print("Could not parse template: %s" % err)
return None
@click.group()
def motd():
pass
@motd.command()
@click.argument("image", type=click.Path(exists=True))
@click.option(
"--day", prompt="Date", help="The day to add", default=date_string,
)
@click.option(
"--title", prompt="Title", help="The title for the picture",
)
def add(image, day, title):
# validate day
if day == "today":
next_date = date.today()
day = f"{next_date.year}-{next_date.month:02}-{next_date.day:02}"
elif day == "tomorrow":
next_date = date.today() + timedelta(days=1)
day = f"{next_date.year}-{next_date.month:02}-{next_date.day:02}"
else:
try:
user_date = datetime.strptime(day, "%Y-%m-%d")
day = f"{user_date.year}-{user_date.month:02}-{user_date.day:02}"
except ValueError:
print("Cannot parse date {}, please use YYYY-MM-DD".format(day))
return
# resize and copy image
try:
os.mkdir("src/images/motd")
except OSError:
pass
im = Image.open(image)
im.thumbnail((1460, 1460), Image.ANTIALIAS)
thumb_filename = "{}.jpg".format(day)
thumb_path = os.path.join("src", "images", "motd", thumb_filename)
rgb_im = im.convert("RGB")
rgb_im.save(thumb_path, "jpeg", quality=90)
# add new row to file
row = {"date": day, "image": thumb_filename, "title": title}
rows = update_entries(row, get_rows())
with open("motd.json", "w") as json_file:
json.dump(rows, json_file, indent=2)
# link list windows to individual pages
# maybe add rsync step?
# render_update(rows)
# tiles
# rss feed
def is_today_or_earlier(row):
last_date = datetime.strptime(row["date"], "%Y-%m-%d")
if last_date <= datetime.today():
return True
return False
@motd.command()
@click.option("--all/--only-latest", default=False)
def build(all):
rows = get_rows()
earlier_rows = [row for row in rows if is_today_or_earlier(row)]
print("Rendering most recent:")
render_list(earlier_rows)
render_day(earlier_rows[-1], True)
if all:
print("Rendering previous days:")
for row in earlier_rows[:-1]:
render_day(row)
print("Rendering atom feed:")
feed = AtomFeed(
"motd",
feed_url="https://goodrobot.net/motd/atom.xml",
url="https://goodrobot.net/motd/",
subtitle="Mango of the day",
)
feed_rows = earlier_rows[-10:]
for row in feed_rows[::-1]:
row_datetime = datetime.strptime(row["date"], "%Y-%m-%d")
feed.add(
row["title"],
get_rss_html(row),
url="https://goodrobot.net/motd/{}.html".format(row["date"]),
content_type="html",
updated=row_datetime,
published=row_datetime,
author="jd",
title_type="text",
)
with open("build/motd/atom.xml", "wb") as atom_file:
atom_file.write(feed.get_response().data)
print("Done!")
if __name__ == "__main__":
motd()