GithubHelp home page GithubHelp logo

Comments (5)

eoranged avatar eoranged commented on July 29, 2024

You can use a snippet from Readme for each page:

for page_id in space.pages:
    page = client.get_block(page_id)
    print(page.title)
    for child in page.children:
        print(child.title)

from notion-py.

eoranged avatar eoranged commented on July 29, 2024

After digging a bit deeper, I found that this is not enough.
What you need is to call client.post("loadUserContent", {}).json()["recordMap"]["block"].keys() to fetch ids of all pages and then iterate through all the children blocks of types 'page' and 'collection'. Maybe there are more block types that displayed as pages (like collection_view), but I didn't need them for my task.
This is a dirty and inefficient but working example of how to do this — https://github.com/eoranged/notion-scripts/blob/master/audit.py

from notion-py.

jamalex avatar jamalex commented on July 29, 2024

I've added a client.get_top_level_pages method in #87 based on @eoranged's code suggestion. This will be released soon in a new version.

For the recursion, you can just use standard recursion to loop down through the descendants to do whatever you like with them. For example:

def recursively_print_titles(block, depth=0):
    print("\t" * depth + block.title)
    for child in block.children:
        if child.type in ["page", "collection"]:
            recursively_print_titles(child, depth=depth+1)

for block in client.get_top_level_pages():
    recursively_print_titles(block)

(but it's going to be slowwwww, if you have a ton of pages)

EDIT: added conditional so it wouldn't include other types of blocks (e.g. page contents)

from notion-py.

odusseys avatar odusseys commented on July 29, 2024

I believe this get_top_level_pages method is broken. It's pretty tough to replicate but I am getting plenty of pages which are not top-level pages in some cases (as attested by the fact that the parent_table is not 'space'). I think it needs a check on the parent_table to work (would also be interested to know why it loads so many more unneccessary blocks already).

from notion-py.

rishabhjaiz avatar rishabhjaiz commented on July 29, 2024

Is there a way for an third party app integration to access list of all page ids?

from notion-py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.