-
Notifications
You must be signed in to change notification settings - Fork 2
/
notion.py
173 lines (132 loc) Β· 4.04 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
164
165
166
167
168
169
170
171
172
173
import json
import requests
from decouple import config
def read_database(database_id, headers):
"""Read the whole Notion DB"""
read_url = f"https://api.notion.com/v1/databases/{database_id}/query"
res = requests.request("POST", read_url, headers=headers)
data = res.json()
for obj in data['results']:
print(obj['properties']['ID']['number'])
def search_db(database_id, issue_id, headers):
"""Search for a Card in Notion DB"""
query_url = f"https://api.notion.com/v1/databases/{database_id}/query"
query_params = {
"database_id": database_id,
"filter": {
"property": 'ID',
"number": {
"equals": issue_id
}
}
}
data = json.dumps(query_params)
response = requests.request("POST", query_url, headers=headers, data=data)
print(response.status_code)
response = response.json()
return response['results'][0]['id']
def move_2_completed(page_id, headers):
"""Move a page or card to completed coloum"""
update_url = f"https://api.notion.com/v1/pages/{page_id}"
update_data = {
"properties": {
"Status":{
"id": "=_;M",
"type": "select",
"select": {
"id": "3",
"name": "Completed",
"color": "green"
}
}
}
}
data = json.dumps(update_data)
response = requests.request("PATCH", update_url, headers=headers, data=data)
print(response.status_code)
print(response.text)
def move_2_open(page_id, headers):
"""Add a new page or card in open coloum"""
update_url = f"https://api.notion.com/v1/pages/{page_id}"
update_data = {
"properties": {
"Status":{
"id": "=_;M",
"type": "select",
"select": {
"id": "1",
"name": "Open Issues",
"color": "red"
}
}
}
}
data = json.dumps(update_data)
response = requests.request("PATCH", update_url, headers=headers, data=data)
print(response.status_code)
print(response.text)
def create_page(database_id, headers, title, issue_url, issue_id):
"""Create a new Page"""
create_url = 'https://api.notion.com/v1/pages'
new_page_data = {
"parent": { "database_id": database_id },
"properties": {
"Name": {
"title": [
{
"text": {
"content": f"{title}"
}
}
]
},
"Status":{
"id": "=_;M",
"type": "select",
"select": {
"id": "1",
"name": "Open Issues",
"color": "red"
}
},
"URL": {
"id": "U>Vt",
"type": "url",
"url": f"{issue_url}"
},
"ID": {
"id": "eEq_",
"type": "number",
"number": issue_id}}
}
data = json.dumps(new_page_data)
res = requests.request("POST", create_url, headers=headers, data=data)
print("Status: ", res.status_code)
print(res.text)
def get_bearer(code):
"""Get User's Bearer Code"""
request_url = "https://api.notion.com/v1/oauth/token"
body_data = {
"grant_type" : "authorization_code",
"code" : f'{code}',
}
token = config('NOTION_BASIC')
oauth_header = {
'Authorization': f'Basic {token}'
}
response = requests.post(request_url, headers=oauth_header, data=body_data)
response = response.json()
return response['access_token']
def get_page_id(auth_token):
"""Get Notion's Page ID"""
search_url = "https://api.notion.com/v1/search"
search_header = {
"Authorization": f"{auth_token}",
"Notion-Version": "2021-08-16",
"Content-Type": "application/json"
}
search_body = {}
response = requests.post(search_url, headers=search_header, data=search_body)
response = response.json()
db_id = response['results'][0]['id']
return db_id