baidu_search_task.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import time
  6. import requests
  7. from uuid import uuid4
  8. from applications.config import db_config
  9. from applications.functions import whisper
  10. from applications.ai import kimi_ai
  11. from spider.baijiahao_article import bjh_article, bjh_url_list
  12. class BaiduGenerateTask(object):
  13. """
  14. 视频匹配文章流程
  15. 流程
  16. 1. 拿视频id,标题等信息匹配账号
  17. """
  18. def __init__(self, mysql_client):
  19. """
  20. :param mysql_client mysql服务池
  21. """
  22. self.mysql_client = mysql_client
  23. async def whisper_task(self):
  24. """
  25. 执行定时任务,把库里面的视频转文本
  26. :return:
  27. """
  28. select_sql = f"""SELECT video_id FROM {db_config} WHERE status_code = 0 ORDER BY id ASC limit 1;"""
  29. video_list = await self.mysql_client.select(select_sql)
  30. async def whisper_and_update(video_id, mysql_client):
  31. """
  32. whisper处理视频并且把信息更新到mysql表中
  33. :param video_id:
  34. :param mysql_client:
  35. :return:
  36. """
  37. try:
  38. w_response = whisper(video_id)
  39. except:
  40. w_response = {"text": "whisper failed"}
  41. print(w_response)
  42. text = w_response['text'].replace("'", "")
  43. update_sql = f"""
  44. UPDATE {db_config}
  45. SET
  46. video_text = %s,
  47. status_code = %s
  48. WHERE video_id = %s;
  49. """
  50. print(update_sql)
  51. await mysql_client.async_insert(sql=update_sql, params=(text, 1, video_id))
  52. for vid in video_list:
  53. await whisper_and_update(video_id=vid[0], mysql_client=self.mysql_client)
  54. async def materials_task(self):
  55. """
  56. 获取task的材料
  57. :return:
  58. """
  59. select_sql = f"""SELECT task_id, video_title, video_text FROM {db_config} WHERE status_code = 1 ORDER BY id ASC limit 1;"""
  60. task_list = await self.mysql_client.select(select_sql)
  61. async def baidu_search(task_tuple, mysql_client):
  62. """
  63. :param task_tuple:
  64. :param mysql_client:
  65. """
  66. task_id, title, text = task_tuple
  67. url_list = bjh_url_list(title)
  68. L = []
  69. for url in url_list:
  70. a_text, img_list = bjh_article(url.split("&")[0])
  71. obj = {
  72. "text": a_text,
  73. "img_list": img_list
  74. }
  75. L.append(obj)
  76. update_sql = f"""
  77. UPDATE {db_config}
  78. SET materials = %s, status_code = %s
  79. WHERE task_id = %s;
  80. """
  81. print(update_sql)
  82. await mysql_client.async_insert(sql=update_sql, params=(L, 2, task_id))
  83. for task in task_list:
  84. await baidu_search(task, self.mysql_client)