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
|
|
|
|
2023-09-17 20:35:27 +00:00
|
|
|
import google_calendar
|
2022-08-18 22:03:40 +00:00
|
|
|
|
2023-09-17 20:35:27 +00:00
|
|
|
import 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
|
|
|
|
|
|
|
## Paths
|
|
|
|
|
|
|
|
@bottle.route('/static/<path:path>')
|
|
|
|
def static(path):
|
|
|
|
return bottle.static_file(path, root = './static')
|
|
|
|
|
2023-09-17 20:35:27 +00:00
|
|
|
def get_events_map():
|
|
|
|
today = datetime.date.today()
|
|
|
|
events_map = { }
|
|
|
|
for prefix, url in config.ICAL_LINKS.items():
|
|
|
|
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
|
|
|
|
2023-09-17 20:35:27 +00:00
|
|
|
@bottle.route('/')
|
|
|
|
def reddit_index():
|
|
|
|
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
|
|
|
|
|
|
|
## 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)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parser.parse_args()
|
|
|
|
bottle.run(host=args.hostname, port=args.port)
|
|
|
|
|
|
|
|
|