kobo-wall-calendar/kobo_wall_calendar/__main__.py

77 lines
2.2 KiB
Python
Raw Normal View History

2022-08-18 22:03:40 +00:00
import argparse
import bottle
2023-09-17 20:35:27 +00:00
import datetime
2022-08-18 22:03:40 +00:00
2024-07-17 13:49:16 +00:00
from . import (google_calendar, config)
2022-08-18 22:03:40 +00:00
2023-09-17 20:35:27 +00:00
TEMPLATE_INDEX = bottle.SimpleTemplate(name = "templates/index.html")
2022-08-18 22:03:40 +00:00
2023-09-17 16:06:20 +00:00
# Template rendering
2022-08-18 22:03:40 +00:00
2023-09-17 20:35:27 +00:00
def last_day_of_month(date):
date = date.replace(day = 28) + datetime.timedelta(days = 5)
date = date.replace(day = 1)
date = date - datetime.timedelta(days = 1)
return date
def range_to_render(today):
first_day_of_month = today.replace(day = 1)
start = first_day_of_month - datetime.timedelta(days = first_day_of_month.weekday())
end = last_day_of_month(today)
return (start, end)
2023-09-17 16:06:20 +00:00
def render_calendar(events_map, today):
2022-08-18 22:03:40 +00:00
days = []
2023-09-17 20:35:27 +00:00
start, end = range_to_render(today)
date = start
while date <= end:
2022-08-18 22:03:40 +00:00
days.append({
2023-09-17 20:35:27 +00:00
'date': date,
'is_weekend': date.isoweekday() == 6 or date.isoweekday() == 7,
'week_of_year': int(date.strftime('%W')),
'days_from_now': (date - today).days,
'events': events_map.get(date, []),
2022-08-18 22:03:40 +00:00
})
2023-09-17 20:35:27 +00:00
date += datetime.timedelta(days = 1)
header = today.strftime('%B %Y')
return TEMPLATE_INDEX.render(days = days, header = header)
2023-09-17 16:06:20 +00:00
2024-07-17 13:41:01 +00:00
def get_events_map() -> tuple[dict[datetime.date, list[str]], datetime.date]:
2023-09-17 20:35:27 +00:00
today = datetime.date.today()
events_map = { }
2024-07-17 13:49:16 +00:00
for url in config.get_ical_links():
prefix = 'J '
2023-09-17 20:35:27 +00:00
for date, event in google_calendar.get_events_for_month(url, today):
text = '{}: {}'.format(prefix, event.summary)
events_map.setdefault(date, []).append(text)
del date, event, text
2023-09-17 16:06:20 +00:00
2023-09-17 20:35:27 +00:00
return events_map, today
2023-09-17 16:06:20 +00:00
2024-07-17 13:41:01 +00:00
## Paths
@bottle.route('/static/<path:path>')
def static(path):
return bottle.static_file(path, root = './static')
2023-09-17 20:35:27 +00:00
@bottle.route('/')
2024-07-17 13:41:01 +00:00
def month_calendar():
2023-09-17 20:35:27 +00:00
events_map, today = get_events_map()
2023-09-17 16:06:20 +00:00
return render_calendar(events_map, today)
2022-08-18 22:03:40 +00:00
2024-07-17 13:41:01 +00:00
def main():
## Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument('--hostname', action='store', default = 'localhost', dest='hostname')
parser.add_argument('--port', action='store', default = 8080, dest='port', type = int)
2022-08-18 22:03:40 +00:00
args = parser.parse_args()
2024-07-17 13:41:01 +00:00
# Initialization
2022-08-18 22:03:40 +00:00
bottle.run(host=args.hostname, port=args.port)
2024-07-17 13:41:01 +00:00
if __name__ == '__main__':
main()
2022-08-18 22:03:40 +00:00