feat: 新增Notion自动化综合案例示例代码
This commit is contained in:
11
projects/notion-automator/pytion/schemas/__init__.py
Normal file
11
projects/notion-automator/pytion/schemas/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from .api import NotionAPI, PageSchema, ParentSchema # noqa: ignore
|
||||
from .property import ( # noqa: ignore
|
||||
IdProvider,
|
||||
NumberProperty,
|
||||
Property,
|
||||
RichTextProperty,
|
||||
StringProperty,
|
||||
TextProperty,
|
||||
URLProperty,
|
||||
)
|
||||
from .sspai import NewThingSchema, ProductInfoSchema # noqa: ignore
|
||||
25
projects/notion-automator/pytion/schemas/api.py
Normal file
25
projects/notion-automator/pytion/schemas/api.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from pydantic import BaseModel
|
||||
from pytion._types import DictLike
|
||||
|
||||
__all__ = ("NotionAPI", "ParentSchema", "PageSchema")
|
||||
|
||||
BASE_URL = "https://api.notion.com/v1"
|
||||
|
||||
|
||||
class NotionAPI:
|
||||
# databases
|
||||
QUERY_DATABASE_BY_ID: str = BASE_URL + "/databases/{database_id}/query"
|
||||
|
||||
# pages
|
||||
QUERY_PAGE_BY_ID: str = BASE_URL + "/pages/{page_id}"
|
||||
CREATE_PAGE: str = BASE_URL + "/pages"
|
||||
|
||||
|
||||
class ParentSchema(BaseModel):
|
||||
type: str = "database_id"
|
||||
database_id: str
|
||||
|
||||
|
||||
class PageSchema(BaseModel):
|
||||
parent: ParentSchema
|
||||
properties: DictLike
|
||||
74
projects/notion-automator/pytion/schemas/property.py
Normal file
74
projects/notion-automator/pytion/schemas/property.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import random
|
||||
from string import ascii_letters, digits, punctuation
|
||||
from typing import List, Optional, Union
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pytion._types import Numeric
|
||||
|
||||
|
||||
# ===============
|
||||
# Base type
|
||||
# ===============
|
||||
class IdProvider:
|
||||
|
||||
letters = "".join([ascii_letters, punctuation, digits])
|
||||
|
||||
def __init__(self, version="string") -> None:
|
||||
self.version = version
|
||||
|
||||
def __call__(self, *args, **kwargs) -> str:
|
||||
if self.version == "string":
|
||||
return "".join(random.choices(self.letters, k=5))
|
||||
return str(uuid4())
|
||||
|
||||
|
||||
class Property(BaseModel):
|
||||
id: Union[str, UUID] = Field(default_factory=IdProvider())
|
||||
type: Optional[str]
|
||||
|
||||
|
||||
# ===============
|
||||
# String type
|
||||
# ===============
|
||||
class Content(BaseModel):
|
||||
content: str
|
||||
|
||||
|
||||
class TextProperty(BaseModel):
|
||||
type: str = "text"
|
||||
text: Content
|
||||
|
||||
|
||||
class RichTextProperty(Property):
|
||||
type: str = "rich_text"
|
||||
rich_text: List[TextProperty]
|
||||
|
||||
|
||||
class TitleProperty(Property):
|
||||
type: str = "title"
|
||||
title: List[TextProperty]
|
||||
|
||||
|
||||
class StringProperty(dict):
|
||||
def __new__(cls, value: str, type=None, *args, **kwargs):
|
||||
data = [TextProperty(text=Content(content=value))]
|
||||
if type == "title":
|
||||
return TitleProperty(title=data)
|
||||
return RichTextProperty(rich_text=data)
|
||||
|
||||
|
||||
# ===============
|
||||
# Numeric type
|
||||
# ===============
|
||||
class NumberProperty(Property):
|
||||
type: str = "number"
|
||||
number: Numeric
|
||||
|
||||
|
||||
# ===============
|
||||
# URL type
|
||||
# ===============
|
||||
class URLProperty(Property):
|
||||
type: str = "url"
|
||||
url: Optional[str]
|
||||
18
projects/notion-automator/pytion/schemas/sspai.py
Normal file
18
projects/notion-automator/pytion/schemas/sspai.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
__all__ = ("NewThingSchema", "ProductInfoSchema")
|
||||
|
||||
|
||||
class ProductInfoSchema(BaseModel):
|
||||
price: Optional[str] = None
|
||||
source: Optional[str] = None
|
||||
|
||||
|
||||
class NewThingSchema(ProductInfoSchema):
|
||||
id: Optional[int] = Field(default_factory=int)
|
||||
name: str
|
||||
author: str
|
||||
url: Optional[str] = None
|
||||
issue: Optional[int] = None
|
||||
Reference in New Issue
Block a user