-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathics_converter.py
151 lines (127 loc) · 4.58 KB
/
ics_converter.py
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
150
151
import datetime
import re
import uuid
def parse_llm_output(llm_text):
"""
Parse the LLM's text lines of the form:
Quiz#: MM/DD
會考: MM/DD
Returns a list of dicts like:
[
{'type': 'quiz', 'title': 'Quiz1', 'date': '03/05'},
{'type': 'exam', 'title': '會考', 'date': '03/25'},
...
]
"""
events = []
# Split lines and parse
for line in llm_text.strip().splitlines():
line = line.strip()
# Examples:
# "Quiz1: 03/05"
# "會考: 03/25"
# Regex capture group: (Quiz\d+|會考) : (MM/DD)
match = re.match(r'^(Quiz\d+|會考)\s*:\s*(\d{2}/\d{2})$', line)
if match:
event_type_str = match.group(1) # e.g. "Quiz1" or "會考"
date_str = match.group(2) # e.g. "03/05"
if event_type_str.startswith("Quiz"):
events.append({
'type': 'quiz',
'title': event_type_str, # e.g. "Quiz1"
'date': date_str
})
else:
# "會考"
events.append({
'type': 'exam',
'title': event_type_str, # "會考"
'date': date_str
})
return events
def generate_ics(
llm_text,
start_year=2025,
quiz_time=("10:00", "12:00"),
exam_time=("10:00", "12:00"),
alarm_minutes=15
):
"""
Generates an iCalendar (ICS) file text based on the LLM output.
- llm_text: text with lines like "Quiz1: 03/05" or "會考: 03/25"
- start_year: integer year to prepend to MM/DD
- quiz_time, exam_time: ("HH:MM", "HH:MM") for start/end times
- alarm_minutes: int for the VALARM trigger (minutes before event)
"""
events = parse_llm_output(llm_text)
# Build ICS lines
ics_lines = []
ics_lines.append("BEGIN:VCALENDAR")
ics_lines.append("VERSION:2.0")
ics_lines.append("PRODID:-//MyOrganization//ClassCalendar//EN")
ics_lines.append("CALSCALE:GREGORIAN")
# Helper to create datetime objects
def parse_date(month_day, start_or_end):
"""
month_day: "MM/DD"
start_or_end: "start" or "end" (to decide which time range to use)
"""
mm, dd = month_day.split('/')
hour_min = quiz_time if current_event['type'] == 'quiz' else exam_time
if start_or_end == "start":
time_str = hour_min[0] # e.g. "10:00"
else:
time_str = hour_min[1] # e.g. "12:00"
HH, MM = time_str.split(':')
# Construct a datetime (we assume the same year "start_year")
return datetime.datetime(
year=start_year,
month=int(mm),
day=int(dd),
hour=int(HH),
minute=int(MM),
second=0
)
# Build VEVENT blocks
for current_event in events:
# parse date/time
dtstart = parse_date(current_event['date'], "start")
dtend = parse_date(current_event['date'], "end")
# Format date/time in UTC or local. Typically ICS is in UTC or floating time.
# Here, let's keep it "floating" (no TZ) for simplicity:
dtstart_str = dtstart.strftime("%Y%m%dT%H%M%S")
dtend_str = dtend.strftime("%Y%m%dT%H%M%S")
# unique ID
uid_str = str(uuid.uuid4())
# dtstamp is "now"
dtstamp_str = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
ics_lines.append("BEGIN:VEVENT")
ics_lines.append(f"UID:{uid_str}")
ics_lines.append(f"DTSTAMP:{dtstamp_str}")
ics_lines.append(f"DTSTART:{dtstart_str}")
ics_lines.append(f"DTEND:{dtend_str}")
ics_lines.append(f"SUMMARY:微積分 {current_event['title']}")
# Add an alarm
ics_lines.append("BEGIN:VALARM")
ics_lines.append("ACTION:DISPLAY")
ics_lines.append(f"DESCRIPTION:Reminder for {current_event['title']}")
ics_lines.append(f"TRIGGER:-PT{alarm_minutes}M")
ics_lines.append("END:VALARM")
ics_lines.append("END:VEVENT")
# End of VCALENDAR
ics_lines.append("END:VCALENDAR")
# Return as a single string
return "\n".join(ics_lines)
if __name__ == "__main__":
with open("llm_output.txt", "r", encoding="utf-8") as f:
llm_output = f.read()
ics_content = generate_ics(
llm_output,
start_year=2025,
quiz_time=("10:00", "12:00"),
exam_time=("10:00", "12:00"),
alarm_minutes=15
)
with open("schedule.ics", "w", encoding="utf-8") as f:
f.write(ics_content)
print("ICS file generated: schedule.ics")