""" @author: luojunhui """ import json import time import requests from uuid import uuid4 from applications.config import db_config from applications.functions import whisper from applications.ai import kimi_ai from spider.baijiahao_article import bjh_article, bjh_url_list class BaiduGenerateTask(object): """ 视频匹配文章流程 流程 1. 拿视频id,标题等信息匹配账号 """ def __init__(self, mysql_client): """ :param mysql_client mysql服务池 """ self.mysql_client = mysql_client async def whisper_task(self): """ 执行定时任务,把库里面的视频转文本 :return: """ select_sql = f"""SELECT video_id FROM {db_config} WHERE status_code = 0 ORDER BY id ASC limit 1;""" video_list = await self.mysql_client.select(select_sql) async def whisper_and_update(video_id, mysql_client): """ whisper处理视频并且把信息更新到mysql表中 :param video_id: :param mysql_client: :return: """ try: w_response = whisper(video_id) except: w_response = {"text": "whisper failed"} print(w_response) text = w_response['text'].replace("'", "") update_sql = f""" UPDATE {db_config} SET video_text = %s, status_code = %s WHERE video_id = %s; """ print(update_sql) await mysql_client.async_insert(sql=update_sql, params=(text, 1, video_id)) for vid in video_list: await whisper_and_update(video_id=vid[0], mysql_client=self.mysql_client) async def materials_task(self): """ 获取task的材料 :return: """ select_sql = f"""SELECT task_id, video_title, video_text FROM {db_config} WHERE status_code = 1 ORDER BY id ASC limit 1;""" task_list = await self.mysql_client.select(select_sql) async def baidu_search(task_tuple, mysql_client): """ :param task_tuple: :param mysql_client: """ task_id, title, text = task_tuple url_list = bjh_url_list(title) L = [] for url in url_list: a_text, img_list = bjh_article(url.split("&")[0]) obj = { "text": a_text, "img_list": img_list } L.append(obj) update_sql = f""" UPDATE {db_config} SET materials = %s, status_code = %s WHERE task_id = %s; """ print(update_sql) await mysql_client.async_insert(sql=update_sql, params=(L, 2, task_id)) for task in task_list: await baidu_search(task, self.mysql_client)