-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnotion-clear-trash.py
51 lines (41 loc) · 1.48 KB
/
notion-clear-trash.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
from notion.client import NotionClient
def get_trash(client):
query = {
"type": "BlocksInSpace",
"query": "",
"filters": {
"isDeletedOnly": True,
"excludeTemplates": False,
"isNavigableOnly": True,
"requireEditPermissions": False,
"ancestors": [],
"createdBy": [],
"editedBy": [],
"lastEditedTime": {},
"createdTime": {}
},
"sort": "Relevance",
"limit": 1000,
"spaceId": client.current_space.id,
"source": "trash"
}
results = client.post('/api/v3/search', query)
block_ids = results.json()['results']
return [block_id['id'] for block_id in block_ids]
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def delete_permanently(client, block_ids):
for block_batch in chunks(block_ids, 10):
try:
client.post("deleteBlocks", {"blockIds": block_batch, "permanentlyDelete": True})
except Exception as err:
print(err)
print(block_batch)
if __name__== "__main__":
token = input('Please enter your auth token: ')
client = NotionClient(token_v2=token)
block_ids = get_trash(client)
delete_permanently(client, block_ids)
print('Successfully cleared all trash blocks.')