Skip to content

Commit

Permalink
1. notion api迁移, searchTrashPages to search
Browse files Browse the repository at this point in the history
2. batch delete blocks

Change-Id: I5224c190a34eb2828261c0f0f4939cc2703ee0c1
  • Loading branch information
朱静 committed Nov 21, 2020
1 parent 60ea92b commit de6fa35
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions notion-clear-trash.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,43 @@

def get_trash(client):
query = {
'query': '',
'limit': 1000,
'spaceId': client.current_space.id
}
results = client.post('/api/v3/searchTrashPages', 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_ids
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_id in block_ids:
client.post("deleteBlocks", {"blockIds": [block_id], "permanentlyDelete": True})
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__":
Expand All @@ -24,5 +48,4 @@ def delete_permanently(client, block_ids):

block_ids = get_trash(client)
delete_permanently(client, block_ids)

print('Successfully cleared all trash blocks.')

0 comments on commit de6fa35

Please sign in to comment.