getOffVideosDaily.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """
  2. @author: luojunhui
  3. @description: GetOffVideos Daily
  4. """
  5. import json
  6. import time
  7. from concurrent.futures.thread import ThreadPoolExecutor
  8. import requests
  9. import schedule
  10. from tqdm import tqdm
  11. from applications import PQMySQL, Functions
  12. from applications.decoratorApi import retryOnTimeout
  13. @retryOnTimeout()
  14. def bot(account_list):
  15. """
  16. 机器人
  17. """
  18. url = "https://open.feishu.cn/open-apis/bot/v2/hook/b44333f2-16c0-4cb1-af01-d135f8704410"
  19. headers = {"Content-Type": "application/json"}
  20. payload = {
  21. "msg_type": "interactive",
  22. "card": {
  23. "elements": [
  24. {
  25. "tag": "div",
  26. "text": {
  27. "content": "存在视频下架失败<at id=all></at>\n",
  28. "tag": "lark_md",
  29. },
  30. },
  31. {
  32. "tag": "div",
  33. "text": {
  34. "content": json.dumps(
  35. account_list, ensure_ascii=False, indent=4
  36. ),
  37. "tag": "lark_md",
  38. },
  39. },
  40. ],
  41. "header": {"title": {"content": "【重点关注】", "tag": "plain_text"}},
  42. },
  43. }
  44. requests.request("POST", url=url, headers=headers, data=json.dumps(payload), timeout=10)
  45. class AutoGetOffVideos(object):
  46. """
  47. 自动下架视频
  48. """
  49. pqMysql = PQMySQL()
  50. @classmethod
  51. def getLongArticlesVideos(cls, time_stamp):
  52. """
  53. 获取待下架的视频
  54. :return:
  55. """
  56. select_sql = f"""
  57. SELECT video_id
  58. FROM get_off_videos
  59. WHERE video_status = 1 and publish_time < {time_stamp};
  60. """
  61. result = cls.pqMysql.select(sql=select_sql)
  62. return result
  63. @classmethod
  64. def updateVideoIdStatus(cls, video_id):
  65. """
  66. 修改数据库内视频状态
  67. :param video_id:
  68. :return:
  69. """
  70. time_stamp = int(time.time())
  71. select_sql = f"""
  72. UPDATE get_off_videos
  73. SET video_status = 0, get_off_time = {time_stamp}
  74. WHERE video_id = %s;
  75. """
  76. cls.pqMysql.update(
  77. sql=select_sql,
  78. params=video_id
  79. )
  80. print("更新成功")
  81. @classmethod
  82. def changeVideoIdStatus(cls, video_id):
  83. """
  84. 修改视频规则
  85. :return:
  86. """
  87. url = "https://admin.piaoquantv.com/manager/video/audit/v2/updateAuditStatus"
  88. payload = "videoId={}&auditStatus=2&updateReasonJson=&rejectReasonJson=%5B%7B%22reason%22%3A%22%E9%95%BF%E6%96%87%E8%87%AA%E5%8A%A8%E4%B8%8B%E6%9E%B6%22%2C%22reasonId%22%3A-1%7D%5D&adminUid=206".format(
  89. video_id)
  90. headers = {
  91. 'accept': 'application/json',
  92. 'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8',
  93. 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  94. 'cookie': 'SESSION=NTljNTg2YjktMTU0MC00YWQ5LWE4ZTktNDFhODY0NzM3NTcx',
  95. 'origin': 'https://admin.piaoquantv.com',
  96. 'priority': 'u=1, i',
  97. 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
  98. 'sec-ch-ua-mobile': '?0',
  99. 'sec-ch-ua-platform': '"macOS"',
  100. 'sec-fetch-dest': 'empty',
  101. 'sec-fetch-mode': 'cors',
  102. 'sec-fetch-site': 'same-origin',
  103. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
  104. }
  105. response = requests.request(
  106. "POST",
  107. url,
  108. headers=headers,
  109. data=payload
  110. )
  111. if response.status_code == 200:
  112. result = response.json()
  113. if result.get("code", None) == 0:
  114. cls.updateVideoIdStatus(video_id=video_id)
  115. @classmethod
  116. def task1(cls):
  117. """
  118. 已经请求超过3天的视频全部下架
  119. :return:
  120. """
  121. now_stamp = int(time.time())
  122. three_days_before = now_stamp - 3 * 24 * 60 * 60
  123. video_set = cls.getLongArticlesVideos(time_stamp=three_days_before)
  124. vid_list = [i[0] for i in video_set]
  125. for video_id in tqdm(vid_list):
  126. cls.changeVideoIdStatus(video_id=video_id)
  127. # with ThreadPoolExecutor(max_workers=8) as Pool1:
  128. # Pool1.map(cls.changeVideoIdStatus, vid_list)
  129. @classmethod
  130. def task2(cls):
  131. """
  132. 校验 3 天前发布的视频是否已经下架
  133. :return:
  134. """
  135. three_days_ago = int(time.time()) - 3 * 24 * 3600
  136. sql = f"""
  137. SELECT video_id
  138. FROM get_off_videos
  139. WHERE publish_time < {three_days_ago} and video_status = 1;
  140. """
  141. vid_tuple = cls.pqMysql.select(sql)
  142. if vid_tuple:
  143. vid_list = [i[0] for i in vid_tuple]
  144. for vid in vid_list:
  145. cls.changeVideoIdStatus(video_id=vid)
  146. time.sleep(10)
  147. vid_tuple2 = cls.pqMysql.select(sql)
  148. if vid_tuple2:
  149. vid_list2 = [i[0] for i in vid_tuple2]
  150. bot(vid_list2)
  151. else:
  152. return
  153. else:
  154. return
  155. def getOffJob():
  156. """
  157. 下架任务
  158. :return:
  159. """
  160. AGV = AutoGetOffVideos()
  161. AGV.task1()
  162. def checkJob():
  163. """
  164. 校验 3 天前发布的视频是否已经被下架
  165. :return:
  166. """
  167. AGV = AutoGetOffVideos()
  168. AGV.task2()
  169. if __name__ == '__main__':
  170. schedule.every().day.at("09:30").do(Functions().job_with_thread, getOffJob)
  171. schedule.every().day.at("10:30").do(Functions().job_with_thread, checkJob)
  172. schedule.every().day.at("14:30").do(Functions().job_with_thread, getOffJob)
  173. schedule.every().day.at("15:00").do(Functions().job_with_thread, checkJob)
  174. while True:
  175. schedule.run_pending()
  176. time.sleep(1)
  177. print("自动下架视频任务正常执行")