# coding=utf-8 from dataclasses import dataclass, field import os from datetime import datetime def convert_timestamp(timestamp): """ 自动识别时间戳是秒级还是毫秒级,并转换为 datetime 对象。 参数: timestamp -- 时间戳(整数或浮点数) 返回: datetime 对象 """ # 如果时间戳大于 1e10,认为是毫秒级时间戳 if timestamp > 1e10: timestamp /= 1000.0 # 转换为秒级时间戳 return datetime.fromtimestamp(timestamp) @dataclass class AiSearchResult: """ ai搜索结果对象 """ # 标题 title: str = '' # url url: str = '' # 来源 host_name: str = '' # 描述 body: str = '' # 发布时间 publish_time: str|int|float = '' #是否被ai引用 is_referenced: str = '0' #情感倾向" 1- 中立 2- 正面 3- 负面 sentiment_type = 0 #情感类型 type = 0 def __post_init__(self): if isinstance(self.publish_time, float): self.publish_time = int(self.publish_time) if isinstance(self.publish_time, int): self.publish_time = convert_timestamp(self.publish_time).strftime('%Y-%m-%d') if isinstance(self.publish_time, str): try: now = datetime.now() publish = datetime.strptime(self.publish_time, f'%m-%d') except ValueError: return self.publish_time = publish.strftime(f'{now.year}-%m-%d') @dataclass class AiAnswer: """ ai回答对象 """ platform_id: int platform_name: str prompt: str keyword: str answer: str = '' search_result: list[AiSearchResult] = field(default_factory=list) screenshot_file: str = '' # 状态 run_status: bool = True def __post_init__(self): self.screenshot_file = os.path.abspath(self.screenshot_file)