getOffVideosDaily.py 5.8 KB

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