getOffVideosDaily.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. """
  2. @author: luojunhui
  3. @description: GetOffVideos Daily
  4. """
  5. import json
  6. import time
  7. import requests
  8. import schedule
  9. from tqdm import tqdm
  10. from applications import PQMySQL, Functions, log
  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. log(
  62. task="getOffVideosDaily",
  63. function="getLongArticlesVideos",
  64. message="查找到视频 id_list,一共{}条视频".format(len(result))
  65. )
  66. return result
  67. @classmethod
  68. def updateVideoIdStatus(cls, video_id):
  69. """
  70. 修改数据库内视频状态
  71. :param video_id:
  72. :return:
  73. """
  74. time_stamp = int(time.time())
  75. select_sql = f"""
  76. UPDATE get_off_videos
  77. SET video_status = 0, get_off_time = {time_stamp}
  78. WHERE video_id = %s;
  79. """
  80. try:
  81. cls.pqMysql.update(
  82. sql=select_sql,
  83. params=video_id
  84. )
  85. log(
  86. task="getOffVideosDaily",
  87. function="updateVideoIdStatus",
  88. message="成功修改视频状态",
  89. data={"video_id": video_id}
  90. )
  91. except Exception as e:
  92. log(
  93. task="getOffVideosDaily",
  94. function="updateVideoIdStatus",
  95. message="修改视频状态失败--- 推测 sql 问题,报错信息:{}".format(e),
  96. status="fail"
  97. )
  98. @classmethod
  99. def changeVideoIdStatus(cls, video_id):
  100. """
  101. 修改视频规则
  102. :return:
  103. """
  104. url = "https://admin.piaoquantv.com/manager/video/audit/v2/updateAuditStatus"
  105. 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(
  106. video_id)
  107. headers = {
  108. 'accept': 'application/json',
  109. 'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8',
  110. 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  111. 'cookie': 'SESSION=NTljNTg2YjktMTU0MC00YWQ5LWE4ZTktNDFhODY0NzM3NTcx',
  112. 'origin': 'https://admin.piaoquantv.com',
  113. 'priority': 'u=1, i',
  114. 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
  115. 'sec-ch-ua-mobile': '?0',
  116. 'sec-ch-ua-platform': '"macOS"',
  117. 'sec-fetch-dest': 'empty',
  118. 'sec-fetch-mode': 'cors',
  119. 'sec-fetch-site': 'same-origin',
  120. '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'
  121. }
  122. response = requests.request(
  123. "POST",
  124. url,
  125. headers=headers,
  126. data=payload,
  127. timeout=10
  128. )
  129. if response.status_code == 200:
  130. result = response.json()
  131. if result.get("code", None) == 0:
  132. cls.updateVideoIdStatus(video_id=video_id)
  133. else:
  134. log(
  135. task="getOffVideosDaily",
  136. function="changeVideoIdStatus",
  137. message="请求票圈修改状态异常---video_id = {}".format(video_id),
  138. data=result
  139. )
  140. else:
  141. log(
  142. task="getOffVideosDaily",
  143. function="changeVideoIdStatus",
  144. status="fail",
  145. message="请求票圈修改状态异常,状态码非 200 ---video_id = {}".format(video_id),
  146. )
  147. @classmethod
  148. def task1(cls):
  149. """
  150. 已经请求超过3天的视频全部下架
  151. :return:
  152. """
  153. now_stamp = int(time.time())
  154. three_days_before = now_stamp - 3 * 24 * 60 * 60
  155. video_set = cls.getLongArticlesVideos(time_stamp=three_days_before)
  156. vid_list = [i[0] for i in video_set]
  157. for video_id in tqdm(vid_list):
  158. try:
  159. cls.changeVideoIdStatus(video_id=video_id)
  160. except Exception as e:
  161. log(
  162. task="getOffVideosDaily",
  163. function="task1",
  164. status="fail",
  165. message="task1下架单个视频失败,video_id={}, 报错信息={}".format(video_id, e),
  166. )
  167. @classmethod
  168. def task2(cls):
  169. """
  170. 校验 3 天前发布的视频是否已经下架
  171. :return:
  172. """
  173. three_days_ago = int(time.time()) - 3 * 24 * 3600
  174. sql = f"""
  175. SELECT video_id
  176. FROM get_off_videos
  177. WHERE publish_time < {three_days_ago} and video_status = 1;
  178. """
  179. vid_tuple = cls.pqMysql.select(sql)
  180. if vid_tuple:
  181. vid_list = [i[0] for i in vid_tuple]
  182. for vid in vid_list:
  183. try:
  184. cls.changeVideoIdStatus(video_id=vid)
  185. except Exception as e:
  186. log(
  187. task="getOffVideosDaily",
  188. function="task2",
  189. status="fail",
  190. message="task2下架单个视频失败,video_id={}, 报错信息={}".format(vid, e),
  191. )
  192. time.sleep(10)
  193. vid_tuple2 = cls.pqMysql.select(sql)
  194. if vid_tuple2:
  195. vid_list2 = [i[0] for i in vid_tuple2]
  196. bot(vid_list2)
  197. else:
  198. return
  199. else:
  200. return
  201. def getOffJob():
  202. """
  203. 下架任务
  204. :return:
  205. """
  206. AGV = AutoGetOffVideos()
  207. AGV.task1()
  208. def checkJob():
  209. """
  210. 校验 3 天前发布的视频是否已经被下架
  211. :return:
  212. """
  213. AGV = AutoGetOffVideos()
  214. AGV.task2()
  215. if __name__ == '__main__':
  216. getOffJob()
  217. schedule.every().day.at("09:30").do(Functions().job_with_thread, getOffJob)
  218. schedule.every().day.at("10:30").do(Functions().job_with_thread, checkJob)
  219. schedule.every().day.at("14:30").do(Functions().job_with_thread, getOffJob)
  220. schedule.every().day.at("15:00").do(Functions().job_with_thread, checkJob)
  221. while True:
  222. schedule.run_pending()
  223. time.sleep(1)
  224. log(
  225. task="getOffVideos",
  226. function="main",
  227. message="自动下架视频任务正常执行"
  228. )