-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotionClient.py
61 lines (52 loc) · 1.94 KB
/
NotionClient.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
import requests
from datetime import datetime
from icalendar import Calendar, Event
import tempfile, os
class NotionClient:
def __init__(self, _token):
self.cal = Calendar()
self.TOKEN = _token
def get_database(self, database_id):
url = f"https://api.notion.com/v1/databases/{database_id}/query"
headers = {
"Authorization": f"Bearer {self.TOKEN}",
"Notion-Version": "2021-08-16",
"Content-Type": "application/json"
}
data = {
"query": "Class Deadlines",
"sort": {
"direction":"ascending",
"timestamp":"last_edited_time"
}
}
events = []
items = requests.post(url, headers=headers).json()["results"]
for item in items:
type_ = item["properties"]["Type"]["select"]["name"] # if its Assignment | Exam -> Make `title` all CAPS
class_name = item["properties"]["Class"]["select"]["name"]
title = item["properties"]["Name"]["title"][0]["plain_text"]
if type_ == "Assignment" or type_ == "Exam":
title.upper()
date = item["properties"]["Date"]["date"]["start"]
date = datetime.strptime(date, '%Y-%m-%d')
url = item["url"]
events.append(
{
"title": f"{title} [{class_name}]",
"date": date,
"url": url,
}
)
self.export_ical(events)
def export_ical(self, items):
# Add events to ical
for item in items:
event = Event()
event.add("summary", item["title"])
event.add("dtstart", item["date"].date())
self.cal.add_component(event)
# Write to disk
with open("Notion.ics", "wb") as f:
f.write(self.cal.to_ical())
print("Completely Wrote to Disk")