-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotion.py
163 lines (138 loc) · 4.67 KB
/
notion.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
152
153
154
155
156
157
158
159
160
161
162
163
import requests, json
class NotionApi:
def __init__(
self,
notionToken=None,
database_id=None,
schoolAb=None,
version="2021-08-16",
):
self.database_id = database_id
self.notionToken = notionToken
self.schoolAb = schoolAb
self.notionHeaders = {
"Authorization": "Bearer " + notionToken,
"Content-Type": "application/json",
"Notion-Version": "2021-08-16",
}
def queryDatabase(self):
readUrl = f"https://api.notion.com/v1/databases/{self.database_id}/query"
res = requests.request("POST", readUrl, headers=self.notionHeaders)
data = res.json()
with open("./db.json", "w", encoding="utf8") as f:
json.dump(data, f, ensure_ascii=False)
return data
def test_if_database_id_exists(self):
res = requests.request(
"GET",
f"https://api.notion.com/v1/databases/{self.database_id}/",
headers=self.notionHeaders,
)
return json.loads(res.text)["object"] != "error"
# Creates a new database in page_id page built for Canvas assignments and returns it's database_id
def createNewDatabase(self, page_id):
newPageData = {
"parent": {
"type": "page_id",
"page_id": page_id,
},
"icon": {"type": "emoji", "emoji": "🔖"},
"cover": {
"type": "external",
"external": {"url": "https://website.domain/images/image.png"},
},
"title": [
{
"type": "text",
"text": {
"content": "Canvas Assignments",
"link": None,
},
}
],
"properties": {
"State": {
"formula": {
"expression": '(dateBetween(prop("Due Date"), now(), "days") == 0) ? "🟧" : ((dateBetween(prop("Due Date"), now(), "days") < 0) ? "🟥" : "🟩")'
}
},
"Status": {
"select": {
"options": [
{"name": "To Do", "color": "pink"},
{"name": "In Progress", "color": "red"},
{"name": "Completed", "color": "green"},
]
}
},
"Assignment": {"title": {}},
"Class": {
"type": "select",
"select": {"options": []},
},
"Due Date": {"date": {}},
"URL": {"url": {}},
"Notes": {"rich_text": {}},
},
}
data = json.dumps(newPageData)
res = requests.request(
"POST",
"https://api.notion.com/v1/databases",
headers=self.notionHeaders,
data=data,
)
print(res.text)
newDbId = json.loads(res.text).get("id")
return newDbId
def createNewDatabaseItem(
self,
id,
className,
assignmentName,
status,
url=None,
dueDate=None,
):
if dueDate["start"] == None:
dueDate = None
# if status:
# status = "To do"
# else:
# status = "Completed"
createUrl = "https://api.notion.com/v1/pages"
newPageData = {
"parent": {"database_id": self.database_id},
"properties": {
"Status": {"select": {"name": status, "color": "pink"}},
"Assignment": {
"type": "title",
"title": [
{
"text": {
"content": assignmentName,
},
}
],
},
"Class": {
"select": {
"name": className,
}
},
"Due Date": {"date": dueDate},
"URL": {
"url": url,
},
},
}
data = json.dumps(newPageData)
res = requests.request("POST", createUrl, headers=self.notionHeaders, data=data)
print(res.text)
return res
def parseDatabaseForAssignments(self):
urls = []
if self.queryDatabase().get("results") != None:
for item in self.queryDatabase().get("results"):
urls.append(item["properties"]["URL"]["url"])
return urls