-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (100 loc) · 3.72 KB
/
main.py
File metadata and controls
134 lines (100 loc) · 3.72 KB
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
#!/usr/bin/env python
"""The Greg or Ian Calendar.
A web app, API and RFC5545 iCalendar feed to determine whether today is a "Greg" or an
"Ian" day.
This program is free software: you can redistribute it and/or modify it under the terms
of the GNU Affero General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this
program. If not, see <http://www.gnu.org/licenses/>.
"""
__copyright__ = "Copyright (C) 2024 Mike Coats"
__license__ = "AGPL-3.0-or-later"
__contact__ = "i.am@mikecoats.com"
__author__ = "Mike Coats"
__credits__ = ["Mike Coats", "Natasha Jay", "Khomsun Chaiwong"]
__maintainer__ = "Mike Coats"
__email__ = "i.am@mikecoats.com"
__status__ = "Production"
__version__ = "1.1.2"
import asyncio
from datetime import date, timedelta
import uvicorn
from fastapi import FastAPI, Request, Response
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from ics import Calendar, Event
GREG = "Greg"
IAN = "Ian"
GREG_OR_IAN = [
GREG,
IAN,
GREG,
IAN,
GREG,
IAN,
GREG,
]
class CalendarResponse(Response):
"""Used to tell FastAPI to return the response with a 'text/calendar' media type.
This class is cribbed directly from FastAPI's HTMLResponse, which is simply:
```
class HTMLResponse(Response):
media_type = "text/html"
```
"""
media_type = "text/calendar"
app = FastAPI()
templates = Jinja2Templates(directory=".")
app.mount("/assets", StaticFiles(directory="./assets"), name="assets")
def day_ordinal(day: int):
"""Return whether a day of the month is a 'st', 'nd', 'rd' or 'th'."""
if day in (1, 21, 31):
return "st"
if day in (2, 22):
return "nd"
if day in (3, 23):
return "rd"
return "th"
def pretty_date(src: date):
"""Serialize a date in the format 'Sunday, 5th May 2024'."""
day_of_month = src.day
pretty_day = f"{day_of_month}{day_ordinal(day_of_month)}"
return src.strftime(f"%A, {pretty_day} %B %Y")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
"""Return an HTML page so our visitor knows today's 'Greg' or 'Ian' status."""
today = date.today()
return templates.TemplateResponse(
"index.html",
{
"request": request,
"date": today.strftime("%Y-%m-%d"),
"nice_date": pretty_date(today),
"greg_or_ian": GREG_OR_IAN[today.weekday()],
},
)
def build_event(src_date: date, offset_days: int):
"""Return an all-day Event, captioned with Greg or Ian."""
offset = timedelta(days=offset_days)
event_date = src_date + offset
e = Event(begin=event_date, name=GREG_OR_IAN[event_date.weekday()])
e.make_all_day()
return e
@app.get("/feed.ics", response_class=CalendarResponse)
async def feed():
"""Return an iCal feed so our visitor knows many 'Greg' or 'Ian' statuses."""
today = date.today()
c = Calendar(events=[build_event(today, offset) for offset in range(-7, 28)])
return c.serialize()
async def main():
"""When the module is run directly, hook up a uvicorn server and host the app."""
config = uvicorn.Config("main:app", host="0.0.0.0", port=8001, log_level="info")
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
asyncio.run(main())