2024-08-05 10:51:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import List
|
|
|
|
|
2024-08-08 06:19:32 +00:00
|
|
|
from model.m_baidu_tieba import TiebaComment, TiebaNote
|
2024-08-23 00:29:24 +00:00
|
|
|
from var import source_keyword_var
|
2024-08-08 06:19:32 +00:00
|
|
|
|
2024-08-05 10:51:51 +00:00
|
|
|
from . import tieba_store_impl
|
|
|
|
from .tieba_store_impl import *
|
|
|
|
|
|
|
|
|
|
|
|
class TieBaStoreFactory:
|
|
|
|
STORES = {
|
|
|
|
"csv": TieBaCsvStoreImplement,
|
|
|
|
"db": TieBaDbStoreImplement,
|
|
|
|
"json": TieBaJsonStoreImplement
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_store() -> AbstractStore:
|
|
|
|
store_class = TieBaStoreFactory.STORES.get(config.SAVE_DATA_OPTION)
|
|
|
|
if not store_class:
|
|
|
|
raise ValueError(
|
|
|
|
"[TieBaStoreFactory.create_store] Invalid save option only supported csv or db or json ...")
|
|
|
|
return store_class()
|
|
|
|
|
2024-08-24 01:12:03 +00:00
|
|
|
async def batch_update_tieba_notes(note_list: List[TiebaNote]):
|
|
|
|
"""
|
|
|
|
Batch update tieba notes
|
|
|
|
Args:
|
|
|
|
note_list:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not note_list:
|
|
|
|
return
|
|
|
|
for note_item in note_list:
|
|
|
|
await update_tieba_note(note_item)
|
2024-08-05 10:51:51 +00:00
|
|
|
|
2024-08-06 17:01:21 +00:00
|
|
|
async def update_tieba_note(note_item: TiebaNote):
|
|
|
|
"""
|
|
|
|
Add or Update tieba note
|
|
|
|
Args:
|
|
|
|
note_item:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-08-23 00:29:24 +00:00
|
|
|
note_item.source_keyword = source_keyword_var.get()
|
2024-08-06 17:01:21 +00:00
|
|
|
save_note_item = note_item.model_dump()
|
|
|
|
save_note_item.update({"last_modify_ts": utils.get_current_timestamp()})
|
|
|
|
utils.logger.info(f"[store.tieba.update_tieba_note] tieba note: {save_note_item}")
|
|
|
|
|
|
|
|
await TieBaStoreFactory.create_store().store_content(save_note_item)
|
2024-08-05 10:51:51 +00:00
|
|
|
|
|
|
|
|
2024-08-06 18:34:56 +00:00
|
|
|
async def batch_update_tieba_note_comments(note_id:str, comments: List[TiebaComment]):
|
|
|
|
"""
|
|
|
|
Batch update tieba note comments
|
|
|
|
Args:
|
|
|
|
note_id:
|
|
|
|
comments:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-08-05 10:51:51 +00:00
|
|
|
if not comments:
|
|
|
|
return
|
|
|
|
for comment_item in comments:
|
|
|
|
await update_tieba_note_comment(note_id, comment_item)
|
|
|
|
|
|
|
|
|
2024-08-06 18:34:56 +00:00
|
|
|
async def update_tieba_note_comment(note_id: str, comment_item: TiebaComment):
|
2024-08-05 10:51:51 +00:00
|
|
|
"""
|
|
|
|
Update tieba note comment
|
|
|
|
Args:
|
|
|
|
note_id:
|
|
|
|
comment_item:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2024-08-06 18:34:56 +00:00
|
|
|
save_comment_item = comment_item.model_dump()
|
|
|
|
save_comment_item.update({"last_modify_ts": utils.get_current_timestamp()})
|
|
|
|
utils.logger.info(f"[store.tieba.update_tieba_note_comment] tieba note id: {note_id} comment:{save_comment_item}")
|
|
|
|
await TieBaStoreFactory.create_store().store_comment(save_comment_item)
|
2024-08-24 01:12:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
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('nickname'),
|
|
|
|
'gender': '女' if user_info.get('gender') == "f" else '男',
|
|
|
|
'avatar': user_info.get('avatar'),
|
|
|
|
'ip_location': user_info.get("ip_location", ""),
|
|
|
|
'follows': user_info.get('follow_count', ''),
|
|
|
|
'fans': user_info.get('followers_count', ''),
|
|
|
|
'follow_tieba_list': user_info.get("tieba_list", ''),
|
|
|
|
'last_modify_ts': utils.get_current_timestamp(),
|
|
|
|
'registration_duration': user_info.get("registration_duration", ""),
|
|
|
|
}
|
|
|
|
utils.logger.info(f"[store.tieba.save_creator] creator:{local_db_item}")
|
|
|
|
await TieBaStoreFactory.create_store().store_creator(local_db_item)
|