auto_getoff_videos.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 getLongArticlesVideos(cls, time_stamp):
  17. """
  18. 获取待下架的视频
  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 get_off_videos
  32. WHERE video_status = 1 and publish_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 updateVideoIdstatus(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 get_off_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 changeStatus(cls, video_id):
  63. """
  64. 修改视频规则
  65. :return:
  66. """
  67. cls.updateVideoIdstatus(video_id=video_id)
  68. path = "./static/{}.json".format(video_id)
  69. if os.path.exists(path):
  70. print("File already Exists")
  71. else:
  72. url = "https://admin.piaoquantv.com/manager/video/audit/v2/updateAuditStatus"
  73. 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(
  74. video_id)
  75. headers = {
  76. 'accept': 'application/json',
  77. 'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8',
  78. 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  79. 'cookie': 'SESSION=NTljNTg2YjktMTU0MC00YWQ5LWE4ZTktNDFhODY0NzM3NTcx',
  80. 'origin': 'https://admin.piaoquantv.com',
  81. 'priority': 'u=1, i',
  82. 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
  83. 'sec-ch-ua-mobile': '?0',
  84. 'sec-ch-ua-platform': '"macOS"',
  85. 'sec-fetch-dest': 'empty',
  86. 'sec-fetch-mode': 'cors',
  87. 'sec-fetch-site': 'same-origin',
  88. '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'
  89. }
  90. response = requests.request(
  91. "POST",
  92. url,
  93. headers=headers,
  94. data=payload
  95. )
  96. if response.json()['code'] == 0:
  97. with open(path, "w") as f:
  98. f.write(json.dumps({"time": int(time.time())}))
  99. else:
  100. print("失败的请求")
  101. @classmethod
  102. def task1(cls):
  103. """
  104. 已经请求超过3天的视频全部下架
  105. :return:
  106. """
  107. now_stamp = int(time.time())
  108. seven_days_before = now_stamp - 3 * 24 * 60 * 60
  109. video_set = cls.getLongArticlesVideos(time_stamp=seven_days_before)
  110. vid_list = [i[0] for i in video_set]
  111. with ThreadPoolExecutor(max_workers=8) as Pool1:
  112. Pool1.map(cls.changeStatus, vid_list)