feat: 新增Notion自动化综合案例示例代码

This commit is contained in:
100gle
2022-09-10 17:41:03 +08:00
parent ccc37da694
commit c3a251aca5
18 changed files with 2998 additions and 0 deletions

View 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

View 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

View 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]

View 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