getOffVideosDaily.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """
  2. @author: luojunhui
  3. @description: GetOffVideos Daily
  4. """
  5. import time
  6. import datetime
  7. import traceback
  8. from tqdm import tqdm
  9. from applications import PQMySQL, PQAPI, log, bot
  10. EXPIRE_TIME = 3 * 24 * 60 * 60
  11. class AutoGetOffVideos(object):
  12. """
  13. 自动下架视频
  14. """
  15. def __init__(self):
  16. self.db_client = None
  17. self.pq_api = None
  18. def base_init(self):
  19. """
  20. 初始化数据库和票圈公共方法
  21. :return:
  22. """
  23. try:
  24. self.db_client = PQMySQL()
  25. except Exception as e:
  26. error_msg = traceback.format_exc()
  27. bot(
  28. title="自动下架视频任务,数据库初始化失败",
  29. detail={
  30. "error": str(e),
  31. "error_msg": error_msg
  32. }
  33. )
  34. return False
  35. try:
  36. self.pq_api = PQAPI()
  37. except Exception as e:
  38. error_msg = traceback.format_exc()
  39. bot(
  40. title="自动下架视频任务,pq公共方法连接失败",
  41. detail={
  42. "error": str(e),
  43. "error_msg": error_msg
  44. }
  45. )
  46. return False
  47. return True
  48. def get_long_articles_videos(self, timestamp):
  49. """
  50. 获取待下架的视频
  51. :return:
  52. """
  53. select_sql = f"""
  54. SELECT video_id
  55. FROM get_off_videos
  56. WHERE video_status = 1 and publish_time < {timestamp};
  57. """
  58. result = self.db_client.select(sql=select_sql)
  59. log(
  60. task="getOffVideosDaily",
  61. function="get_long_articles_videos",
  62. message="查找到视频 id_list,一共{}条视频".format(len(result))
  63. )
  64. return result
  65. def update_video_id_status(self, video_id):
  66. """
  67. 修改数据库内视频状态
  68. :param video_id:
  69. :return:
  70. """
  71. timestamp = int(time.time())
  72. select_sql = f"""
  73. UPDATE get_off_videos
  74. SET video_status = 0, get_off_time = {timestamp}
  75. WHERE video_id = %s;
  76. """
  77. try:
  78. self.db_client.update(
  79. sql=select_sql,
  80. params=video_id
  81. )
  82. log(
  83. task="getOffVideosDaily",
  84. function="update_video_id_status",
  85. message="成功修改视频状态",
  86. data={"video_id": video_id}
  87. )
  88. except Exception as e:
  89. log(
  90. task="getOffVideosDaily",
  91. function="update_video_id_status",
  92. message="修改视频状态失败--- 推测 sql 问题,报错信息:{}".format(e),
  93. status="fail",
  94. data={"video_id": video_id}
  95. )
  96. def change_video_id_status(self, video_id):
  97. """
  98. 修改视频id状态
  99. :return:
  100. """
  101. response = self.pq_api.changeVideoStatus(video_id, 2)
  102. if response:
  103. self.update_video_id_status(video_id=video_id)
  104. else:
  105. log(
  106. task="getOffVideosDaily",
  107. function="change_video_id_status",
  108. status="fail",
  109. message="请求票圈修改状态异常: ---video_id = {}".format(video_id),
  110. )
  111. bot(
  112. title="get_off_videos 下架视频异常",
  113. detail={
  114. "video_id": video_id
  115. },
  116. mention=False
  117. )
  118. def get_off_job(self):
  119. """
  120. 已经请求超过3天的视频全部下架
  121. :return:
  122. """
  123. now_stamp = int(time.time())
  124. three_days_before = now_stamp - EXPIRE_TIME
  125. video_tuple = self.get_long_articles_videos(timestamp=three_days_before)
  126. vid_list = [i[0] for i in video_tuple]
  127. for video_id in tqdm(vid_list):
  128. try:
  129. self.change_video_id_status(video_id=video_id)
  130. except Exception as e:
  131. log(
  132. task="getOffVideosDaily",
  133. function="get_off_job",
  134. status="fail",
  135. message="get_off_job下架单个视频失败,video_id={}, 报错信息={}".format(video_id, e),
  136. )
  137. def check_job(self):
  138. """
  139. 校验 3 天前发布的视频是否已经下架
  140. :return:
  141. """
  142. three_days_ago = int(time.time()) - EXPIRE_TIME
  143. sql = f"""
  144. SELECT video_id
  145. FROM get_off_videos
  146. WHERE publish_time < {three_days_ago}
  147. AND video_status = 1;
  148. """
  149. vid_tuple = self.db_client.select(sql)
  150. if vid_tuple:
  151. vid_list = [i[0] for i in vid_tuple]
  152. for vid in vid_list:
  153. try:
  154. self.change_video_id_status(video_id=vid)
  155. except Exception as e:
  156. log(
  157. task="getOffVideosDaily",
  158. function="check_job",
  159. status="fail",
  160. message="task2下架单个视频失败,video_id={}, 报错信息={}".format(vid, e),
  161. )
  162. time.sleep(10)
  163. vid_tuple2 = self.db_client.select(sql)
  164. if vid_tuple2:
  165. vid_list2 = [i[0] for i in vid_tuple2]
  166. bot(
  167. title="getOffVideosDaily_check_job",
  168. detail={
  169. "video_list": vid_list2
  170. }
  171. )
  172. else:
  173. return
  174. else:
  175. return
  176. def main():
  177. """
  178. main function
  179. :return:
  180. """
  181. auto_get_off_job = AutoGetOffVideos()
  182. if auto_get_off_job.base_init():
  183. auto_get_off_job.get_off_job()
  184. time.sleep(60)
  185. auto_get_off_job.check_job()
  186. bot(
  187. title="get_off_jobs任务执行完成通知",
  188. mention=False,
  189. detail={
  190. "finish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  191. }
  192. )
  193. if __name__ == '__main__':
  194. main()