jamalex / notion-py

Unofficial Python API client for Notion.so
MIT License
4.29k stars 474 forks source link

Access page history #206

Open timsgardner opened 4 years ago

timsgardner commented 4 years ago

Notion tracks page history (at least for paid plans). This can be useful for determining when a task was completed, for example. Is there a way to access page history with the API? Thank you.

aviral-batra commented 3 years ago

@jamalex @timsgardner

I'm not sure if this is the best way to do it but if you run the code below, I think you can get the different 'snapshots' of pages.

Snapshots stand for the different versions of the page at a particular timestamp. In the code I have submitted a request to get the list of snapshots and their timestamps for a specific block id/page. Looking at the dev tools in the browser, I saw that to get a specific 'snapshot', you need to provide the timestamp.

Now after entering the timestamp and block id along with the request 'getSnapshot' I saw the request returned a signed url. I made a simple get request to that signed url (returning the block structure for each snapshot, stored in snapshot.json()).

from notion.client import NotionClient
import requests

client = NotionClient(token_v2=token_v2)  # put the actual token v2 in place of token_v2
req = {"blockId": block_id, "size": 50}  # put the actual block id in place of block_id
snapshots_list = client.post("getSnapshotsList", req).json()

for snapshot in snapshots_list['snapshots']:
    req2 = {"blockId": block_id, "timestamp": snapshot['timestamp']}
    response = client.post("getSnapshot", req2).json()
    for the_only_signed_url in response['signedUrls']:
        snapshot = requests.get(the_only_signed_url)
        # print(snapshot.json())
odusseys commented 3 years ago

The API method to do this is getActivityLog FYI