Implement calendar display with multi-day event support

- Added timezone support (Pacific/Auckland) for calendar events
- Implemented recurring event handling using recurring_ical_events library
- Created horizontal 5-day column layout for calendar display
- Fixed multi-day event rendering to show events across all active days
- Updated calendar to show next 5 days (today + 4)
- Reduced font sizes and padding for compact display
- Changed image rotation interval to 60 seconds
- Added pytz and recurring_ical_events dependencies

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-15 08:00:52 +13:00
parent 71a450b968
commit 189a340321
6 changed files with 215 additions and 46 deletions

86
app.py
View File

@@ -2,8 +2,11 @@ from flask import Flask, render_template, jsonify
import requests
import os
import random
from datetime import datetime, timedelta
from datetime import datetime, timedelta, date
from icalendar import Calendar
from config import Config
import pytz
import recurring_ical_events
app = Flask(__name__)
app.config.from_object(Config)
@@ -93,27 +96,76 @@ def get_weather():
@app.route('/api/calendar')
def get_calendar():
"""Fetch Google Calendar events."""
"""Fetch Google Calendar events from iCal feed."""
global calendar_cache
# Check cache
now = datetime.now()
# Check cache - use timezone-aware datetime
nz_tz = pytz.timezone('Pacific/Auckland')
now = datetime.now(nz_tz)
if (calendar_cache['data'] and calendar_cache['timestamp'] and
(now - calendar_cache['timestamp']).total_seconds() < app.config['CALENDAR_UPDATE_INTERVAL']):
return jsonify(calendar_cache['data'])
# TODO: Implement Google Calendar API integration
# For now, return placeholder data
try:
# This is placeholder data - will be replaced with actual Google Calendar API
events = [
{
'title': 'Setup Google Calendar API',
'start': (datetime.now() + timedelta(hours=2)).isoformat(),
'end': (datetime.now() + timedelta(hours=3)).isoformat(),
'location': ''
}
]
# Fetch iCal feed
ical_url = app.config.get('GOOGLE_CALENDAR_ICAL_URL')
if not ical_url:
return jsonify([])
response = requests.get(ical_url, timeout=10)
response.raise_for_status()
# Parse iCal data
cal = Calendar.from_ical(response.content)
# Use recurring_ical_events to get all events in the date range (including recurring ones)
cutoff_date = now + timedelta(days=app.config['CALENDAR_DAYS_AHEAD'])
# Get all events between now and cutoff_date
recurring_events = recurring_ical_events.of(cal).between(now, cutoff_date)
events = []
for component in recurring_events:
dtstart = component.get('dtstart')
dtend = component.get('dtend')
summary = str(component.get('summary', 'No Title'))
if dtstart and dtstart.dt:
# Handle both datetime and date objects
if isinstance(dtstart.dt, datetime):
event_start = dtstart.dt
# Make sure event_start is timezone-aware
if event_start.tzinfo is None:
event_start = nz_tz.localize(event_start)
elif isinstance(dtstart.dt, date):
# For date-only events, create a timezone-aware datetime
event_start = nz_tz.localize(datetime.combine(dtstart.dt, datetime.min.time()))
else:
continue
# Handle end time
if dtend and dtend.dt:
if isinstance(dtend.dt, datetime):
event_end = dtend.dt
# Make sure event_end is timezone-aware
if event_end.tzinfo is None:
event_end = nz_tz.localize(event_end)
elif isinstance(dtend.dt, date):
event_end = nz_tz.localize(datetime.combine(dtend.dt, datetime.min.time()))
else:
event_end = event_start + timedelta(hours=1)
else:
event_end = event_start + timedelta(hours=1)
events.append({
'title': summary,
'start': event_start.isoformat(),
'end': event_end.isoformat(),
'location': str(component.get('location', ''))
})
# Sort events by start time
events.sort(key=lambda x: x['start'])
calendar_cache = {'data': events, 'timestamp': now}
return jsonify(events)
@@ -122,7 +174,7 @@ def get_calendar():
app.logger.error(f"Error fetching calendar: {str(e)}")
if calendar_cache['data']:
return jsonify(calendar_cache['data'])
return jsonify({'error': 'Unable to fetch calendar data'}), 500
return jsonify([])
@app.route('/api/background')
@@ -184,4 +236,4 @@ if __name__ == '__main__':
os.makedirs(app.config['CREDENTIALS_DIR'], exist_ok=True)
# Run the app
app.run(host='0.0.0.0', port=5000, debug=True)
app.run(host='0.0.0.0', port=5002, debug=False, use_reloader=False)