-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbriefing_service.py
More file actions
192 lines (143 loc) · 5.28 KB
/
briefing_service.py
File metadata and controls
192 lines (143 loc) · 5.28 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from datetime import datetime, timedelta
from app.ai_service import generate_briefing
from app.notion_service import get_all_tasks_debug
def deduplicate_tasks(tasks):
unique = {}
for task in tasks:
props = task["properties"]
try:
raw_title = props["Name"]["title"][0]["text"]["content"]
except Exception:
continue
clean = raw_title.lower().strip()
clean = clean.replace("semester ii", "")
clean = clean.replace("(1mh201cc25)", "")
clean = clean.replace("-", " ")
clean = " ".join(clean.split())
try:
due_date = props["Due Date"]["date"]["start"]
except Exception:
continue
if clean not in unique or due_date < unique[clean]["raw_due_date"]:
unique[clean] = {
"title": raw_title,
"raw_due_date": due_date,
}
return list(unique.values())
def clean_title(title):
if not title:
return None
title = title.strip()
title = " ".join(title.split())
words = []
for word in title.split():
if word.isupper() and len(word) <= 5:
words.append(word)
else:
words.append(word.capitalize())
title = " ".join(words)
bad_keywords = ["event", "venue", "time", "schedule"]
title_lower = title.lower()
if any(word in title_lower for word in bad_keywords) and len(title.split()) > 6:
return None
return title
def format_date(date_str):
try:
date = datetime.fromisoformat(date_str)
return date.strftime("%b %d")
except Exception:
return date_str
def categorize_tasks(raw_tasks):
today = datetime.today().date()
tasks = deduplicate_tasks(raw_tasks)
today_tasks = []
upcoming_tasks = []
overdue_tasks = []
for task in tasks:
title = clean_title(task["title"])
if not title:
continue
try:
due_date = datetime.fromisoformat(task["raw_due_date"]).date()
except Exception:
continue
task_data = {
"title": title,
"due_date": format_date(task["raw_due_date"]),
"raw_due_date": task["raw_due_date"],
}
if due_date == today:
today_tasks.append(task_data)
elif due_date > today:
upcoming_tasks.append(task_data)
else:
overdue_tasks.append(task_data)
upcoming_tasks.sort(key=lambda x: x["raw_due_date"])
overdue_tasks.sort(key=lambda x: x["raw_due_date"])
today_tasks.sort(key=lambda x: x["raw_due_date"])
upcoming_tasks = upcoming_tasks[:5]
return today_tasks, upcoming_tasks, overdue_tasks
def generate_insights(today_tasks, upcoming_tasks, overdue_tasks):
insights = []
if len(overdue_tasks) > 0:
insights.append(f"You have {len(overdue_tasks)} overdue task(s). Clear them first.")
if len(today_tasks) > 2:
insights.append("Busy day ahead. Prioritize wisely.")
elif len(today_tasks) == 0:
insights.append("No tasks due today. Great chance to get ahead.")
date_count = {}
for task in upcoming_tasks:
date = task["due_date"]
date_count[date] = date_count.get(date, 0) + 1
for date, count in date_count.items():
if count >= 3:
insights.append(f"Heavy workload on {date} ({count} tasks). Start early.")
tomorrow = datetime.today().date() + timedelta(days=1)
for task in upcoming_tasks:
due = datetime.fromisoformat(task["raw_due_date"]).date()
if due == tomorrow:
insights.append(f"'{task['title']}' is due tomorrow. Start now.")
return insights
def build_demo_tasks():
today = datetime.today().date()
def make_task(title, due_date):
return {
"properties": {
"Name": {
"title": [{"text": {"content": title}}],
},
"Due Date": {
"date": {"start": due_date.isoformat()},
},
}
}
return [
make_task("Operating Systems Viva Prep", today - timedelta(days=2)),
make_task("DBMS Assignment Final Edit", today - timedelta(days=1)),
make_task("Computer Networks Lab Record", today),
make_task("AI Quiz Revision", today),
make_task("Data Structures Worksheet", today + timedelta(days=1)),
make_task("Math Tutorial Submission", today + timedelta(days=3)),
make_task("Mini Project Checkpoint", today + timedelta(days=5)),
]
def run_briefing(use_demo_data=False):
print("Morning Briefing\n")
tasks = build_demo_tasks() if use_demo_data else get_all_tasks_debug()
if not tasks:
print("No tasks found.")
return
today_tasks, upcoming_tasks, overdue_tasks = categorize_tasks(tasks)
print(
f"Summary: Today={len(today_tasks)}, "
f"Upcoming={len(upcoming_tasks)}, "
f"Overdue={len(overdue_tasks)}"
)
insights = generate_insights(today_tasks, upcoming_tasks, overdue_tasks)
briefing = generate_briefing(today_tasks, upcoming_tasks, overdue_tasks)
print("\nYour Day\n")
if insights:
print("Insights\n")
for insight in insights:
print(f"- {insight}")
print("\n----------------------\n")
print(briefing)