basic.py 4.1 KB

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