Skip to content
/ potion Public

A functional, easy to use Python wrapper of Notion Api.

License

Notifications You must be signed in to change notification settings

sailist/potion

Repository files navigation

Potion

A functional, easy to use Python wrapper of Notion Api.

  • easy-to-use low-level api
  • integration local-backup function
  • render notion object to markdown/html, etc..

Install

pip install notion-potion

Usage

Quick Start

Authentication

from potion import Request, NotionHeader

token = ''

nh = NotionHeader(authorization=token)
req = Request(nh.headers)

Create an integration follow this official tutorial to get token.

A 'retrieve' example. (Full code can be found here)

from potion.api import *

print(req.get(url=database_retrieve('1bb0f79b87584afe8609d6e248285cfb')))

And a 'update' example. (Full code)

from potion.api import *
from potion.objects import *

property_list = [
    sche.AnySchema('+1', args=Null),  # delete property
    sche.URL('Photo'),  # update property type of `Photo`
    sche.MultiSelect('Store availability', [  # update Options of MultiSelect perproty `Store availability`
        sche.Option('Duc Loi Market'),
        sche.Option('Rainbow Grocery'),
        sche.Option('Gus\'s Community Market'),
        sche.Option('The Good Life Grocery', color='orange'),
    ])
]
properties = Properties(*property_list)

# Create Database object
data = Database(properties=properties)

# Commit update operation
print(data)
print(req.patch(url=database_update('1bb0f79b87584afe8609d6e248285cfb'),
                data=data))

Basic Example

Here lists examples reimplemented by potion from official shell examples

Database Pages Blocks Users Search Comment
Query a Database Retrieve a page Retrieve a block Retrieve a user Search Retrieve Comments
Create a database Create a page Update a block List all users Create Comment
Retrieve a database Update page Retrieve block children Retrieve your token's bot user
Update database Retrieve a page property item Append block children
Delete a block

Object Oriented Api

potion also provides object oriented operations for Notion page and Notion database:

from potion.beans import NotionPage, NotionDatabase

token = ''
# Retrieve
page = NotionPage(auth=token, id=...)
# Create a new one
page = NotionPage(auth=token, parent=...)
print(page)

# set property
page.set_text('title', 'temp')
page.set_number('End', 42)
page.flush_property()

# append content
page.append_code("""print('hello world!')""", 'python')
page.flush_children()

# page.parent
## page.parent_database
## page.parent_page
database = page.create_database()
# TODO
# database operations
database.add_property()
database.update_property()
database.create_page()

Backup

python -m potion.backup {token} {backup_dir}
# python -m potion.backup secret_umqPgKzCvvCaAc1FE408aRvYHymxaak5HriWIvaVzs ./backup

It will use dbrecord to generate two sqlite database file under backup_dir.

You can read backuped data simpily by using the code below:

from dbrecord import PList

backup_dir = ...
lis = PList(f'{backup_dir}/notion-backup.sqlite')
print(lis[0])

Api List

See api.md for details.

Development & Contribute

git clone https://github.com/sailist/potion
python setup

Any issue and pr is welcome.

TODOs

  • Some uncommon used Object, like Filter, Emoji, File, etc..(While, you can implement your idea without this Object by pass dict as args.)
  • Parse json responses recurrsive into notion objects.

Related