def get_events_for_date(target_date=None): """ Fetches events for a specific date from Google Calendar via ICS feeds. Args: target_date: datetime.date object for the target day. If None, uses today. Returns a list of event dictionaries. """ # Hardcoded calendar URLs (not using env var to avoid placeholder issues) CALENDAR_URLS = [ 'cal1url', 'call2url' ] LOCAL_TZ = os.getenv('TIMEZONE', 'Europe/Lisbon') # Get timezone local = tz.gettz(LOCAL_TZ) # If no target date provided, use today if target_date is None: target_date = datetime.now(local).date() # Create datetime for the target day boundaries day_start = datetime.combine(target_date, datetime.min.time()).replace(tzinfo=local) day_end = day_start + timedelta(days=1) # Debug: Print the date range we're checking print(f"\n[Debug] Checking calendars for date: {target_date.strftime('%Y-%m-%d')}") print(f" Start: {day_start.strftime('%Y-%m-%d %H:%M %Z')}") print(f" End: {day_end.strftime('%Y-%m-%d %H:%M %Z')}") print(f" Timezone: {LOCAL_TZ}") all_events = [] # Fetch from each calendar for idx, cal_url in enumerate(CALENDAR_URLS, 1): calendar_name = f"Calendar {idx}" print(f"\n[Debug] Fetching {calendar_name}...") try: # Load calendar from ICS URL with adequate timeout r = requests.get(cal_url, timeout=30) r.raise_for_status() cal = Calendar(r.text) events_found_this_cal = 0 total_events_in_cal = len(list(cal.events)) print(f" Total events in {calendar_name}: {total_events_in_cal}") # Use timeline to efficiently filter events for target day's date range # Convert local times to UTC for timeline filtering day_start_utc = day_start.astimezone(timezone.utc) day_end_utc = day_end.astimezone(timezone.utc) # Get events in target day's range using timeline days_timeline = cal.timeline.overlapping(day_start_utc, day_end_utc) for e in days_timeline: if not e.begin: continue # Get event start time start = e.begin.datetime if start.tzinfo is None: start = start.replace(tzinfo=timezone.utc) # Convert to local timezone start_local = start.astimezone(local) # Debug: Print first few events to see dates if events_found_this_cal < 3: print(f" Event: '{e.name}' at {start_local.strftime('%Y-%m-%d %H:%M')}") # Get end time end = e.end.datetime if e.end else None end_local = end.astimezone(local) if end else None all_events.append({ "title": e.name, "start": start_local.strftime("%H:%M"), "end": end_local.strftime("%H:%M") if end_local else None, "location": e.location or "", "description": e.description or "" }) events_found_this_cal += 1 print(f" [OK] Found {events_found_this_cal} event(s) for target day in {calendar_name}") except requests.exceptions.RequestException as e: print(f" [X] Network error fetching {calendar_name}: {str(e)}") continue except Exception as e: print(f" [X] Error processing {calendar_name}: {type(e).__name__}: {str(e)}") continue # Sort by start time all_events.sort(key=lambda x: x["start"]) # Print all events in detail if all_events: print(f"\n[Google Calendar] Found {len(all_events)} event(s) for {target_date.strftime('%Y-%m-%d')}:") print("-" * 60) for event in all_events: time_str = f"{event['start']}-{event['end']}" if event['end'] else event['start'] location_str = f" @ {event['location']}" if event['location'] else "" print(f" {time_str} | {event['title']}{location_str}") print("-" * 60) else: print(f"\n[Google Calendar] No events for {target_date.strftime('%Y-%m-%d')}") return all_events