mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-10-03 15:18:11 +08:00
refactor: 教学版移除全平台用户个人信息采集与持久化
- 用户 ID 转为匿名 creator_hash,昵称中间脱敏,IP/头像/主页/签名/性别不再采集 - 覆盖 xhs/weibo/bilibili/douyin/kuaishou/tieba/zhihu 7 个平台 - 删除 7 张 creator 档案 ORM 表,15 张内容/评论表新增 creator_hash 列 - B 站禁用粉丝/关注/联系人列表抓取 - 新增 tools/user_hash.py 与 4 个平台的 mock+SQLite 端到端测试 测试: pytest tests/test_no_user_info.py tests/test_weibo_no_user_info.py tests/test_douyin_no_user_info.py tests/test_kuaishou_no_user_info.py (21 passed)
This commit is contained in:
+22
-31
@@ -35,7 +35,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
import config
|
||||
from base.base_crawler import AbstractStore
|
||||
from database.models import WeiboCreator, WeiboNote, WeiboNoteComment
|
||||
from database.models import WeiboNote, WeiboNoteComment
|
||||
from tools import utils, words
|
||||
from tools.async_file_writer import AsyncFileWriter
|
||||
from database.db_session import get_session
|
||||
@@ -58,6 +58,13 @@ def calculate_number_of_files(file_store_path: str) -> int:
|
||||
return 1
|
||||
|
||||
|
||||
def _filter_model_fields(model_cls, item: Dict) -> Dict:
|
||||
"""只保留目标 ORM 模型已有的列,避免把已删除/多余字段(如 avatar/gender/
|
||||
profile_url/ip_location/user_id)传给 ORM 构造而报错。教学版兜底保护。"""
|
||||
allowed = {col.name for col in model_cls.__table__.columns}
|
||||
return {k: v for k, v in item.items() if k in allowed}
|
||||
|
||||
|
||||
class WeiboCsvStoreImplement(AbstractStore):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@@ -88,13 +95,14 @@ class WeiboCsvStoreImplement(AbstractStore):
|
||||
async def store_creator(self, creator: Dict):
|
||||
"""
|
||||
Weibo creator CSV storage implementation
|
||||
教学版:不采集/持久化创作者个人信息,空操作。
|
||||
Args:
|
||||
creator:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
await self.writer.write_to_csv(item_type="creators", item=creator)
|
||||
pass
|
||||
|
||||
|
||||
class WeiboDbStoreImplement(AbstractStore):
|
||||
@@ -108,6 +116,8 @@ class WeiboDbStoreImplement(AbstractStore):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# 教学版兜底:过滤掉已删除/多余字段,确保不会把 user_id/avatar 等传给 ORM
|
||||
content_item = _filter_model_fields(WeiboNote, content_item)
|
||||
note_id = int(content_item.get("note_id"))
|
||||
content_item["note_id"] = note_id
|
||||
async with get_session() as session:
|
||||
@@ -135,6 +145,8 @@ class WeiboDbStoreImplement(AbstractStore):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# 教学版兜底:过滤掉已删除/多余字段,确保不会把 user_id/avatar 等传给 ORM
|
||||
comment_item = _filter_model_fields(WeiboNoteComment, comment_item)
|
||||
comment_id = int(comment_item.get("comment_id"))
|
||||
comment_item["comment_id"] = comment_id
|
||||
comment_item["note_id"] = int(comment_item.get("note_id", 0) or 0)
|
||||
@@ -162,29 +174,14 @@ class WeiboDbStoreImplement(AbstractStore):
|
||||
async def store_creator(self, creator: Dict):
|
||||
"""
|
||||
Weibo creator DB storage implementation
|
||||
教学版:不采集/持久化创作者个人信息,空操作(WeiboCreator 表已删除)。
|
||||
Args:
|
||||
creator:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user_id = int(creator.get("user_id"))
|
||||
creator["user_id"] = user_id
|
||||
async with get_session() as session:
|
||||
stmt = select(WeiboCreator).where(WeiboCreator.user_id == user_id)
|
||||
res = await session.execute(stmt)
|
||||
db_creator = res.scalar_one_or_none()
|
||||
if db_creator:
|
||||
db_creator.last_modify_ts = utils.get_current_timestamp()
|
||||
for key, value in creator.items():
|
||||
if hasattr(db_creator, key):
|
||||
setattr(db_creator, key, value)
|
||||
else:
|
||||
creator["add_ts"] = utils.get_current_timestamp()
|
||||
creator["last_modify_ts"] = utils.get_current_timestamp()
|
||||
db_creator = WeiboCreator(**creator)
|
||||
session.add(db_creator)
|
||||
await session.commit()
|
||||
pass
|
||||
|
||||
|
||||
class WeiboJsonStoreImplement(AbstractStore):
|
||||
@@ -217,13 +214,14 @@ class WeiboJsonStoreImplement(AbstractStore):
|
||||
async def store_creator(self, creator: Dict):
|
||||
"""
|
||||
creator JSON storage implementation
|
||||
教学版:不采集/持久化创作者个人信息,空操作。
|
||||
Args:
|
||||
creator:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
await self.writer.write_single_item_to_json(item_type="creators", item=creator)
|
||||
pass
|
||||
|
||||
|
||||
class WeiboJsonlStoreImplement(AbstractStore):
|
||||
@@ -238,7 +236,8 @@ class WeiboJsonlStoreImplement(AbstractStore):
|
||||
await self.writer.write_to_jsonl(item_type="comments", item=comment_item)
|
||||
|
||||
async def store_creator(self, creator: Dict):
|
||||
await self.writer.write_to_jsonl(item_type="creators", item=creator)
|
||||
# 教学版:不采集/持久化创作者个人信息,空操作。
|
||||
pass
|
||||
|
||||
|
||||
class WeiboSqliteStoreImplement(WeiboDbStoreImplement):
|
||||
@@ -291,19 +290,11 @@ class WeiboMongoStoreImplement(AbstractStore):
|
||||
async def store_creator(self, creator_item: Dict):
|
||||
"""
|
||||
Store creator information to MongoDB
|
||||
教学版:不采集/持久化创作者个人信息,空操作。
|
||||
Args:
|
||||
creator_item: Creator data
|
||||
"""
|
||||
user_id = creator_item.get("user_id")
|
||||
if not user_id:
|
||||
return
|
||||
|
||||
await self.mongo_store.save_or_update(
|
||||
collection_suffix="creators",
|
||||
query={"user_id": user_id},
|
||||
data=creator_item
|
||||
)
|
||||
utils.logger.info(f"[WeiboMongoStoreImplement.store_creator] Saved creator {user_id} to MongoDB")
|
||||
pass
|
||||
|
||||
|
||||
class WeiboExcelStoreImplement:
|
||||
|
||||
Reference in New Issue
Block a user