131 lines
4.7 KiB
Python
131 lines
4.7 KiB
Python
|
# -*- coding: utf-8 -*-
|
|||
|
# @Author : relakkes@gmail.com
|
|||
|
# @Time : 2024/1/14 18:46
|
|||
|
# @Desc : 抖音存储实现类
|
|||
|
|
|||
|
import csv
|
|||
|
import pathlib
|
|||
|
from typing import Dict
|
|||
|
|
|||
|
import aiofiles
|
|||
|
from tortoise.contrib.pydantic import pydantic_model_creator
|
|||
|
|
|||
|
from base.base_crawler import AbstractStore
|
|||
|
from tools import utils
|
|||
|
from var import crawler_type_var
|
|||
|
|
|||
|
|
|||
|
class DouyinCsvStoreImplement(AbstractStore):
|
|||
|
csv_store_path: str = "data/douyin"
|
|||
|
|
|||
|
def make_save_file_name(self, store_type: str) -> str:
|
|||
|
"""
|
|||
|
make save file name by store type
|
|||
|
Args:
|
|||
|
store_type: contents or comments
|
|||
|
|
|||
|
Returns: eg: data/douyin/search_comments_20240114.csv ...
|
|||
|
|
|||
|
"""
|
|||
|
return f"{self.csv_store_path}/{crawler_type_var.get()}_{store_type}_{utils.get_current_date()}.csv"
|
|||
|
|
|||
|
async def save_data_to_csv(self, save_item: Dict, store_type: str):
|
|||
|
"""
|
|||
|
Below is a simple way to save it in CSV format.
|
|||
|
Args:
|
|||
|
save_item: save content dict info
|
|||
|
store_type: Save type contains content and comments(contents | comments)
|
|||
|
|
|||
|
Returns: no returns
|
|||
|
|
|||
|
"""
|
|||
|
pathlib.Path(self.csv_store_path).mkdir(parents=True, exist_ok=True)
|
|||
|
save_file_name = self.make_save_file_name(store_type=store_type)
|
|||
|
async with aiofiles.open(save_file_name, mode='a+', encoding="utf-8-sig", newline="") as f:
|
|||
|
writer = csv.writer(f)
|
|||
|
if await f.tell() == 0:
|
|||
|
await writer.writerow(save_item.keys())
|
|||
|
await writer.writerow(save_item.values())
|
|||
|
|
|||
|
async def store_content(self, content_item: Dict):
|
|||
|
"""
|
|||
|
Xiaohongshu content CSV storage implementation
|
|||
|
Args:
|
|||
|
content_item: note item dict
|
|||
|
|
|||
|
Returns:
|
|||
|
|
|||
|
"""
|
|||
|
await self.save_data_to_csv(save_item=content_item, store_type="contents")
|
|||
|
|
|||
|
async def store_comment(self, comment_item: Dict):
|
|||
|
"""
|
|||
|
Xiaohongshu comment CSV storage implementation
|
|||
|
Args:
|
|||
|
comment_item: comment item dict
|
|||
|
|
|||
|
Returns:
|
|||
|
|
|||
|
"""
|
|||
|
await self.save_data_to_csv(save_item=comment_item, store_type="comments")
|
|||
|
|
|||
|
|
|||
|
class DouyinDbStoreImplement(AbstractStore):
|
|||
|
async def store_content(self, content_item: Dict):
|
|||
|
"""
|
|||
|
Douyin content DB storage implementation
|
|||
|
Args:
|
|||
|
content_item: content item dict
|
|||
|
|
|||
|
Returns:
|
|||
|
|
|||
|
"""
|
|||
|
from .douyin_store_db_types import DouyinAweme
|
|||
|
aweme_id = content_item.get("aweme_id")
|
|||
|
if not await DouyinAweme.filter(aweme_id=aweme_id).exists():
|
|||
|
content_item["add_ts"] = utils.get_current_timestamp()
|
|||
|
douyin_aweme_pydantic = pydantic_model_creator(DouyinAweme, name='DouyinAwemeCreate', exclude=('id',))
|
|||
|
douyin_data = douyin_aweme_pydantic(**content_item)
|
|||
|
douyin_aweme_pydantic.model_validate(douyin_data)
|
|||
|
await DouyinAweme.create(**douyin_data.dict())
|
|||
|
else:
|
|||
|
douyin_aweme_pydantic = pydantic_model_creator(DouyinAweme, name='DouyinAwemeUpdate',
|
|||
|
exclude=('id', 'add_ts'))
|
|||
|
douyin_data = douyin_aweme_pydantic(**content_item)
|
|||
|
douyin_aweme_pydantic.model_validate(douyin_data)
|
|||
|
await DouyinAweme.filter(aweme_id=aweme_id).update(**douyin_data.model_dump())
|
|||
|
|
|||
|
async def store_comment(self, comment_item: Dict):
|
|||
|
"""
|
|||
|
Douyin content DB storage implementation
|
|||
|
Args:
|
|||
|
comment_item: comment item dict
|
|||
|
|
|||
|
Returns:
|
|||
|
|
|||
|
"""
|
|||
|
from .douyin_store_db_types import DouyinAwemeComment
|
|||
|
comment_id = comment_item.get("comment_id")
|
|||
|
if not await DouyinAwemeComment.filter(comment_id=comment_id).exists():
|
|||
|
comment_item["add_ts"] = utils.get_current_timestamp()
|
|||
|
comment_pydantic = pydantic_model_creator(DouyinAwemeComment, name='DouyinAwemeCommentCreate',
|
|||
|
exclude=('id',))
|
|||
|
comment_data = comment_pydantic(**comment_item)
|
|||
|
comment_pydantic.model_validate(comment_data)
|
|||
|
await DouyinAwemeComment.create(**comment_data.model_dump())
|
|||
|
else:
|
|||
|
comment_pydantic = pydantic_model_creator(DouyinAwemeComment, name='DouyinAwemeCommentUpdate',
|
|||
|
exclude=('id', 'add_ts'))
|
|||
|
comment_data = comment_pydantic(**comment_item)
|
|||
|
comment_pydantic.model_validate(comment_data)
|
|||
|
await DouyinAwemeComment.filter(comment_id=comment_item).update(**comment_data.model_dump())
|
|||
|
|
|||
|
|
|||
|
class DouyinJsonStoreImplement(AbstractStore):
|
|||
|
async def store_content(self, content_item: Dict):
|
|||
|
pass
|
|||
|
|
|||
|
async def store_comment(self, comment_item: Dict):
|
|||
|
pass
|
|||
|
|