get_off_videos.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """
  2. @author: luojunhui
  3. @desc: auto get off videos in 3 days for safety
  4. """
  5. import time
  6. from pymysql.cursors import DictCursor
  7. from tqdm import tqdm
  8. from applications import log, PQAPI
  9. from applications.db import DatabaseConnector
  10. from applications.api import FeishuBotApi
  11. from config import long_articles_config
  12. class GetOffVideos:
  13. def __init__(self):
  14. self.db_client = DatabaseConnector(long_articles_config)
  15. self.db_client.connect()
  16. self.pq_api = PQAPI()
  17. self.feishu_robot = FeishuBotApi()
  18. self.EXPIRE_TIME = 3 * 24 * 60 * 60
  19. def get_long_articles_videos(self, timestamp):
  20. """
  21. 获取待下架的视频
  22. :return:
  23. """
  24. fetch_query = f"""
  25. select video_id from get_off_videos where video_status = %s and publish_time < %s;
  26. """
  27. article_list = self.db_client.fetch(
  28. query=fetch_query, cursor_type=DictCursor, params=(1, timestamp)
  29. )
  30. log(
  31. task="getOffVideosDaily",
  32. function="get_long_articles_videos",
  33. message="查找到视频 id_list,一共{}条视频".format(len(article_list)),
  34. )
  35. return article_list
  36. def update_video_statsu(self, video_id, ori_status, new_status):
  37. """
  38. 更新get_off_videos表内,视频状态
  39. """
  40. now_timestamp = int(time.time())
  41. update_query = f"""
  42. update get_off_videos
  43. set video_status = %s, get_off_time = %s
  44. where video_id = %s and video_status = %s;
  45. """
  46. self.db_client.save(
  47. query=update_query, params=(new_status, now_timestamp, video_id, ori_status)
  48. )
  49. def change_video_id_status(self, video_id):
  50. """
  51. 修改视频状态
  52. """
  53. response = self.pq_api.changeVideoStatus(video_id, 2)
  54. if response:
  55. self.update_video_statsu(video_id=video_id, ori_status=1, new_status=0)
  56. else:
  57. log(
  58. task="getOffVideosDaily",
  59. function="change_video_status",
  60. status="fail",
  61. message="请求票圈修改状态异常: ---video_id = {}".format(video_id),
  62. )
  63. def check_task(self):
  64. three_days_ago = int(time.time()) - self.EXPIRE_TIME
  65. fetch_query = f"""
  66. select video_id from get_off_videos where publish_time < %s and video_status = %s;
  67. """
  68. video_id_list = self.db_client.fetch(
  69. fetch_query, cursor_type=DictCursor, params=(three_days_ago, 1)
  70. )
  71. if video_id_list:
  72. video_id_list = [i["video_id"] for i in video_id_list]
  73. for vid in video_id_list:
  74. try:
  75. self.change_video_id_status(video_id=vid)
  76. except Exception as e:
  77. log(
  78. task="getOffVideosDaily",
  79. function="check_job",
  80. status="fail",
  81. message="task2下架单个视频失败,video_id={}, 报错信息={}".format(
  82. vid, e
  83. ),
  84. )
  85. time.sleep(10)
  86. # check
  87. rest = self.db_client.fetch(
  88. query=fetch_query, cursor_type=DictCursor, params=(three_days_ago, 1)
  89. )
  90. if rest:
  91. rest = [i["video_id"] for i in rest]
  92. self.feishu_robot.bot(
  93. title="get_off_videos 下架视频异常", detail={"video_id": rest}
  94. )
  95. else:
  96. return
  97. else:
  98. return
  99. def get_off_job(self):
  100. """
  101. 自动下架视频任务
  102. """
  103. three_days_before = int(time.time()) - self.EXPIRE_TIME
  104. fetch_response = self.get_long_articles_videos(timestamp=three_days_before)
  105. video_id_list = [i["video_id"] for i in fetch_response]
  106. for video_id in tqdm(video_id_list):
  107. try:
  108. self.change_video_id_status(video_id=video_id)
  109. except Exception as e:
  110. log(
  111. task="getOffVideosDaily",
  112. function="get_off_job",
  113. status="fail",
  114. message="get_off_job下架单个视频失败,video_id={}, 报错信息={}".format(
  115. video_id, e
  116. ),
  117. )
  118. def deal(self):
  119. self.get_off_job()
  120. time.sleep(60)
  121. self.check_task()
  122. self.feishu_robot.bot(
  123. title="get_off_videos 下架视频任务完成",
  124. detail={"message": "get_off_videos 下架视频任务完成"},
  125. env="long_articles_task",
  126. mention=False,
  127. )