from common import Feishu from common.aliyun_log import AliyunLogger from common.cover_gpt4o import CoverAnalyzer from common.pq_cover import PQ from common.redis import insert_pq_data from common.sql_help import sqlCollect import time import random class CoverMethod: @staticmethod def cover_data_dispose(data_list): valid_records = [record for record in data_list if record['cover_sum'] > 0] if valid_records: # 获取最高的cover_sum highest_sum = max( record['cover_sum'] for record in valid_records ) # 获取所有cover_sum等于最高分数的记录 candidates = [record for record in valid_records if record['cover_sum'] == highest_sum] # 随机选择一个 selected_record = random.choice(candidates) highest_cover_url = selected_record['cover_url'] highest_cover_sum = selected_record['cover_sum'] return highest_cover_url, highest_cover_sum else: return None, None @staticmethod def cover_method(video_id): video_id_status = sqlCollect.select_video_cover_method(video_id) # 判断该视频id是否处理过 if video_id_status: AliyunLogger.logging(video_id, "重复视频ID,不做封面处理", "2001") text = ( f"**通知类型**: 重复视频ID,不做封面处理\n" f"**站内视频ID链接**: https://admin.piaoquantv.com/cms/post-detail/{video_id}/detail\n" ) Feishu.finish_bot( text, "https://open.feishu.cn/open-apis/bot/v2/hook/63745308-c53e-4030-90b9-77383b057252", "【 封面修改通知 】" ) return cover_list = PQ.get_pq_all_cover(video_id) # 获取所有封面 if cover_list: # gpt 评分 start_time = time.time() cover_all_data = CoverAnalyzer.cover_list_data(cover_list) end_time = time.time() execution_time = end_time - start_time print(f"代码运行时间: {execution_time:.4f} 秒") highest_cover_url, highest_cover_sum = CoverMethod.cover_data_dispose(cover_all_data) # 获取分数最高的视频封面 if highest_cover_url == None: AliyunLogger.logging( video_id, "视频封面分数都为0,不做封面更改", "2002", cover_all_data) text = ( f"**通知类型**: 视频封面分数都为0,不做封面更改\n" f"**站内视频ID链接**: https://admin.piaoquantv.com/cms/post-detail/{video_id}/detail\n" ) Feishu.finish_bot( text, "https://open.feishu.cn/open-apis/bot/v2/hook/63745308-c53e-4030-90b9-77383b057252", "【 封面修改通知 】" ) return cover_id, old_cover_url = PQ.get_pq_cover_id(video_id) # 获取原封面 和 封面ID if cover_id == None: AliyunLogger.logging( video_id, "获取视频原封面和封面ID失败", "3002", cover_all_data, old_cover_url, highest_cover_url) insert_pq_data( [video_id] ) return if highest_cover_url == old_cover_url: AliyunLogger.logging( video_id, "原封面与新封面一致,不做封面修改", "2003", cover_all_data, old_cover_url, highest_cover_url ) text = ( f"**通知类型**: 视频封面分数都为0,不做封面更改\n" f"**站内视频ID链接**: https://admin.piaoquantv.com/cms/post-detail/{video_id}/detail\n" f"**原视频封面链接**: {old_cover_url}\n" f"**新视频封面链接**: {highest_cover_url}\n" f"**新视频封面分数**: {highest_cover_sum}\n" ) Feishu.finish_bot( text, "https://open.feishu.cn/open-apis/bot/v2/hook/63745308-c53e-4030-90b9-77383b057252", "【 封面修改通知 】" ) return code = PQ.update_pq_video_cover(video_id, highest_cover_url, cover_id) # 修改封面 if code == 0: AliyunLogger.logging( video_id, "修改封面成功", "1000", cover_all_data, old_cover_url, highest_cover_url) text = ( f"**通知类型**: 视频封面修改成功\n" f"**站内视频ID链接**: https://admin.piaoquantv.com/cms/post-detail/{video_id}/detail\n" f"**原视频封面链接**: {old_cover_url}\n" f"**新视频封面链接**: {highest_cover_url}\n" f"**新视频封面分数**: {highest_cover_sum}\n" ) Feishu.finish_bot( text, "https://open.feishu.cn/open-apis/bot/v2/hook/63745308-c53e-4030-90b9-77383b057252", "【 封面修改通知 】" ) sqlCollect.insert_video_cover_method(video_id, cover_id, old_cover_url, highest_cover_url) return else: text = ( f"**通知类型**: 视频封面修改失败\n" f"**站内视频ID链接**: https://admin.piaoquantv.com/cms/post-detail/{video_id}/detail\n" f"**原视频封面链接**: {old_cover_url}\n" f"**新视频封面链接**: {highest_cover_url}\n" f"**新视频封面分数**: {highest_cover_sum}\n" ) Feishu.finish_bot( text, "https://open.feishu.cn/open-apis/bot/v2/hook/63745308-c53e-4030-90b9-77383b057252", "【 封面修改通知 】" ) AliyunLogger.logging( video_id, "修改视频封面失败", "3003", cover_all_data, old_cover_url, highest_cover_url) insert_pq_data( [video_id] ) else: AliyunLogger.logging(video_id, "没有获取到封面", "3001") text = ( f"**通知类型**: 没有获取到封面\n" f"**站内视频ID链接**: https://admin.piaoquantv.com/cms/post-detail/{video_id}/detail\n" ) Feishu.finish_bot( text, "https://open.feishu.cn/open-apis/bot/v2/hook/63745308-c53e-4030-90b9-77383b057252", "【 封面修改通知 】" ) print(f"{video_id}没有获取到封面") return if __name__ == '__main__': CoverMethod.cover_method("")