From 41fee4ff4f9fed4c604d8e92eecc43182dcad715 Mon Sep 17 00:00:00 2001 From: Relakkes Date: Thu, 7 Mar 2024 22:30:44 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=B0=8F=E7=BA=A2=E4=B9=A6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=8E=B7=E5=8F=96=E8=AF=84=E8=AE=BA=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=9B=BE=E7=89=87=E9=93=BE=E6=8E=A5=20#145?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- media_platform/xhs/client.py | 4 +++- store/xhs/__init__.py | 15 ++++++++++----- store/xhs/xhs_store_db_types.py | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/media_platform/xhs/client.py b/media_platform/xhs/client.py index 219c35a..72e9d89 100644 --- a/media_platform/xhs/client.py +++ b/media_platform/xhs/client.py @@ -267,7 +267,9 @@ class XHSClient: uri = "/api/sns/web/v2/comment/page" params = { "note_id": note_id, - "cursor": cursor + "cursor": cursor, + "top_comment_id":"", + "image_formats": "jpg,webp,avif" } return await self.get(uri, params) diff --git a/store/xhs/__init__.py b/store/xhs/__init__.py index 8d4e002..748bb90 100644 --- a/store/xhs/__init__.py +++ b/store/xhs/__init__.py @@ -37,7 +37,7 @@ async def update_xhs_note(note_item: Dict): if note_item.get('type') == 'video': videos = note_item.get('video').get('media').get('stream').get('h264') if type(videos).__name__ == 'list': - video_url = ','.join([ v.get('master_url') for v in videos]) + video_url = ','.join([v.get('master_url') for v in videos]) local_db_item = { "note_id": note_item.get("note_id"), @@ -56,22 +56,25 @@ async def update_xhs_note(note_item: Dict): "share_count": interact_info.get("share_count"), "ip_location": note_item.get("ip_location", ""), "image_list": ','.join([img.get('url', '') for img in image_list]), - "tag_list": ','.join([tag.get('name', '') for tag in tag_list if tag.get('type')=='topic']), + "tag_list": ','.join([tag.get('name', '') for tag in tag_list if tag.get('type') == 'topic']), "last_modify_ts": utils.get_current_timestamp(), "note_url": f"https://www.xiaohongshu.com/explore/{note_id}" } utils.logger.info(f"[store.xhs.update_xhs_note] xhs note: {local_db_item}") await XhsStoreFactory.create_store().store_content(local_db_item) + async def batch_update_xhs_note_comments(note_id: str, comments: List[Dict]): if not comments: return for comment_item in comments: await update_xhs_note_comment(note_id, comment_item) + async def update_xhs_note_comment(note_id: str, comment_item: Dict): user_info = comment_item.get("user_info", {}) comment_id = comment_item.get("id") + comment_pictures = [item.get("url_default", "") for item in comment_item.get("pictures", [])] local_db_item = { "comment_id": comment_id, "create_time": comment_item.get("create_time"), @@ -82,11 +85,13 @@ async def update_xhs_note_comment(note_id: str, comment_item: Dict): "nickname": user_info.get("nickname"), "avatar": user_info.get("image"), "sub_comment_count": comment_item.get("sub_comment_count"), + "pictures": ",".join(comment_pictures), "last_modify_ts": utils.get_current_timestamp(), } utils.logger.info(f"[store.xhs.update_xhs_note_comment] xhs note comment:{local_db_item}") await XhsStoreFactory.create_store().store_comment(local_db_item) + async def save_creator(user_id: str, creator: Dict): user_info = creator.get('basicInfo', {}) @@ -104,14 +109,14 @@ async def save_creator(user_id: str, creator: Dict): local_db_item = { 'user_id': user_id, 'nickname': user_info.get('nickname'), - 'gender': '女' if user_info.get('gender') == 1 else '男' , + 'gender': '女' if user_info.get('gender') == 1 else '男', 'avatar': user_info.get('images'), 'desc': user_info.get('desc'), 'ip_location': user_info.get('ip_location'), 'follows': follows, 'fans': fans, 'interaction': interaction, - 'tag_list': json.dumps({tag.get('tagType'):tag.get('name') for tag in creator.get('tags')}), + 'tag_list': json.dumps({tag.get('tagType'): tag.get('name') for tag in creator.get('tags')}), } utils.logger.info(f"[store.xhs.save_creator] creator:{local_db_item}") - await XhsStoreFactory.create_store().store_creator(local_db_item) \ No newline at end of file + await XhsStoreFactory.create_store().store_creator(local_db_item) diff --git a/store/xhs/xhs_store_db_types.py b/store/xhs/xhs_store_db_types.py index 69ec1e2..ee8f240 100644 --- a/store/xhs/xhs_store_db_types.py +++ b/store/xhs/xhs_store_db_types.py @@ -50,6 +50,7 @@ class XHSNoteComment(XhsBaseModel): note_id = fields.CharField(max_length=64, description="笔记ID") content = fields.TextField(description="评论内容") sub_comment_count = fields.IntField(description="子评论数量") + pictures = fields.CharField(null=True, max_length=512, description="评论的图片集合") class Meta: table = "xhs_note_comment"