import os import time import tool @tool.singleton class VideoManger: __video_cache_path = "datas/videos.json" # {"视频内容+视频时间":(评论数量,更新时间)} __video_data_cache_dict = {} def __init__(self): if os.path.exists(self.__video_cache_path): with open(self.__video_cache_path, encoding="utf-8", mode='r') as f: lines = f.readlines() if lines: self.__video_data_cache_dict = eval(lines[0]) def is_need_click(self, video_info): """ 是否需要点击 :param video_info: :return: """ video_name, video_time, comment_count = video_info[0], video_info[1], video_info[2] k = f"{video_name}{video_time}" if k not in self.__video_data_cache_dict: return True if int(comment_count) - int(self.__video_data_cache_dict[k][0]) > 0: return True return False def add_video_infos(self, video_infos): """ 添加视频信息 :param video_infos: :return: """ print("保存所有视频内容") self.__video_data_cache_dict = {f"{x[0]}{x[1]}": (x[2], time.time()) for x in video_infos} # 保存到文件 with open(self.__video_cache_path, encoding="utf-8", mode='w') as f: f.write(f"{self.__video_data_cache_dict}")