-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion_service.py
More file actions
77 lines (60 loc) · 2.25 KB
/
notion_service.py
File metadata and controls
77 lines (60 loc) · 2.25 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
import os
import requests
from dotenv import load_dotenv
from notion_setup import create_task
from .calendar_service import create_calendar_event, format_due_display
load_dotenv()
NOTION_API_KEY = os.getenv("NOTION_API_KEY")
DATABASE_ID = os.getenv("NOTION_DB_ID") or os.getenv("NOTION_DATABASE_ID")
def get_all_tasks_debug():
if not NOTION_API_KEY or not DATABASE_ID:
return []
url = f"https://api.notion.com/v1/databases/{DATABASE_ID}/query"
headers = {
"Authorization": f"Bearer {NOTION_API_KEY}",
"Notion-Version": "2022-06-28",
}
response = requests.post(url, headers=headers)
if not response.ok:
print(f"Notion query failed: {response.status_code} {response.text}")
return []
return response.json().get("results", [])
def send_to_notion(assignments):
for assignment in assignments:
if not isinstance(assignment, dict):
print(f"Skipping invalid assignment payload: {assignment}")
continue
raw_title = (assignment.get("title") or "").strip()
if not raw_title:
print(f"Skipping assignment with missing title: {assignment}")
continue
title = raw_title[:100]
due_date = assignment.get("due_date")
time = assignment.get("time", "18:00:00")
subject = assignment.get("subject", "General")
try:
create_task(
title=title,
due_date=due_date,
subject=subject,
description=assignment.get("description"),
priority=assignment.get("priority", "LOW"),
)
except Exception as error:
print(f"Notion: ERROR failed to create task '{title}': {error}")
continue
print(f"Task created: {title}")
if due_date:
print(f"Due: {format_due_display(due_date, time)}")
else:
print("Due: Not found")
print(f"Subject: {subject}")
print("Notion: OK Task added")
if due_date:
create_calendar_event(
title=title,
due_date=due_date,
description=assignment.get("description", ""),
time=time,
)
print("\n----------------------\n")