2024-01-14 14:06:31 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# @Author : relakkes@gmail.com
|
|
|
|
|
# @Time : 2024/1/14 21:35
|
|
|
|
|
# @Desc : 微博存储实现类
|
2024-01-15 16:40:07 +00:00
|
|
|
|
import asyncio
|
2024-01-14 14:06:31 +00:00
|
|
|
|
import csv
|
2024-01-14 14:40:01 +00:00
|
|
|
|
import json
|
|
|
|
|
import os
|
2024-01-14 14:06:31 +00:00
|
|
|
|
import pathlib
|
|
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
|
|
import aiofiles
|
2024-04-17 15:13:40 +00:00
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
|
from base.base_crawler import AbstractStore
|
2024-06-12 07:33:39 +00:00
|
|
|
|
from tools import utils,words
|
2024-01-14 14:06:31 +00:00
|
|
|
|
from var import crawler_type_var
|
2024-06-12 07:33:39 +00:00
|
|
|
|
import config
|
2024-05-08 14:26:35 +00:00
|
|
|
|
|
|
|
|
|
def calculate_number_of_files(file_store_path: str) -> int:
|
2024-04-26 02:57:44 +00:00
|
|
|
|
"""计算数据保存文件的前部分排序数字,支持每次运行代码不写到同一个文件中
|
|
|
|
|
Args:
|
|
|
|
|
file_store_path;
|
|
|
|
|
Returns:
|
|
|
|
|
file nums
|
|
|
|
|
"""
|
|
|
|
|
if not os.path.exists(file_store_path):
|
|
|
|
|
return 1
|
2024-05-08 14:26:35 +00:00
|
|
|
|
try:
|
|
|
|
|
return max([int(file_name.split("_")[0])for file_name in os.listdir(file_store_path)])+1
|
|
|
|
|
except ValueError:
|
|
|
|
|
return 1
|
2024-04-26 02:57:44 +00:00
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
class WeiboCsvStoreImplement(AbstractStore):
|
|
|
|
|
csv_store_path: str = "data/weibo"
|
2024-05-08 14:26:35 +00:00
|
|
|
|
file_count:int=calculate_number_of_files(csv_store_path)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
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/bilibili/search_comments_20240114.csv ...
|
|
|
|
|
|
|
|
|
|
"""
|
2024-04-26 02:57:44 +00:00
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
|
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):
|
|
|
|
|
"""
|
|
|
|
|
Weibo 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):
|
|
|
|
|
"""
|
|
|
|
|
Weibo 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 WeiboDbStoreImplement(AbstractStore):
|
|
|
|
|
async def store_content(self, content_item: Dict):
|
|
|
|
|
"""
|
|
|
|
|
Weibo content DB storage implementation
|
|
|
|
|
Args:
|
|
|
|
|
content_item: content item dict
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2024-04-06 14:11:10 +00:00
|
|
|
|
|
|
|
|
|
from .weibo_store_sql import (add_new_content,
|
|
|
|
|
query_content_by_content_id,
|
|
|
|
|
update_content_by_content_id)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
note_id = content_item.get("note_id")
|
2024-04-06 14:11:10 +00:00
|
|
|
|
note_detail: Dict = await query_content_by_content_id(content_id=note_id)
|
|
|
|
|
if not note_detail:
|
2024-01-14 14:06:31 +00:00
|
|
|
|
content_item["add_ts"] = utils.get_current_timestamp()
|
2024-04-06 14:11:10 +00:00
|
|
|
|
await add_new_content(content_item)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
else:
|
2024-04-06 14:11:10 +00:00
|
|
|
|
await update_content_by_content_id(note_id, content_item=content_item)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
async def store_comment(self, comment_item: Dict):
|
|
|
|
|
"""
|
|
|
|
|
Weibo content DB storage implementation
|
|
|
|
|
Args:
|
|
|
|
|
comment_item: comment item dict
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2024-04-06 14:11:10 +00:00
|
|
|
|
from .weibo_store_sql import (add_new_comment,
|
|
|
|
|
query_comment_by_comment_id,
|
|
|
|
|
update_comment_by_comment_id)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
comment_id = comment_item.get("comment_id")
|
2024-04-06 14:11:10 +00:00
|
|
|
|
comment_detail: Dict = await query_comment_by_comment_id(comment_id=comment_id)
|
|
|
|
|
if not comment_detail:
|
2024-01-14 14:06:31 +00:00
|
|
|
|
comment_item["add_ts"] = utils.get_current_timestamp()
|
2024-04-06 14:11:10 +00:00
|
|
|
|
await add_new_comment(comment_item)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
else:
|
2024-04-06 14:11:10 +00:00
|
|
|
|
await update_comment_by_comment_id(comment_id, comment_item=comment_item)
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
|
2024-01-14 14:40:01 +00:00
|
|
|
|
class WeiboJsonStoreImplement(AbstractStore):
|
2024-06-12 07:33:39 +00:00
|
|
|
|
json_store_path: str = "data/weibo/json"
|
|
|
|
|
words_store_path: str = "data/weibo/words"
|
2024-01-15 16:40:07 +00:00
|
|
|
|
lock = asyncio.Lock()
|
2024-05-08 14:26:35 +00:00
|
|
|
|
file_count:int=calculate_number_of_files(json_store_path)
|
2024-06-12 07:33:39 +00:00
|
|
|
|
WordCloud = words.AsyncWordCloudGenerator()
|
2024-04-26 02:57:44 +00:00
|
|
|
|
|
2024-01-14 14:40:01 +00:00
|
|
|
|
|
2024-06-12 07:33:39 +00:00
|
|
|
|
def make_save_file_name(self, store_type: str) -> (str,str):
|
2024-01-14 14:40:01 +00:00
|
|
|
|
"""
|
|
|
|
|
make save file name by store type
|
|
|
|
|
Args:
|
|
|
|
|
store_type: Save type contains content and comments(contents | comments)
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2024-04-26 02:57:44 +00:00
|
|
|
|
|
2024-06-12 07:33:39 +00:00
|
|
|
|
return (
|
|
|
|
|
f"{self.json_store_path}/{crawler_type_var.get()}_{store_type}_{utils.get_current_date()}.json",
|
|
|
|
|
f"{self.words_store_path}/{crawler_type_var.get()}_{store_type}_{utils.get_current_date()}"
|
|
|
|
|
)
|
2024-01-14 14:40:01 +00:00
|
|
|
|
|
|
|
|
|
async def save_data_to_json(self, save_item: Dict, store_type: str):
|
|
|
|
|
"""
|
|
|
|
|
Below is a simple way to save it in json format.
|
|
|
|
|
Args:
|
|
|
|
|
save_item: save content dict info
|
|
|
|
|
store_type: Save type contains content and comments(contents | comments)
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
pathlib.Path(self.json_store_path).mkdir(parents=True, exist_ok=True)
|
2024-06-12 07:33:39 +00:00
|
|
|
|
pathlib.Path(self.words_store_path).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
save_file_name,words_file_name_prefix = self.make_save_file_name(store_type=store_type)
|
2024-01-14 14:40:01 +00:00
|
|
|
|
save_data = []
|
|
|
|
|
|
2024-01-15 16:40:07 +00:00
|
|
|
|
async with self.lock:
|
|
|
|
|
if os.path.exists(save_file_name):
|
|
|
|
|
async with aiofiles.open(save_file_name, 'r', encoding='utf-8') as file:
|
|
|
|
|
save_data = json.loads(await file.read())
|
|
|
|
|
|
|
|
|
|
save_data.append(save_item)
|
|
|
|
|
async with aiofiles.open(save_file_name, 'w', encoding='utf-8') as file:
|
|
|
|
|
await file.write(json.dumps(save_data, ensure_ascii=False))
|
2024-01-14 14:40:01 +00:00
|
|
|
|
|
2024-06-12 07:33:39 +00:00
|
|
|
|
if config.ENABLE_GET_COMMENTS and config.ENABLE_GET_WORDCLOUD:
|
|
|
|
|
try:
|
|
|
|
|
await self.WordCloud.generate_word_frequency_and_cloud(save_data, words_file_name_prefix)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
|
async def store_content(self, content_item: Dict):
|
2024-01-14 14:40:01 +00:00
|
|
|
|
"""
|
|
|
|
|
content JSON storage implementation
|
|
|
|
|
Args:
|
|
|
|
|
content_item:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
await self.save_data_to_json(content_item, "contents")
|
2024-01-14 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
async def store_comment(self, comment_item: Dict):
|
2024-01-14 14:40:01 +00:00
|
|
|
|
"""
|
|
|
|
|
comment JSON storage implementatio
|
|
|
|
|
Args:
|
|
|
|
|
comment_item:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
await self.save_data_to_json(comment_item, "comments")
|