auto_getoff_videos.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import os.path
  6. import time
  7. from concurrent.futures.thread import ThreadPoolExecutor
  8. import requests
  9. import pymysql
  10. from applications.aliyunLog import read_log
  11. class AutoGetOffVideos(object):
  12. """
  13. 自动下架视频
  14. """
  15. @classmethod
  16. def get_long_articles_video_set(cls, time_stamp):
  17. """
  18. 获取长文视频list
  19. :return:
  20. """
  21. spider_connection = pymysql.connect(
  22. host="rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com",
  23. port=3306,
  24. user="crawler",
  25. passwd="crawler123456@",
  26. db="piaoquan-crawler",
  27. charset="utf8mb4"
  28. )
  29. select_sql = f"""
  30. SELECT recall_video_id1, recall_video_id2, recall_video_id3
  31. FROM long_articles_video
  32. WHERE content_status = %s and request_time_stamp < %s;
  33. """
  34. cursor = spider_connection.cursor()
  35. cursor.execute(select_sql, (2, time_stamp))
  36. data = cursor.fetchall()
  37. vid_set = set()
  38. for item in data:
  39. for vid in item:
  40. if vid:
  41. vid_set.add(vid)
  42. return tuple(vid_set)
  43. @classmethod
  44. def check_video_by_id(cls, video_id):
  45. """
  46. 通过视频id来判断首次播放时间和现在时间戳大于3天的视频
  47. :param video_id:
  48. :return:
  49. """
  50. end_time = int(time.time())
  51. start_time = end_time - 7 * 24 * 3600
  52. res = read_log(
  53. start_time=start_time, end_time=end_time, query='businessType : videoPlay and videoId: {}'.format(video_id)
  54. )
  55. if res:
  56. cls.change_status(video_id=video_id)
  57. else:
  58. print("No need to change")
  59. @classmethod
  60. def change_status(cls, video_id):
  61. """
  62. 修改视频规则
  63. :return:
  64. """
  65. path = "./static/{}.json".format(video_id)
  66. if os.path.exists(path):
  67. print("File already Exists")
  68. else:
  69. url = "https://admin.piaoquantv.com/manager/video/audit/v2/updateAuditStatus"
  70. 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(video_id)
  71. headers = {
  72. 'accept': 'application/json',
  73. 'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8',
  74. 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  75. 'cookie': 'SESSION=NTljNTg2YjktMTU0MC00YWQ5LWE4ZTktNDFhODY0NzM3NTcx',
  76. 'origin': 'https://admin.piaoquantv.com',
  77. 'priority': 'u=1, i',
  78. 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
  79. 'sec-ch-ua-mobile': '?0',
  80. 'sec-ch-ua-platform': '"macOS"',
  81. 'sec-fetch-dest': 'empty',
  82. 'sec-fetch-mode': 'cors',
  83. 'sec-fetch-site': 'same-origin',
  84. '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'
  85. }
  86. response = requests.request(
  87. "POST",
  88. url,
  89. headers=headers,
  90. data=payload
  91. )
  92. if response.json()['code'] == 0:
  93. with open(path, "w") as f:
  94. f.write(json.dumps({"time": int(time.time())}))
  95. else:
  96. print("失败的请求")
  97. @classmethod
  98. def task2(cls):
  99. """
  100. main function process
  101. :return:
  102. """
  103. now_time_stamp = int(time.time())
  104. three_days_before = now_time_stamp - 4 * 24 * 3600
  105. vid_set = cls.get_long_articles_video_set(time_stamp=three_days_before)
  106. with ThreadPoolExecutor(max_workers=8) as Pool:
  107. Pool.map(cls.check_video_by_id, list(vid_set))
  108. @classmethod
  109. def task1(cls):
  110. """
  111. 已经请求超过7天的视频全部下架
  112. :return:
  113. """
  114. now_stamp = int(time.time())
  115. seven_days_before = now_stamp - 8 * 24 * 3600
  116. video_set = cls.get_long_articles_video_set(time_stamp=seven_days_before)
  117. with ThreadPoolExecutor(max_workers=8) as Pool1:
  118. Pool1.map(cls.change_status, list(video_set))