mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-09-20 03:17:55 +08:00
旧实现只覆盖 4 个平台,且把整个文件读进内存、无重试与完整性校验,
代码按平台复制粘贴了 4 份。本次用统一下载器替换:
- 新增 media_downloader/:流式写入、Range 续传、指数退避重试、大小校验、
路径穿越防护;B 站 DASH 音视频分轨下载后交由 ffmpeg 无损合流
- 新增 media_platform/<平台>/media.py:从平台原始响应提取媒体地址,
与下载器解耦;快手首次接入下载能力
- 开关:config.ENABLE_GET_MEDIA 与 --get_media,并打通 API/WebUI;
同时修正旧配置项 ENABLE_GET_MEIDAS 的拼写
- 落盘按帖子聚合:{SAVE_DATA_PATH 或 data}/{platform}/media/{内容ID}/
- B 站装好 ffmpeg 时走 DASH 最高画质,否则降级 mp4 直链(产物 video-durl.mp4,
避免低清文件阻塞后续的高清路径)
- 删除 4 个 *_store_media.py、AbstractStoreImage/Video 及各 client 的媒体 GET 方法
媒体下载失败只记录日志,不中断爬取主流程。
159 lines
6.3 KiB
Python
159 lines
6.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (c) 2025 relakkes@gmail.com
|
|
#
|
|
# This file is part of MediaCrawler project.
|
|
# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/store/xhs/__init__.py
|
|
# GitHub: https://github.com/NanmiCoder
|
|
# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1
|
|
#
|
|
|
|
# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则:
|
|
# 1. 不得用于任何商业用途。
|
|
# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。
|
|
# 3. 不得进行大规模爬取或对平台造成运营干扰。
|
|
# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。
|
|
# 5. 不得用于任何非法或不当的用途。
|
|
#
|
|
# 详细许可条款请参阅项目根目录下的LICENSE文件。
|
|
# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。
|
|
|
|
# -*- coding: utf-8 -*-
|
|
# @Author : relakkes@gmail.com
|
|
# @Time : 2024/1/14 17:34
|
|
# @Desc :
|
|
from typing import List
|
|
|
|
import config
|
|
from media_platform.xhs.media import extract_video_urls
|
|
from var import source_keyword_var
|
|
from tools.user_hash import anonymize_user_id, mask_nickname
|
|
|
|
from ._store_impl import *
|
|
|
|
|
|
class XhsStoreFactory:
|
|
STORES = {
|
|
"csv": XhsCsvStoreImplement,
|
|
"db": XhsDbStoreImplement,
|
|
"postgres": XhsDbStoreImplement,
|
|
"json": XhsJsonStoreImplement,
|
|
"jsonl": XhsJsonlStoreImplement,
|
|
"sqlite": XhsSqliteStoreImplement,
|
|
"mongodb": XhsMongoStoreImplement,
|
|
"excel": XhsExcelStoreImplement,
|
|
}
|
|
|
|
@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 or sqlite or mongodb or excel ...")
|
|
return store_class()
|
|
|
|
|
|
async def update_xhs_note(note_item: Dict):
|
|
"""
|
|
Update Xiaohongshu note
|
|
Args:
|
|
note_item:
|
|
|
|
Returns:
|
|
|
|
"""
|
|
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", [])
|
|
tag_list: List[Dict] = note_item.get("tag_list", [])
|
|
|
|
for img in image_list:
|
|
if img.get('url_default') != '':
|
|
img.update({'url': img.get('url_default')})
|
|
|
|
video_url = ','.join(extract_video_urls(note_item))
|
|
|
|
local_db_item = {
|
|
"note_id": note_item.get("note_id"), # Note ID
|
|
"type": note_item.get("type"), # Note type
|
|
"title": note_item.get("title") or note_item.get("desc", "")[:255], # Note title
|
|
"desc": note_item.get("desc", ""), # Note description
|
|
"video_url": video_url, # Note video url
|
|
"time": note_item.get("time"), # Note publish time
|
|
"last_update_time": note_item.get("last_update_time", 0), # Note last update time
|
|
"creator_hash": anonymize_user_id(user_info.get("user_id")), # 创作者匿名哈希(不存原始 user_id)
|
|
"nickname": mask_nickname(user_info.get("nickname")), # 用户昵称(已脱敏)
|
|
"liked_count": interact_info.get("liked_count"), # Like count
|
|
"collected_count": interact_info.get("collected_count"), # Collection count
|
|
"comment_count": interact_info.get("comment_count"), # Comment count
|
|
"share_count": interact_info.get("share_count"), # Share count
|
|
"image_list": ','.join([img.get('url', '') for img in image_list]), # Image URLs
|
|
"tag_list": ','.join([tag.get('name', '') for tag in tag_list if tag.get('type') == 'topic']), # Tags
|
|
"last_modify_ts": utils.get_current_timestamp(), # Last modification timestamp (Generated by MediaCrawler, mainly used to record the latest update time of a record in DB storage)
|
|
"note_url": f"https://www.xiaohongshu.com/explore/{note_id}?xsec_token={note_item.get('xsec_token')}&xsec_source=pc_search", # Note URL
|
|
"source_keyword": source_keyword_var.get(), # Search keyword
|
|
"xsec_token": note_item.get("xsec_token"), # xsec_token
|
|
}
|
|
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]):
|
|
"""
|
|
Batch update Xiaohongshu note comments
|
|
Args:
|
|
note_id:
|
|
comments:
|
|
|
|
Returns:
|
|
|
|
"""
|
|
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):
|
|
"""
|
|
Update Xiaohongshu note comment
|
|
Args:
|
|
note_id:
|
|
comment_item:
|
|
|
|
Returns:
|
|
|
|
"""
|
|
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", [])]
|
|
target_comment = comment_item.get("target_comment", {})
|
|
local_db_item = {
|
|
"comment_id": comment_id, # Comment ID
|
|
"create_time": comment_item.get("create_time"), # Comment time
|
|
"note_id": note_id, # Note ID
|
|
"content": comment_item.get("content"), # Comment content
|
|
"creator_hash": anonymize_user_id(user_info.get("user_id")), # 创作者匿名哈希(不存原始 user_id)
|
|
"nickname": mask_nickname(user_info.get("nickname")), # 用户昵称(已脱敏)
|
|
"sub_comment_count": comment_item.get("sub_comment_count", 0), # Sub-comment count
|
|
"pictures": ",".join(comment_pictures), # Comment pictures
|
|
"parent_comment_id": target_comment.get("id", ""), # Parent comment ID
|
|
"last_modify_ts": utils.get_current_timestamp(), # Last modification timestamp (Generated by MediaCrawler, mainly used to record the latest update time of a record in DB storage)
|
|
"like_count": comment_item.get("like_count", 0),
|
|
}
|
|
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):
|
|
"""
|
|
Save Xiaohongshu creator
|
|
Args:
|
|
user_id:
|
|
creator:
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# 教学版:创作者个人资料(昵称/性别/头像/IP/粉丝数等)不再落库,防骚扰。
|
|
return
|