basic.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import os
  2. import time
  3. import datetime
  4. import requests
  5. from applications.db import DatabaseConnector
  6. def get_status_field_by_task(task: str) -> tuple[str, str]:
  7. match task:
  8. case "upload":
  9. status = "upload_status"
  10. update_timestamp = "upload_status_ts"
  11. case "extract":
  12. status = "extract_status"
  13. update_timestamp = "extract_status_ts"
  14. case "get_cover":
  15. status = "get_cover_status"
  16. update_timestamp = "get_cover_status_ts"
  17. case _:
  18. raise ValueError(f"Unexpected task: {task}")
  19. return status, update_timestamp
  20. def roll_back_lock_tasks(
  21. db_client: DatabaseConnector,
  22. task: str,
  23. max_process_time: int,
  24. init_status: int,
  25. processing_status: int
  26. ) -> int:
  27. """
  28. rollback tasks which have been locked for a long time
  29. """
  30. status, update_timestamp = get_status_field_by_task(task)
  31. now_timestamp = int(time.time())
  32. timestamp_threshold = now_timestamp - max_process_time
  33. update_query = f"""
  34. update long_articles_new_video_cover
  35. set {status} = %s
  36. where {status} = %s and {update_timestamp} < %s;
  37. """
  38. rollback_rows = db_client.save(
  39. query=update_query, params=(init_status, processing_status, timestamp_threshold)
  40. )
  41. return rollback_rows
  42. def download_file(task_id, oss_path):
  43. """
  44. 下载视频文件
  45. """
  46. video_url = "https://rescdn.yishihui.com/" + oss_path
  47. file_name = "static/{}.mp4".format(task_id)
  48. if os.path.exists(file_name):
  49. return file_name
  50. proxies = {"http": None, "https": None}
  51. with open(file_name, "wb") as f:
  52. response = requests.get(video_url, proxies=proxies)
  53. f.write(response.content)
  54. return file_name
  55. def generate_summary_prompt(text):
  56. prompt = f"""
  57. 你是1个优秀的公众号文章写作大师,我对你有以下要求
  58. 视频总结:{text}
  59. 第一个要求:请仔细阅读以上视频总结,挑选其中最吸引人的情节或话题,总结为100字左右文章精彩总结(字数计算包括标点符号),这部分内容为段落1。
  60. 句子段落之间以悬念承接,可以吸引读者往下读第二句。
  61. 第二个要求:在这100字内容的结尾处,增加1-2句话的引导,引导大家去观看上面的视频了解详情,可以加一些emoji表情。注意是点击上面的视频,不是下面的视频。这部分内容为段落2。
  62. 你最终输出一段总结内容,将第一段和第二段之间空格一行。不用加标题或者主题,也不用写第几段、多少字这样的话。整体的语言风格要口语化、直接点,要让60岁以上的老年人能看懂、能共情。人的名字尽量用全名,不用简称。
  63. """
  64. return prompt
  65. def update_task_queue_status(
  66. db_client: DatabaseConnector,
  67. task_id: int,
  68. task: str,
  69. ori_status: int,
  70. new_status: int) -> int:
  71. # update task queue status
  72. status, update_timestamp = get_status_field_by_task(task)
  73. update_query = f"""
  74. update long_articles_new_video_cover
  75. set {status} = %s, {update_timestamp} = %s
  76. where {status} = %s and id = %s;
  77. """
  78. update_rows = db_client.save(
  79. query=update_query,
  80. params=(
  81. new_status,
  82. datetime.datetime.now(),
  83. ori_status,
  84. task_id,
  85. ),
  86. )
  87. return update_rows