From edb56cf360c5771bb0eb2592c660d858c8d98ac8 Mon Sep 17 00:00:00 2001 From: BiDuang Date: Fri, 19 Jul 2024 22:33:55 +0800 Subject: [PATCH] feat: Routers update --- Models/comic.py | 30 ++++++++++++++++-------------- Models/plugins.py | 2 +- Models/user.py | 3 +++ Routers/core.py | 6 +++++- Routers/user.py | 8 ++++---- Services/Modulator/manager.py | 10 ++++++---- pyproject.toml | 2 ++ 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Models/comic.py b/Models/comic.py index 16c4cb9..5a6a4e9 100644 --- a/Models/comic.py +++ b/Models/comic.py @@ -1,12 +1,13 @@ -from pydantic import BaseModel +from typing import Any -from typing import List, Optional, Dict, Any +from pydantic import BaseModel class BaseComicInfo(BaseModel): """BaseComicInfo""" + """作者,漫画作者""" - author: List[str] + author: list[str] """封面,漫画封面图片URL""" cover: str """标识符,漫画在所属平台的索引ID""" @@ -17,25 +18,26 @@ class BaseComicInfo(BaseModel): class ComicInfo(BaseModel): """ComicInfo""" + """章节数,漫画章节数""" - chapters: Optional[int] = None + chapters: int | None = None """评论量,漫画评论量""" - comments: Optional[int] = None + comments: int | None = None """简介,漫画简介""" - description: Optional[str] = None + description: str | None = None """额外信息,源平台携带的其它漫画信息""" - extras: Optional[Dict[str, Any]] = None + extras: dict[str, Any] | None = None """收藏量,漫画收藏量""" - favorites: Optional[int] = None + favorites: int | None = None """已收藏,漫画是否已收藏""" - is_favorite: Optional[bool] = None + is_favorite: bool | None = None """已完结,漫画是否已完结""" - is_finished: Optional[bool] = None + is_finished: bool | None = None """已阅读,漫画是否已阅读""" - is_viewed: Optional[bool] = None + is_viewed: bool | None = None """标签,漫画标签""" - tags: Optional[List[str]] = None + tags: list[str] | None = None """更新时间,漫画最近的更新时间戳""" - updated_at: Optional[int] = None + updated_at: int | None = None """阅读量,漫画阅读量""" - views: Optional[int] = None + views: int | None = None diff --git a/Models/plugins.py b/Models/plugins.py index 0d4c7e0..d4848a8 100644 --- a/Models/plugins.py +++ b/Models/plugins.py @@ -27,7 +27,7 @@ def search(self, keyword: str) -> list[BaseComicInfo]: class IAuth(ABC): @abstractmethod - async def login(self, body: dict[str, str], user: UserData) -> StandardResponse: + async def login(self, body: dict[str, str], user_data: UserData) -> StandardResponse: pass diff --git a/Models/user.py b/Models/user.py index 5f4f971..127deaa 100644 --- a/Models/user.py +++ b/Models/user.py @@ -20,6 +20,9 @@ def __init__(self, uid: str, plugin_cookies: dict[str, BaseCookie[str]]): self.uid = uid self.plugin_cookies = plugin_cookies + def set_src_cookies(self, src: str, cookies: BaseCookie[str]) -> None: + self.plugin_cookies[src] = cookies + def get_src_cookies(self, src: str) -> BaseCookie[str]: if src in self.plugin_cookies: return self.plugin_cookies[src] diff --git a/Routers/core.py b/Routers/core.py index a77f789..ffcab9e 100644 --- a/Routers/core.py +++ b/Routers/core.py @@ -12,5 +12,9 @@ @core_router.get("/ping") -async def get_status() -> StandardResponse: +async def get_status(): return StandardResponse(message="pong") + +@core_router.get("/protocol") +async def get_cnm_version(): + pass \ No newline at end of file diff --git a/Routers/user.py b/Routers/user.py index 3c6118c..b370d89 100644 --- a/Routers/user.py +++ b/Routers/user.py @@ -129,7 +129,7 @@ async def encrypt_src_pwd( record: PwdDb | None = ( db.query(PwdDb) .filter( - PwdDb.src_id == src_id + PwdDb.source == src_id and PwdDb.uid == user.uid and PwdDb.account == body.account ) @@ -160,7 +160,7 @@ async def get_src_accounts( src: str, db: Session = Depends(get_db), user: User = Depends(get_current_user) ): records: list[PwdDb] = ( - db.query(PwdDb).filter(PwdDb.src_id == src and PwdDb.uid == user.uid).all() + db.query(PwdDb).filter(PwdDb.source == src and PwdDb.uid == user.uid).all() ) return StandardResponse(data=[record.account for record in records]) @@ -176,7 +176,7 @@ async def decrypt_src_pwd( record: PwdDb | None = ( db.query(PwdDb) .filter( - PwdDb.src_id == src and PwdDb.uid == user.uid and PwdDb.account == account + PwdDb.source == src and PwdDb.uid == user.uid and PwdDb.account == account ) .first() ) @@ -184,4 +184,4 @@ async def decrypt_src_pwd( if record is None: raise ExceptionResponse.not_found else: - return StandardResponse(data=decrypt_src_password(record.pwd, record.src_id)) + return StandardResponse(data=decrypt_src_password(record.pwd, record.source)) diff --git a/Services/Modulator/manager.py b/Services/Modulator/manager.py index 362a34d..59c05da 100644 --- a/Services/Modulator/manager.py +++ b/Services/Modulator/manager.py @@ -4,10 +4,9 @@ import os from http.cookies import BaseCookie from pathlib import Path -from typing import Set, cast +from typing import Set import toml -from fastapi import Response from Configs.config import config from Models.plugins import BasePlugin, Plugin @@ -47,6 +46,7 @@ def load_plugin(self, plugin_dir: Path) -> bool: ) as f: plugin_info = toml.load(f) + src_list: list[str] = [] for src in plugin_info["plugin"]["source"]: if src in self.registered_source: logger.error( @@ -55,8 +55,10 @@ def load_plugin(self, plugin_dir: Path) -> bool: return False else: logger.info(f"Registering source {src}") + src_list.append(src) + + module = importlib.import_module(f"Plugins.{plugin_dir.name}.main") - module = importlib.import_module(f"Plugins.{plugin_dir.name}.main") if issubclass(entry := getattr(module, plugin_dir.name), BasePlugin): instance = entry() if instance.on_load(): @@ -73,7 +75,7 @@ def load_plugin(self, plugin_dir: Path) -> bool: else: raise ImportError logger.info(f"Plugin {plugin_dir.name} Loaded") - self.registered_source.add(src) + self.registered_source.update(src_list) return True else: logger.error(f"Plugin {plugin_dir.name} is not a valid plugin") diff --git a/pyproject.toml b/pyproject.toml index 5da6f9f..a565c3c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,8 @@ pycryptodome = "^3.20.0" rich = "^13.7.0" slowapi = "^0.1.8" toml = "^0.10.2" +nest-asyncio = "^1.6.0" +pymysql = "^1.1.1" [build-system] requires = ["poetry-core"]