From 30d0e733d5bdc2b2e62bcef3849eef9ab498b8f6 Mon Sep 17 00:00:00 2001 From: Relakkes Date: Wed, 15 Jan 2025 14:49:55 +0800 Subject: [PATCH] feat: douyin adds comment images --- schema/tables.sql | 3 +- store/douyin/__init__.py | 112 +++++++++++++++++++++++++-------------- 2 files changed, 75 insertions(+), 40 deletions(-) diff --git a/schema/tables.sql b/schema/tables.sql index c7d8312..3d0e7c6 100644 --- a/schema/tables.sql +++ b/schema/tables.sql @@ -536,4 +536,5 @@ CREATE TABLE `zhihu_creator` ( -- add column `like_count` to douyin_aweme_comment alter table douyin_aweme_comment add column `like_count` varchar(255) NOT NULL DEFAULT '0' COMMENT '点赞数'; -alter table xhs_note add column xsec_token varchar(50) default null comment '签名算法'; \ No newline at end of file +alter table xhs_note add column xsec_token varchar(50) default null comment '签名算法'; +alter table douyin_aweme_comment add column `pictures` varchar(500) NOT NULL DEFAULT '' COMMENT '评论图片列表'; \ No newline at end of file diff --git a/store/douyin/__init__.py b/store/douyin/__init__.py index 8e83981..91ad8c0 100644 --- a/store/douyin/__init__.py +++ b/store/douyin/__init__.py @@ -1,12 +1,12 @@ -# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则: -# 1. 不得用于任何商业用途。 -# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。 -# 3. 不得进行大规模爬取或对平台造成运营干扰。 -# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。 +# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则: +# 1. 不得用于任何商业用途。 +# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。 +# 3. 不得进行大规模爬取或对平台造成运营干扰。 +# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。 # 5. 不得用于任何非法或不当的用途。 -# -# 详细许可条款请参阅项目根目录下的LICENSE文件。 -# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。 +# +# 详细许可条款请参阅项目根目录下的LICENSE文件。 +# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。 # -*- coding: utf-8 -*- @@ -25,7 +25,7 @@ class DouyinStoreFactory: STORES = { "csv": DouyinCsvStoreImplement, "db": DouyinDbStoreImplement, - "json": DouyinJsonStoreImplement + "json": DouyinJsonStoreImplement, } @staticmethod @@ -33,10 +33,35 @@ class DouyinStoreFactory: store_class = DouyinStoreFactory.STORES.get(config.SAVE_DATA_OPTION) if not store_class: raise ValueError( - "[DouyinStoreFactory.create_store] Invalid save option only supported csv or db or json ...") + "[DouyinStoreFactory.create_store] Invalid save option only supported csv or db or json ..." + ) return store_class() +def _extract_comment_image_list(comment_item: Dict) -> List[str]: + """ + 提取评论图片列表 + + Args: + comment_item (Dict): 抖音评论 + + Returns: + List[str]: 评论图片列表 + """ + images_res: List[str] = [] + image_list: List[Dict] = comment_item.get("image_list", []) + + if not image_list: + return [] + + for image in image_list: + image_url_list = image.get("origin_url", {}).get("url_list", []) + if image_url_list and len(image_url_list) > 1: + images_res.append(image_url_list[1]) + + return images_res + + async def update_douyin_aweme(aweme_item: Dict): aweme_id = aweme_item.get("aweme_id") user_info = aweme_item.get("author", {}) @@ -64,8 +89,11 @@ async def update_douyin_aweme(aweme_item: Dict): "source_keyword": source_keyword_var.get(), } utils.logger.info( - f"[store.douyin.update_douyin_aweme] douyin aweme id:{aweme_id}, title:{save_content_item.get('title')}") - await DouyinStoreFactory.create_store().store_content(content_item=save_content_item) + f"[store.douyin.update_douyin_aweme] douyin aweme id:{aweme_id}, title:{save_content_item.get('title')}" + ) + await DouyinStoreFactory.create_store().store_content( + content_item=save_content_item + ) async def batch_update_dy_aweme_comments(aweme_id: str, comments: List[Dict]): @@ -79,13 +107,19 @@ async def update_dy_aweme_comment(aweme_id: str, comment_item: Dict): comment_aweme_id = comment_item.get("aweme_id") if aweme_id != comment_aweme_id: utils.logger.error( - f"[store.douyin.update_dy_aweme_comment] comment_aweme_id: {comment_aweme_id} != aweme_id: {aweme_id}") + f"[store.douyin.update_dy_aweme_comment] comment_aweme_id: {comment_aweme_id} != aweme_id: {aweme_id}" + ) return user_info = comment_item.get("user", {}) comment_id = comment_item.get("cid") parent_comment_id = comment_item.get("reply_id", "0") - avatar_info = user_info.get("avatar_medium", {}) or user_info.get("avatar_300x300", {}) or user_info.get( - "avatar_168x168", {}) or user_info.get("avatar_thumb", {}) or {} + avatar_info = ( + user_info.get("avatar_medium", {}) + or user_info.get("avatar_300x300", {}) + or user_info.get("avatar_168x168", {}) + or user_info.get("avatar_thumb", {}) + or {} + ) save_comment_item = { "comment_id": comment_id, "create_time": comment_item.get("create_time"), @@ -100,39 +134,39 @@ async def update_dy_aweme_comment(aweme_id: str, comment_item: Dict): "nickname": user_info.get("nickname"), "avatar": avatar_info.get("url_list", [""])[0], "sub_comment_count": str(comment_item.get("reply_comment_total", 0)), - "like_count": comment_item.get("digg_count") if comment_item.get("digg_count") else 0, + "like_count": ( + comment_item.get("digg_count") if comment_item.get("digg_count") else 0 + ), "last_modify_ts": utils.get_current_timestamp(), - "parent_comment_id": parent_comment_id + "parent_comment_id": parent_comment_id, + "pictures": ",".join(_extract_comment_image_list(comment_item)), } utils.logger.info( - f"[store.douyin.update_dy_aweme_comment] douyin aweme comment: {comment_id}, content: {save_comment_item.get('content')}") - - await DouyinStoreFactory.create_store().store_comment(comment_item=save_comment_item) - + f"[store.douyin.update_dy_aweme_comment] douyin aweme comment: {comment_id}, content: {save_comment_item.get('content')}" + ) + await DouyinStoreFactory.create_store().store_comment( + comment_item=save_comment_item + ) async def save_creator(user_id: str, creator: Dict): - user_info = creator.get('user', {}) - gender_map = { - 0: '未知', - 1: '男', - 2: '女' - } - avatar_uri = user_info.get('avatar_300x300', {}).get('uri') + user_info = creator.get("user", {}) + gender_map = {0: "未知", 1: "男", 2: "女"} + avatar_uri = user_info.get("avatar_300x300", {}).get("uri") local_db_item = { - 'user_id': user_id, - 'nickname': user_info.get('nickname'), - 'gender': gender_map.get(user_info.get('gender'), '未知'), - 'avatar': f"https://p3-pc.douyinpic.com/img/{avatar_uri}" + r"~c5_300x300.jpeg?from=2956013662", - 'desc': user_info.get('signature'), - 'ip_location': user_info.get('ip_location'), - 'follows': user_info.get("following_count", 0), - 'fans': user_info.get("max_follower_count", 0), - 'interaction': user_info.get("total_favorited", 0), - 'videos_count': user_info.get("aweme_count", 0), + "user_id": user_id, + "nickname": user_info.get("nickname"), + "gender": gender_map.get(user_info.get("gender"), "未知"), + "avatar": f"https://p3-pc.douyinpic.com/img/{avatar_uri}" + + r"~c5_300x300.jpeg?from=2956013662", + "desc": user_info.get("signature"), + "ip_location": user_info.get("ip_location"), + "follows": user_info.get("following_count", 0), + "fans": user_info.get("max_follower_count", 0), + "interaction": user_info.get("total_favorited", 0), + "videos_count": user_info.get("aweme_count", 0), "last_modify_ts": utils.get_current_timestamp(), - } utils.logger.info(f"[store.douyin.save_creator] creator:{local_db_item}") await DouyinStoreFactory.create_store().store_creator(local_db_item)