forked from aravinds-arv/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notion.py
105 lines (86 loc) · 2.84 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
import json
import requests
class NotionClient:
def __init__(self,token,database_id):
self.database_id = database_id
self.token = token
self.headers = {
"Accept": "application/json",
"Notion-Version": "2022-02-22",
"Content-Type": "application/json",
"Authorization": "Bearer "+token
}
def map_properties(self, result):
task_id = result['id']
properties = result['properties']
task_num = properties['Task ID']['number']
description = properties['Description']['title'][0]['text']['content']
date = properties['Date']['date']['start']
status = properties['Status']['checkbox']
return {
'task_id': task_id,
'task_num': task_num,
'description': description,
'status': status,
'date': date
}
def get_pages(self):
query_url = f'https://api.notion.com/v1/databases/{self.database_id}/query'
res = requests.post(query_url, headers=self.headers)
res_dict = res.json()
tasks = res_dict['results']
tasks_list = []
for task in tasks:
task_dict = self.map_properties(task)
tasks_list.append(task_dict)
return tasks_list
def create_page(self,task_id,description,date,status):
create_url = "https://api.notion.com/v1/pages"
data = {
"parent": { "database_id": self.database_id },
"properties": {
"Task ID": {
"number": task_id
},
"Description": {
"title": [
{
"text": {
"content": description
}
}
]
},
"Date": {
"date": {
"start": date,
"end": None
}
},
"Status": {
"checkbox": status
}
}}
data = json.dumps(data)
res = requests.post(create_url,headers=self.headers,data=data)
return res
def update_page(self, task_id, status):
update_url = f"https://api.notion.com/v1/pages/{task_id}"
data = {
"parent": { "database_id": self.database_id },
"properties": {
"Status": {
"checkbox": status
}
}}
data = json.dumps(data)
res = requests.patch(update_url,headers=self.headers,data=data)
return res
def delete_page(self, task_id):
update_url = f"https://api.notion.com/v1/pages/{task_id}"
data = {
'archived' : True
}
data = json.dumps(data)
res = requests.patch(update_url,headers=self.headers,data=data)
return res