auto_getoff_videos.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 video_id
  31. FROM article_match_videos
  32. WHERE video_status = 1 and request_time < %s;
  33. """
  34. cursor = spider_connection.cursor()
  35. cursor.execute(select_sql, time_stamp)
  36. data = cursor.fetchall()
  37. return data
  38. @classmethod
  39. def update_mysql_status(cls, video_id):
  40. """
  41. 修改数据库内视频状态
  42. :param video_id:
  43. :return:
  44. """
  45. spider_connection = pymysql.connect(
  46. host="rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com",
  47. port=3306,
  48. user="crawler",
  49. passwd="crawler123456@",
  50. db="piaoquan-crawler",
  51. charset="utf8mb4"
  52. )
  53. select_sql = f"""
  54. UPDATE article_match_videos
  55. SET video_status = 0
  56. WHERE video_id = %s;
  57. """
  58. cursor = spider_connection.cursor()
  59. cursor.execute(select_sql, video_id)
  60. spider_connection.commit()
  61. @classmethod
  62. def check_video_by_id(cls, video_id):
  63. """
  64. 通过视频id来判断首次播放时间和现在时间戳大于3天的视频
  65. :param video_id:
  66. :return:
  67. """
  68. end_time = int(time.time())
  69. start_time = end_time - 7 * 24 * 3600
  70. res = read_log(
  71. start_time=start_time, end_time=end_time, query='businessType : videoPlay and videoId: {}'.format(video_id)
  72. )
  73. if res:
  74. cls.change_status(video_id=video_id)
  75. else:
  76. print("No need to change")
  77. @classmethod
  78. def change_status(cls, video_id):
  79. """
  80. 修改视频规则
  81. :return:
  82. """
  83. cls.update_mysql_status(video_id=video_id)
  84. path = "./static/{}.json".format(video_id)
  85. if os.path.exists(path):
  86. print("File already Exists")
  87. else:
  88. url = "https://admin.piaoquantv.com/manager/video/audit/v2/updateAuditStatus"
  89. 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(
  90. video_id)
  91. headers = {
  92. 'accept': 'application/json',
  93. 'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8',
  94. 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  95. 'cookie': 'SESSION=NTljNTg2YjktMTU0MC00YWQ5LWE4ZTktNDFhODY0NzM3NTcx',
  96. 'origin': 'https://admin.piaoquantv.com',
  97. 'priority': 'u=1, i',
  98. 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
  99. 'sec-ch-ua-mobile': '?0',
  100. 'sec-ch-ua-platform': '"macOS"',
  101. 'sec-fetch-dest': 'empty',
  102. 'sec-fetch-mode': 'cors',
  103. 'sec-fetch-site': 'same-origin',
  104. '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'
  105. }
  106. response = requests.request(
  107. "POST",
  108. url,
  109. headers=headers,
  110. data=payload
  111. )
  112. # print(response.json())
  113. if response.json()['code'] == 0:
  114. with open(path, "w") as f:
  115. f.write(json.dumps({"time": int(time.time())}))
  116. else:
  117. print("失败的请求")
  118. @classmethod
  119. def task2(cls):
  120. """
  121. main function process
  122. :return:
  123. """
  124. now_time_stamp = int(time.time())
  125. three_days_before = now_time_stamp - 4 * 24 * 3600
  126. vid_set = cls.get_long_articles_video_set(time_stamp=three_days_before)
  127. vid_list = [i[0] for i in vid_set]
  128. with ThreadPoolExecutor(max_workers=8) as Pool:
  129. Pool.map(cls.check_video_by_id, vid_list)
  130. @classmethod
  131. def task1(cls):
  132. """
  133. 已经请求超过7天的视频全部下架
  134. :return:
  135. """
  136. now_stamp = int(time.time())
  137. seven_days_before = now_stamp - 8 * 24 * 3600
  138. video_set = cls.get_long_articles_video_set(time_stamp=seven_days_before)
  139. vid_list = [i[0] for i in video_set]
  140. with ThreadPoolExecutor(max_workers=8) as Pool1:
  141. Pool1.map(cls.change_status, vid_list)