Mercurial > code > home > repos > front-door-display
comparison schedule_server.py @ 12:4092f674046d
+ schedule_server.py
author | drewp@bigasterisk.com |
---|---|
date | Thu, 06 Jun 2024 14:49:34 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:affb3c8f3f58 | 12:4092f674046d |
---|---|
1 ''' | |
2 GET /data | |
3 | |
4 fetch https://bigasterisk.com/gcalendarwatch/graph/calendar/countdown/events | |
5 { | |
6 countdown: [ | |
7 {'label': 'asher birthday', 'when': '6 days 🌞🌞🌞🌞🌞🌞'}. | |
8 ] | |
9 } | |
10 | |
11 fetch iphone location events service in /my/serv/icloud-sync | |
12 { | |
13 map: { | |
14 image: 'bayarea.png', | |
15 locations: [ | |
16 {'label':'K', x: px, y: px} | |
17 ] | |
18 } | |
19 } | |
20 | |
21 fetch calendar upcoming | |
22 { | |
23 time: { | |
24 label: "23:30", | |
25 daySec: 20000, | |
26 scheduleRanges: [ | |
27 {label: 'youtube', startDaySec: 59400, endDaySec: 63000}, | |
28 {label: 'minecraft', startDaySec: 64800, endDaySec: 68400}, | |
29 {label: 'roblox', startDaySec: 64800, endDaySec: 68400}, | |
30 ] | |
31 } | |
32 } | |
33 ''' | |
34 | |
35 import asyncio | |
36 import datetime | |
37 import json | |
38 | |
39 from sse_starlette.sse import EventSourceResponse | |
40 from starlette.applications import Starlette | |
41 from starlette.routing import Route | |
42 | |
43 | |
44 def daySec(t: datetime.datetime): | |
45 return ( | |
46 t - | |
47 t.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() | |
48 | |
49 | |
50 async def updates(): | |
51 while True: | |
52 now = datetime.datetime.now() | |
53 yield json.dumps({ | |
54 'time': { | |
55 'label': now.strftime("%H:%M"), | |
56 'daySec': daySec(now), | |
57 | |
58 }, | |
59 'map': {}, | |
60 'countdown': {}, | |
61 }) | |
62 await asyncio.sleep(60) | |
63 | |
64 | |
65 async def events(request) -> EventSourceResponse: | |
66 generator = updates() | |
67 return EventSourceResponse(generator, | |
68 headers={ | |
69 'Access-Control-Allow-Origin': '*', | |
70 }) | |
71 | |
72 | |
73 app = Starlette(routes=[ | |
74 Route("/events", events), | |
75 ]) | |
76 | |
77 if __name__ == "__main__": | |
78 import uvicorn | |
79 uvicorn.run(app, host="0.0.0.0", port=8005) |