2024-01-14 14:06:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# @Author : relakkes@gmail.com
|
|
|
|
# @Time : 2024/1/14 21:34
|
|
|
|
# @Desc :
|
|
|
|
|
2024-05-28 07:11:52 +00:00
|
|
|
import re
|
2024-06-02 03:16:18 +00:00
|
|
|
from typing import List
|
2024-01-14 14:06:31 +00:00
|
|
|
|
2024-08-23 00:29:24 +00:00
|
|
|
from var import source_keyword_var
|
2024-08-23 21:52:11 +00:00
|
|
|
|
2024-04-09 09:21:52 +00:00
|
|
|
from .weibo_store_image import *
|
2024-04-10 01:54:28 +00:00
|
|
|
from .weibo_store_impl import *
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WeibostoreFactory:
|
|
|
|
STORES = {
|
|
|
|
"csv": WeiboCsvStoreImplement,
|
|
|
|
"db": WeiboDbStoreImplement,
|
2024-04-09 09:21:52 +00:00
|
|
|
"json": WeiboJsonStoreImplement,
|
2024-01-14 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_store() -> AbstractStore:
|
|
|
|
store_class = WeibostoreFactory.STORES.get(config.SAVE_DATA_OPTION)
|
|
|
|
if not store_class:
|
|
|
|
raise ValueError(
|
|
|
|
"[WeibotoreFactory.create_store] Invalid save option only supported csv or db or json ...")
|
|
|
|
return store_class()
|
|
|
|
|
2024-04-06 15:51:03 +00:00
|
|
|
|
2024-08-23 21:52:11 +00:00
|
|
|
async def batch_update_weibo_notes(note_list: List[Dict]):
|
|
|
|
"""
|
|
|
|
Batch update weibo notes
|
|
|
|
Args:
|
|
|
|
note_list:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not note_list:
|
|
|
|
return
|
|
|
|
for note_item in note_list:
|
|
|
|
await update_weibo_note(note_item)
|
|
|
|
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
async def update_weibo_note(note_item: Dict):
|
2024-08-23 21:52:11 +00:00
|
|
|
"""
|
|
|
|
Update weibo note
|
|
|
|
Args:
|
|
|
|
note_item:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not note_item:
|
|
|
|
return
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
mblog: Dict = note_item.get("mblog")
|
|
|
|
user_info: Dict = mblog.get("user")
|
|
|
|
note_id = mblog.get("id")
|
2024-05-28 07:11:52 +00:00
|
|
|
content_text = mblog.get("text")
|
|
|
|
clean_text = re.sub(r"<.*?>", "", content_text)
|
2024-01-14 14:06:31 +00:00
|
|
|
save_content_item = {
|
|
|
|
# 微博信息
|
|
|
|
"note_id": note_id,
|
2024-05-28 07:11:52 +00:00
|
|
|
"content": clean_text,
|
2024-01-14 14:06:31 +00:00
|
|
|
"create_time": utils.rfc2822_to_timestamp(mblog.get("created_at")),
|
|
|
|
"create_date_time": str(utils.rfc2822_to_china_datetime(mblog.get("created_at"))),
|
|
|
|
"liked_count": str(mblog.get("attitudes_count", 0)),
|
|
|
|
"comments_count": str(mblog.get("comments_count", 0)),
|
|
|
|
"shared_count": str(mblog.get("reposts_count", 0)),
|
|
|
|
"last_modify_ts": utils.get_current_timestamp(),
|
|
|
|
"note_url": f"https://m.weibo.cn/detail/{note_id}",
|
|
|
|
"ip_location": mblog.get("region_name", "").replace("发布于 ", ""),
|
|
|
|
|
|
|
|
# 用户信息
|
|
|
|
"user_id": str(user_info.get("id")),
|
|
|
|
"nickname": user_info.get("screen_name", ""),
|
|
|
|
"gender": user_info.get("gender", ""),
|
|
|
|
"profile_url": user_info.get("profile_url", ""),
|
|
|
|
"avatar": user_info.get("profile_image_url", ""),
|
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.weibo.update_weibo_note] weibo note id:{note_id}, title:{save_content_item.get('content')[:24]} ...")
|
|
|
|
await WeibostoreFactory.create_store().store_content(content_item=save_content_item)
|
|
|
|
|
|
|
|
|
|
|
|
async def batch_update_weibo_note_comments(note_id: str, comments: List[Dict]):
|
2024-08-23 21:52:11 +00:00
|
|
|
"""
|
|
|
|
Batch update weibo note comments
|
|
|
|
Args:
|
|
|
|
note_id:
|
|
|
|
comments:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-01-14 14:06:31 +00:00
|
|
|
if not comments:
|
|
|
|
return
|
|
|
|
for comment_item in comments:
|
|
|
|
await update_weibo_note_comment(note_id, comment_item)
|
|
|
|
|
|
|
|
|
|
|
|
async def update_weibo_note_comment(note_id: str, comment_item: Dict):
|
2024-08-23 21:52:11 +00:00
|
|
|
"""
|
|
|
|
Update weibo note comment
|
|
|
|
Args:
|
|
|
|
note_id: weibo note id
|
|
|
|
comment_item: weibo comment item
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not comment_item or not note_id:
|
|
|
|
return
|
2024-01-14 14:06:31 +00:00
|
|
|
comment_id = str(comment_item.get("id"))
|
|
|
|
user_info: Dict = comment_item.get("user")
|
2024-05-28 07:11:52 +00:00
|
|
|
content_text = comment_item.get("text")
|
|
|
|
clean_text = re.sub(r"<.*?>", "", content_text)
|
2024-01-14 14:06:31 +00:00
|
|
|
save_comment_item = {
|
|
|
|
"comment_id": comment_id,
|
|
|
|
"create_time": utils.rfc2822_to_timestamp(comment_item.get("created_at")),
|
|
|
|
"create_date_time": str(utils.rfc2822_to_china_datetime(comment_item.get("created_at"))),
|
|
|
|
"note_id": note_id,
|
2024-05-28 07:11:52 +00:00
|
|
|
"content": clean_text,
|
2024-01-14 14:06:31 +00:00
|
|
|
"sub_comment_count": str(comment_item.get("total_number", 0)),
|
|
|
|
"comment_like_count": str(comment_item.get("like_count", 0)),
|
|
|
|
"last_modify_ts": utils.get_current_timestamp(),
|
|
|
|
"ip_location": comment_item.get("source", "").replace("来自", ""),
|
2024-08-04 16:48:42 +00:00
|
|
|
"parent_comment_id": comment_item.get("rootid", ""),
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
# 用户信息
|
|
|
|
"user_id": str(user_info.get("id")),
|
|
|
|
"nickname": user_info.get("screen_name", ""),
|
|
|
|
"gender": user_info.get("gender", ""),
|
|
|
|
"profile_url": user_info.get("profile_url", ""),
|
|
|
|
"avatar": user_info.get("profile_image_url", ""),
|
|
|
|
}
|
|
|
|
utils.logger.info(
|
2024-04-06 15:51:03 +00:00
|
|
|
f"[store.weibo.update_weibo_note_comment] Weibo note comment: {comment_id}, content: {save_comment_item.get('content', '')[:24]} ...")
|
2024-01-14 14:06:31 +00:00
|
|
|
await WeibostoreFactory.create_store().store_comment(comment_item=save_comment_item)
|
2024-04-09 09:21:52 +00:00
|
|
|
|
2024-08-23 21:52:11 +00:00
|
|
|
|
2024-04-09 09:21:52 +00:00
|
|
|
async def update_weibo_note_image(picid: str, pic_content, extension_file_name):
|
2024-08-23 21:52:11 +00:00
|
|
|
"""
|
|
|
|
Save weibo note image to local
|
|
|
|
Args:
|
|
|
|
picid:
|
|
|
|
pic_content:
|
|
|
|
extension_file_name:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
await WeiboStoreImage().store_image(
|
|
|
|
{"pic_id": picid, "pic_content": pic_content, "extension_file_name": extension_file_name})
|
|
|
|
|
|
|
|
|
|
|
|
async def save_creator(user_id: str, user_info: Dict):
|
|
|
|
"""
|
|
|
|
Save creator information to local
|
|
|
|
Args:
|
|
|
|
user_id:
|
|
|
|
user_info:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
local_db_item = {
|
|
|
|
'user_id': user_id,
|
|
|
|
'nickname': user_info.get('screen_name'),
|
|
|
|
'gender': '女' if user_info.get('gender') == "f" else '男',
|
|
|
|
'avatar': user_info.get('avatar_hd'),
|
|
|
|
'desc': user_info.get('description'),
|
|
|
|
'ip_location': user_info.get("source", "").replace("来自", ""),
|
|
|
|
'follows': user_info.get('follow_count', ''),
|
|
|
|
'fans': user_info.get('followers_count', ''),
|
|
|
|
'tag_list': '',
|
|
|
|
"last_modify_ts": utils.get_current_timestamp(),
|
|
|
|
}
|
|
|
|
utils.logger.info(f"[store.weibo.save_creator] creator:{local_db_item}")
|
|
|
|
await WeibostoreFactory.create_store().store_creator(local_db_item)
|