8000 GitHub - YunAssistant/NotionSDKpy: 基于Notion SDK的Python API,支持创建数据库以及Filter查询
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

基于Notion SDK的Python API,支持创建数据库以及Filter查询

License

Notifications You must be signed in to change notification settings

YunAssistant/NotionSDKpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NotionSDKpy

基于Notion SDK的Python API,支持创建数据库以及Filter查询

前言

该项目为最近的一个项目里抽离出来的NotionSDK,一些功能可能不具有通用性。但底层接口都设计成了可扩展的形式,支持后续改进。

整个项目基于ramnes的SDK进行开发

  • 解决了原版SDK库使用Requests库时可能会遇到的ProxyError问题
  • 增加了Filter,支持复杂查询规则

使用方法

下面简单说明一下使用方法,目前仍在开发阶段,后续会更新完整教程(#TODO)

from notion_client import Client, Filter
NOTION_TOKEN = os.getenv("NOTION_TOKEN", "")

# 初始化 Client
notion = Client(auth=NOTION_TOKEN)

# 初始化 Filter 
list = Filter.rule_list
list["property_name"] = "type" # 增加对应property name的type

Client创建数据库、查询数据库:

def example():
    # Search for an item
    print("\nSearching for the word 'People' ")
    results = notion.search(query="People").get("results")
    print(len(results))
    result = results[0]
    print("The result is: ", result["object"])
    pprint(result["properties"])

    database_id = result["id"]  # store the database id in a variable for future use

    # Create a new page
    your_name = "123123123"
    gh_uname = "123123123123123123123"
    new_page = {
        "Name": {"title": [{"text": {"content": your_name}}]},
        "Tags": {"type": "multi_select", "multi_select": [{"name": "python"}]},
        "GitHub": {
            "type": "rich_text",
            "rich_text": [
                {
                    "type": "text",
                    "text": {"content": gh_uname},
                },
            ],
        },
    }
    notion.pages.create(parent={"database_id": database_id}, properties=new_page)
    print("You were added to the People database!")

    # Query a database
    name = input("\n\nEnter the name of the person to search in People: ")
    results = notion.databases.query(
        **{
            "database_id": database_id,
            "filter": {"property": "Name", "text": {"contains": name}},
        }
    ).get("results")

    no_of_results = len(results)

    if no_of_results == 0:
        print("No results found.")
        sys.exit()

    print(f"No of results found: {len(results)}")

    result = results[0]

    print(f"The first result is a {result['object']} with id {result['id']}.")
    print(f"This was created on {result['created_time']}")

Filter首先通过list增加property_nametype的关系,比如自定义Film_Title对应的是text,则可以直接初始化时添加到list中,查询的时候使用:Film_Title==神秘海域进行查询,若要进行复杂查询方式,则使用:(Film_Title==神秘海域&Tags\c在看)|(Tags\c心愿单)

目前的开发支持multi_selecttextnumber三种类型,以及对应规则如下:

image-20220424124757945

具体查询时可使用:

my_filter = Filter.make_filter("string")
results = notion.databases.query(
    **{
        "database_id": database_id,
        "filter": my_filter, # 自定义查询方式
    }
).get("results")
pprint(results) # 打印结果

About

基于Notion SDK的Python API,支持创建数据库以及Filter查询

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0