Skip to content

Notion unofficial

notionsci.connections.notion_unofficial special

client

NotionUnofficialClient

duplicate_page(self, source_id, parent, block_uuid=None)

Duplicates source page to a new child page of the given parent

Source code in notion_unofficial/client.py
def duplicate_page(self, source_id: ID, parent: Block, block_uuid: ID = None) -> Block:
    """
    Duplicates source page to a new child page of the given parent
    """
    block_id = block_uuid or str(uuid.uuid4())
    space_id = parent.space_info['spaceId']
    user_id = self.current_user.id
    parent_block_id = parent.id

    # Create a block with right copy indicator and user permission
    self.submit_transaction([
        op_copy_indicator(space_id, block_id),
        op_user_permission(block_id, user_id),
        op_parent_block(block_id, parent_block_id),
        op_list_after_block(block_id, parent_block_id)
    ])

    self.submit_duplicate_task(source_id, block_id)

    return self.get_block(block_id)
get_spaces(self)

Returns a list of spaces user has access to

Source code in notion_unofficial/client.py
def get_spaces(self) -> Iterator[Space]:
    """
    Returns a list of spaces user has access to
    """
    response: dict = self.post('getSpaces', {}).json()
    for data in response.values():
        for space_id, space in data['space'].items():
            yield self.get_space(space_id)
get_task_status(self, task_id)

Get a status of a single task

Source code in notion_unofficial/client.py
def get_task_status(self, task_id):
    """
    Get a status of a single task
    """
    data = self.post(
        "getTasks", {"taskIds": [task_id]}
    ).json()

    results = data.get("results")
    if results is None:
        return None

    if not results:
        # Notion does not know about such a task
        print("Invalid task ID.")
        return None

    if len(results) >= 1:
        return results[0].get("state")

    return None
submit_duplicate_task(self, source_id, target_id)

Submits a task to notion to duplicate a block to target' Target must have copy indicator and right permissions

Source code in notion_unofficial/client.py
def submit_duplicate_task(self, source_id: ID, target_id: ID):
    """
    Submits a task to notion to duplicate a block to target'
    Target must have copy indicator and right permissions
    """
    data = self.post("enqueueTask", {
        "task": {
            "eventName": "duplicateBlock",
            "request": {
                "sourceBlockId": source_id,
                "targetBlockId": target_id,
                "addCopyName": False,
                "deepCopyTransclusionContainers": True,
            }
        }
    }).json()

    task_id = data.get("taskId")
    if task_id:
        # Wait until the duplication task finishes
        self.wait_for_task(task_id)
wait_for_task(self, task_id, interval=1, tries=20)

Wait for a task by looping 'tries' times ever 'interval' seconds. The 'interval' parameter can be used to specify milliseconds using double (e.g 0.75).

Source code in notion_unofficial/client.py
def wait_for_task(self, task_id, interval=1, tries=20):
    """
    Wait for a task by looping 'tries' times ever 'interval' seconds.
    The 'interval' parameter can be used to specify milliseconds using double (e.g 0.75).
    """
    for i in range(tries):
        state = self.get_task_status(task_id)
        if state in ["not_started", "in_progress"]:
            sleep(interval)
        elif state == "success":
            return state
        else:
            raise Exception('Unexpected task state: {}'.format(state))

    print("Task takes more time than expected. Specify 'interval' or 'tries' to wait more.")
Back to top