From 1556e1b8cdab6cf5f41ab6a92f2f6a9510dc316d Mon Sep 17 00:00:00 2001 From: zhoubang Date: Sun, 13 Aug 2023 22:39:50 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20orm=E6=B7=BB=E5=8A=A0Pydant?= =?UTF-8?q?ic=E6=95=B0=E6=8D=AE=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/douyin.py | 21 +++++++++++++++++---- models/xiaohongshu.py | 25 +++++++++++++++++++++---- requirements.txt | 3 ++- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/models/douyin.py b/models/douyin.py index 9d797cc..d2a8029 100644 --- a/models/douyin.py +++ b/models/douyin.py @@ -3,6 +3,7 @@ from typing import Dict, List from tortoise import fields from tortoise.models import Model +from tortoise.contrib.pydantic import pydantic_model_creator import config from tools import utils @@ -87,9 +88,15 @@ async def update_douyin_aweme(aweme_item: Dict): if config.IS_SAVED_DATABASED: if not await DouyinAweme.filter(aweme_id=aweme_id).exists(): local_db_item["add_ts"] = utils.get_current_timestamp() - await DouyinAweme.create(**local_db_item) + douyin_aweme_pydantic = pydantic_model_creator(DouyinAweme,name='DouyinAwemeCreate',exclude=('id',)) + douyin_data = douyin_aweme_pydantic(**local_db_item) + douyin_aweme_pydantic.validate(douyin_data) + await DouyinAweme.create(**douyin_data.dict()) else: - await DouyinAweme.filter(aweme_id=aweme_id).update(**local_db_item) + douyin_aweme_pydantic = pydantic_model_creator(DouyinAweme, name='DouyinAwemeUpdate', exclude=('id','add_ts')) + douyin_data = douyin_aweme_pydantic(**local_db_item) + douyin_aweme_pydantic.validate(douyin_data) + await DouyinAweme.filter(aweme_id=aweme_id).update(**douyin_data.dict()) async def batch_update_dy_aweme_comments(aweme_id: str, comments: List[Dict]): @@ -129,6 +136,12 @@ async def update_dy_aweme_comment(aweme_id: str, comment_item: Dict): if config.IS_SAVED_DATABASED: if not await DouyinAwemeComment.filter(comment_id=comment_id).exists(): local_db_item["add_ts"] = utils.get_current_timestamp() - await DouyinAwemeComment.create(**local_db_item) + comment_pydantic = pydantic_model_creator(DouyinAwemeComment, name='DouyinAwemeCommentCreate', exclude=('id',)) + comment_data = comment_pydantic(**local_db_item) + comment_pydantic.validate(comment_data) + await DouyinAwemeComment.create(**comment_data.dict()) else: - await DouyinAwemeComment.filter(comment_id=comment_id).update(**local_db_item) + comment_pydantic = pydantic_model_creator(DouyinAwemeComment, name='DouyinAwemeCommentUpdate', exclude=('id','add_ts')) + comment_data = comment_pydantic(**local_db_item) + comment_pydantic.validate(comment_data) + await DouyinAwemeComment.filter(comment_id=comment_id).update(**comment_data.dict()) diff --git a/models/xiaohongshu.py b/models/xiaohongshu.py index 2b36ece..cd2dfa3 100644 --- a/models/xiaohongshu.py +++ b/models/xiaohongshu.py @@ -1,6 +1,7 @@ from typing import Dict, List from tortoise import fields +from tortoise.contrib.pydantic import pydantic_model_creator from tortoise.models import Model import config @@ -84,9 +85,15 @@ async def update_xhs_note(note_item: Dict): if config.IS_SAVED_DATABASED: if not await XHSNote.filter(note_id=note_id).first(): local_db_item["add_ts"] = utils.get_current_timestamp() - await XHSNote.create(**local_db_item) + note_pydantic = pydantic_model_creator(XHSNote, name="XHSPydanticCreate", exclude=('id', )) + note_data = note_pydantic(**local_db_item) + note_pydantic.validate(note_data) + await XHSNote.create(**note_data.dict()) else: - await XHSNote.filter(note_id=note_id).update(**local_db_item) + note_pydantic = pydantic_model_creator(XHSNote, name="XHSPydanticUpdate", exclude=('id','add_ts')) + note_data = note_pydantic(**local_db_item) + note_pydantic.validate(note_data) + await XHSNote.filter(note_id=note_id).update(**note_data.dict()) async def update_xhs_note_comment(note_id: str, comment_item: Dict): @@ -108,6 +115,16 @@ async def update_xhs_note_comment(note_id: str, comment_item: Dict): if config.IS_SAVED_DATABASED: if not await XHSNoteComment.filter(comment_id=comment_id).first(): local_db_item["add_ts"] = utils.get_current_timestamp() - await XHSNoteComment.create(**local_db_item) + comment_pydantic = pydantic_model_creator(XHSNoteComment, name="CommentPydanticCreate", exclude=('id', )) + comment_data = comment_pydantic(**local_db_item) + comment_pydantic.validate(comment_data) + await XHSNoteComment.create(**comment_data.dict()) else: - await XHSNoteComment.filter(comment_id=comment_id).update(**local_db_item) + comment_pydantic = pydantic_model_creator(XHSNoteComment, name="CommentPydanticUpdate", exclude=('id','add_ts',)) + comment_data = comment_pydantic(**local_db_item) + comment_pydantic.validate(comment_data) + await XHSNoteComment.filter(comment_id=comment_id).update(**comment_data.dict()) + + + + diff --git a/requirements.txt b/requirements.txt index 12c9979..a6df28d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ opencv-python==4.7.0.72 tortoise-orm[asyncmy]==0.19.3 aerich==0.7.2 numpy~=1.24.4 -redis~=4.6.0 \ No newline at end of file +redis~=4.6.0 +Pydantic==1.7 \ No newline at end of file