-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathical_generator.py
More file actions
149 lines (118 loc) · 5.37 KB
/
ical_generator.py
File metadata and controls
149 lines (118 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""iCal file generator for Octopus Energy free electricity sessions."""
import logging
from pathlib import Path
from datetime import datetime, timedelta
from typing import List
from icalendar import Calendar, Event, Alarm, vText
from session_parser import Session
logger = logging.getLogger(__name__)
class ICalGenerator:
"""Generator for iCal calendar files."""
def __init__(self, timezone: str = 'GMT', alarms_enabled: bool = True, alarm_times: List[int] = None):
"""
Initialize iCal generator.
Args:
timezone: Timezone for events
alarms_enabled: Whether to add alarms to events
alarm_times: List of minutes before event to add alarms (e.g., [60, 15, 0])
"""
self.timezone = timezone
self.alarms_enabled = alarms_enabled
self.alarm_times = alarm_times or [60, 15, 0]
def generate(self, sessions: List[Session], output_path: Path) -> bool:
"""
Generate iCal file from sessions.
Args:
sessions: List of Session objects
output_path: Path to save iCal file
Returns:
True if successful, False otherwise
"""
# Create calendar
cal = Calendar()
cal.add('prodid', '-//Octopus Energy Free Electricity//EN')
cal.add('version', '2.0')
cal.add('calscale', 'GREGORIAN')
cal.add('method', 'PUBLISH')
cal.add('x-wr-calname', vText('Octopus Free Electricity'))
cal.add('x-wr-timezone', vText(self.timezone))
if sessions:
description = 'Free electricity sessions from Octopus Energy'
else:
description = 'No free electricity sessions are currently scheduled.'
logger.info("Generating placeholder iCal file with no sessions")
cal.add('x-wr-caldesc', vText(description))
# Add events for each session
for session in sessions:
event = Event()
# Set event properties
event.add('summary', vText('Octopus Free Electricity'))
event.add('dtstart', session.start_time)
event.add('dtend', session.end_time)
# Use session start for deterministic output (avoids unnecessary deployments when unchanged)
event.add('dtstamp', session.start_time)
# Generate unique UID based on session string and start time
uid = f"{session.start_time.strftime('%Y%m%d%H%M')}@octopus.energy"
event.add('uid', uid)
# Add description
description = (
f"Free electricity session: {session.session_str}\n"
f"Duration: {session.duration}\n"
f"Make sure to use electricity during this period!"
)
event.add('description', vText(description))
# Add location
event.add('location', vText('UK'))
# Add status and other properties
event.add('status', vText('CONFIRMED'))
event.add('transp', vText('TRANSPARENT'))
# Add categories
event.add('categories', vText('Free Electricity,Octopus Energy'))
# Add alarms if enabled
if self.alarms_enabled and self.alarm_times:
for minutes in self.alarm_times:
alarm = Alarm()
alarm.add('action', vText('DISPLAY'))
# Custom description based on timing
if minutes == 0:
alarm_desc = 'Free electricity session starting NOW!'
elif minutes < 60:
alarm_desc = f'Free electricity session in {minutes} minutes!'
else:
hours = minutes // 60
alarm_desc = f'Free electricity session in {hours} hour{"s" if hours > 1 else ""}!'
alarm.add('description', vText(alarm_desc))
# Set trigger as timedelta (negative for before the event)
if minutes == 0:
trigger = timedelta(0) # At start time
else:
trigger = -timedelta(minutes=minutes) # Negative = before event
alarm.add('trigger', trigger)
event.add_component(alarm)
logger.debug(f"Added {minutes}-minute alarm for session: {session.session_str}")
# Add event to calendar
cal.add_component(event)
logger.debug(f"Added event for session: {session.session_str}")
# Write to file
try:
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, 'wb') as f:
f.write(cal.to_ical())
logger.info(f"Generated iCal file: {output_path} ({len(sessions)} event(s))")
return True
except Exception as e:
logger.error(f"Failed to write iCal file: {e}")
return False
def update_or_create(
self, sessions: List[Session], output_path: Path
) -> bool:
"""
Update existing iCal file or create new one.
This will replace the entire calendar file with new sessions.
Args:
sessions: List of Session objects
output_path: Path to save iCal file
Returns:
True if successful, False otherwise
"""
return self.generate(sessions, output_path)