-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.py
executable file
·85 lines (76 loc) · 2.63 KB
/
atom.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
from datetime import datetime, timedelta
from flask import Blueprint
import settings as s
import utils as u
atom = Blueprint("atom", __name__)
with open("templates/atom_entry.txt", "r") as atom_entry:
atom_entry = atom_entry.read()
with open("templates/atom_feed.txt", "r") as atom_feed:
atom_feed = atom_feed.read()
with open("locations.txt") as loc:
loc = [L.split(" ") for L in loc.read().splitlines()]
loc = {L[0]: " ".join(L[1:]) for L in loc}
@atom.route("/feed.atom")
def feed():
entries = ld()
updated = entries[-1][1].strftime("%Y-%m-%dT%H:%M:%SZ")
feed_head = atom_feed.format(s.self_url, updated)
feed_out = [feed_head]
for e in entries:
feed_out.append(entry(e))
feed_out.append("</feed>")
return "\n".join(feed_out)
def entry(event):
"""Generate an Atom feed entry.
Input: filename, guests, title, location, description
Template takes URL, datetime, title, link
"""
e = []
dt = event[1].strftime("%Y-%m-%d, %H:%M UTC")
print(event)
desc = f"{dt}<br>Event at <a href='{s._url}{event[4]}'>" \
+ f"{loc[event[4]]}</a><br><br>" + event[5] \
+ f"<br><br><a href='{s.self_url}e/{event[0]}.ics'>Calendar file</a>"
desc = desc.replace("&", "&")
e.append(s.self_url + "e/" + event[0])
e.append(event[1].strftime("%Y-%m-%dT%H:%M:%SZ"))
e.append(event[3])
e.append(desc\
.replace("&apos;", "'")\
.replace("&", "&")\
.replace(">", ">")\
.replace("<", "<"))
return atom_entry.format(*e)
def ld():
with open("data/list.txt", "r") as entries:
entries = sorted(entries.read().splitlines())
for n, e in enumerate(entries):
e = e.split(">")
e.insert(1, e[0][:-7] + "00")
e[1] = datetime.strptime(e[1], "%Y%m%d%H%M")
entries[n] = e
return entries
def today():
return None
def alarm():
"""List all events, beginning with the next [window of time] and
working backwards. Set the upcoming events period in global settings"""
seconds = 60*60*4
now = datetime.now()
entries = ld()
for e in entries:
until = e[1] - now
until = int(until.total_seconds())
if (0 < until < seconds):
print("\t!! event happening in the next 4 hours")
print("\n".join([str(x) for x in e]))
elif (0 > until):
print("\t!! event already happened")
else:
print("\t!! event happening in more than 4 hours")
print(e[1] - now, "\n", e)
# entries = [int(e[0][:-9]) for e in ld()]
# for e in entries:
# print(e, now, e - now)
return None
# 20220405, 20220223