2024-01-14 14:06:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# @Author : relakkes@gmail.com
|
|
|
|
# @Time : 2024/1/14 17:34
|
|
|
|
# @Desc :
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import config
|
2024-08-23 00:29:24 +00:00
|
|
|
from var import source_keyword_var
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
from . import xhs_store_impl
|
2024-07-11 14:49:05 +00:00
|
|
|
from .xhs_store_image import *
|
2024-07-13 19:20:05 +00:00
|
|
|
from .xhs_store_impl import *
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class XhsStoreFactory:
|
|
|
|
STORES = {
|
|
|
|
"csv": XhsCsvStoreImplement,
|
|
|
|
"db": XhsDbStoreImplement,
|
|
|
|
"json": XhsJsonStoreImplement
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_store() -> AbstractStore:
|
|
|
|
store_class = XhsStoreFactory.STORES.get(config.SAVE_DATA_OPTION)
|
|
|
|
if not store_class:
|
|
|
|
raise ValueError("[XhsStoreFactory.create_store] Invalid save option only supported csv or db or json ...")
|
|
|
|
return store_class()
|
|
|
|
|
|
|
|
|
2024-07-11 14:49:05 +00:00
|
|
|
def get_video_url_arr(note_item: Dict) -> List:
|
2024-08-23 22:07:33 +00:00
|
|
|
"""
|
|
|
|
获取视频url数组
|
|
|
|
Args:
|
|
|
|
note_item:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-07-11 14:49:05 +00:00
|
|
|
if note_item.get('type') != 'video':
|
|
|
|
return []
|
|
|
|
|
|
|
|
videoArr = []
|
|
|
|
originVideoKey = note_item.get('video').get('consumer').get('origin_video_key')
|
|
|
|
if originVideoKey == '':
|
|
|
|
originVideoKey = note_item.get('video').get('consumer').get('originVideoKey')
|
|
|
|
# 降级有水印
|
|
|
|
if originVideoKey == '':
|
|
|
|
videos = note_item.get('video').get('media').get('stream').get('h264')
|
|
|
|
if type(videos).__name__ == 'list':
|
|
|
|
videoArr = [v.get('master_url') for v in videos]
|
|
|
|
else:
|
|
|
|
videoArr = [f"http://sns-video-bd.xhscdn.com/{originVideoKey}"]
|
|
|
|
|
|
|
|
return videoArr
|
|
|
|
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
async def update_xhs_note(note_item: Dict):
|
2024-08-23 22:07:33 +00:00
|
|
|
"""
|
|
|
|
更新小红书笔记
|
|
|
|
Args:
|
|
|
|
note_item:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-01-14 14:06:31 +00:00
|
|
|
note_id = note_item.get("note_id")
|
|
|
|
user_info = note_item.get("user", {})
|
|
|
|
interact_info = note_item.get("interact_info", {})
|
|
|
|
image_list: List[Dict] = note_item.get("image_list", [])
|
2024-03-01 17:49:42 +00:00
|
|
|
tag_list: List[Dict] = note_item.get("tag_list", [])
|
|
|
|
|
2024-07-11 09:39:48 +00:00
|
|
|
for img in image_list:
|
|
|
|
if img.get('url_default') != '':
|
|
|
|
img.update({'url': img.get('url_default')})
|
|
|
|
|
2024-07-11 14:49:05 +00:00
|
|
|
video_url = ','.join(get_video_url_arr(note_item))
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
local_db_item = {
|
|
|
|
"note_id": note_item.get("note_id"),
|
|
|
|
"type": note_item.get("type"),
|
|
|
|
"title": note_item.get("title") or note_item.get("desc", "")[:255],
|
|
|
|
"desc": note_item.get("desc", ""),
|
2024-03-01 17:49:42 +00:00
|
|
|
"video_url": video_url,
|
2024-01-14 14:06:31 +00:00
|
|
|
"time": note_item.get("time"),
|
|
|
|
"last_update_time": note_item.get("last_update_time", 0),
|
|
|
|
"user_id": user_info.get("user_id"),
|
|
|
|
"nickname": user_info.get("nickname"),
|
|
|
|
"avatar": user_info.get("avatar"),
|
|
|
|
"liked_count": interact_info.get("liked_count"),
|
|
|
|
"collected_count": interact_info.get("collected_count"),
|
|
|
|
"comment_count": interact_info.get("comment_count"),
|
|
|
|
"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]),
|
2024-03-07 14:30:44 +00:00
|
|
|
"tag_list": ','.join([tag.get('name', '') for tag in tag_list if tag.get('type') == 'topic']),
|
2024-01-14 14:06:31 +00:00
|
|
|
"last_modify_ts": utils.get_current_timestamp(),
|
2024-08-04 06:54:03 +00:00
|
|
|
"note_url": f"https://www.xiaohongshu.com/explore/{note_id}?xsec_token={note_item.get('xsec_token')}&xsec_source=pc_search",
|
2024-08-23 00:29:24 +00:00
|
|
|
"source_keyword": source_keyword_var.get(),
|
2024-01-14 14:06:31 +00:00
|
|
|
}
|
|
|
|
utils.logger.info(f"[store.xhs.update_xhs_note] xhs note: {local_db_item}")
|
|
|
|
await XhsStoreFactory.create_store().store_content(local_db_item)
|
|
|
|
|
2024-03-07 14:30:44 +00:00
|
|
|
|
2024-01-15 16:40:07 +00:00
|
|
|
async def batch_update_xhs_note_comments(note_id: str, comments: List[Dict]):
|
2024-08-23 22:07:33 +00:00
|
|
|
"""
|
|
|
|
批量更新小红书笔记评论
|
|
|
|
Args:
|
|
|
|
note_id:
|
|
|
|
comments:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-01-15 16:40:07 +00:00
|
|
|
if not comments:
|
|
|
|
return
|
|
|
|
for comment_item in comments:
|
|
|
|
await update_xhs_note_comment(note_id, comment_item)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
2024-03-07 14:30:44 +00:00
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
async def update_xhs_note_comment(note_id: str, comment_item: Dict):
|
2024-08-23 22:07:33 +00:00
|
|
|
"""
|
|
|
|
更新小红书笔记评论
|
|
|
|
Args:
|
|
|
|
note_id:
|
|
|
|
comment_item:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-01-14 14:06:31 +00:00
|
|
|
user_info = comment_item.get("user_info", {})
|
|
|
|
comment_id = comment_item.get("id")
|
2024-03-07 14:30:44 +00:00
|
|
|
comment_pictures = [item.get("url_default", "") for item in comment_item.get("pictures", [])]
|
2024-04-12 09:32:20 +00:00
|
|
|
target_comment = comment_item.get("target_comment", {})
|
2024-01-14 14:06:31 +00:00
|
|
|
local_db_item = {
|
|
|
|
"comment_id": comment_id,
|
|
|
|
"create_time": comment_item.get("create_time"),
|
|
|
|
"ip_location": comment_item.get("ip_location"),
|
|
|
|
"note_id": note_id,
|
|
|
|
"content": comment_item.get("content"),
|
|
|
|
"user_id": user_info.get("user_id"),
|
|
|
|
"nickname": user_info.get("nickname"),
|
|
|
|
"avatar": user_info.get("image"),
|
2024-04-12 09:32:20 +00:00
|
|
|
"sub_comment_count": comment_item.get("sub_comment_count", 0),
|
2024-03-07 14:30:44 +00:00
|
|
|
"pictures": ",".join(comment_pictures),
|
2024-04-12 09:32:20 +00:00
|
|
|
"parent_comment_id": target_comment.get("id", 0),
|
2024-01-14 14:06:31 +00:00
|
|
|
"last_modify_ts": utils.get_current_timestamp(),
|
2024-08-23 22:07:33 +00:00
|
|
|
"like_count": comment_item.get("like_count", 0),
|
2024-01-14 14:06:31 +00:00
|
|
|
}
|
|
|
|
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)
|
2024-03-01 17:49:42 +00:00
|
|
|
|
2024-03-07 14:30:44 +00:00
|
|
|
|
2024-03-01 17:49:42 +00:00
|
|
|
async def save_creator(user_id: str, creator: Dict):
|
2024-08-23 22:07:33 +00:00
|
|
|
"""
|
|
|
|
保存小红书创作者
|
|
|
|
Args:
|
|
|
|
user_id:
|
|
|
|
creator:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-03-01 17:49:42 +00:00
|
|
|
user_info = creator.get('basicInfo', {})
|
|
|
|
|
|
|
|
follows = 0
|
|
|
|
fans = 0
|
|
|
|
interaction = 0
|
|
|
|
for i in creator.get('interactions'):
|
|
|
|
if i.get('type') == 'follows':
|
|
|
|
follows = i.get('count')
|
|
|
|
elif i.get('type') == 'fans':
|
|
|
|
fans = i.get('count')
|
|
|
|
elif i.get('type') == 'interaction':
|
|
|
|
interaction = i.get('count')
|
|
|
|
|
|
|
|
local_db_item = {
|
|
|
|
'user_id': user_id,
|
|
|
|
'nickname': user_info.get('nickname'),
|
2024-03-07 14:30:44 +00:00
|
|
|
'gender': '女' if user_info.get('gender') == 1 else '男',
|
2024-03-01 17:49:42 +00:00
|
|
|
'avatar': user_info.get('images'),
|
|
|
|
'desc': user_info.get('desc'),
|
2024-06-26 03:56:41 +00:00
|
|
|
'ip_location': user_info.get('ipLocation'),
|
2024-03-01 17:49:42 +00:00
|
|
|
'follows': follows,
|
|
|
|
'fans': fans,
|
|
|
|
'interaction': interaction,
|
2024-04-06 15:51:03 +00:00
|
|
|
'tag_list': json.dumps({tag.get('tagType'): tag.get('name') for tag in creator.get('tags')},
|
|
|
|
ensure_ascii=False),
|
2024-04-06 14:11:10 +00:00
|
|
|
"last_modify_ts": utils.get_current_timestamp(),
|
2024-03-01 17:49:42 +00:00
|
|
|
}
|
|
|
|
utils.logger.info(f"[store.xhs.save_creator] creator:{local_db_item}")
|
2024-03-07 14:30:44 +00:00
|
|
|
await XhsStoreFactory.create_store().store_creator(local_db_item)
|
2024-07-11 14:49:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def update_xhs_note_image(note_id, pic_content, extension_file_name):
|
2024-08-23 22:07:33 +00:00
|
|
|
"""
|
|
|
|
更新小红书笔
|
|
|
|
Args:
|
|
|
|
note_id:
|
|
|
|
pic_content:
|
|
|
|
extension_file_name:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2024-07-11 14:49:05 +00:00
|
|
|
await XiaoHongShuImage().store_image(
|
|
|
|
{"notice_id": note_id, "pic_content": pic_content, "extension_file_name": extension_file_name})
|