download_sendtime.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/4/18
  4. """
  5. 下载并上传:发布时间榜
  6. 规则:
  7. 1.基本规则:send_time_rule()
  8. 2.视频发布3日内,播放量大于2万(当前时间 - 发布时间 <= 3 天)
  9. """
  10. import json
  11. import os
  12. import random
  13. import sys
  14. import time
  15. import requests
  16. import urllib3
  17. sys.path.append(os.getcwd())
  18. from main.common import Common
  19. from main.get_feeds import get_feeds
  20. from main.publish import Publish
  21. proxies = {"http": None, "https": None}
  22. class DownloadSendtime:
  23. @staticmethod
  24. def send_time_rule(send_time_width, send_time_height, send_time_duration, send_time_share_cnt):
  25. """
  26. 1.分辨率,宽或者高 >= 720 or == 0
  27. 2.600s >= 时长 >= 60s
  28. 3.视频播放量 >= 0
  29. """
  30. if int(send_time_width) >= 720 or int(send_time_height) >= 720 \
  31. or send_time_width == "0" or send_time_height == "0":
  32. if 600 >= int(send_time_duration) >= 60:
  33. if int(send_time_share_cnt) > 0:
  34. return True
  35. else:
  36. return False
  37. else:
  38. return False
  39. else:
  40. return False
  41. @classmethod
  42. def download_sendtime_video(cls, env):
  43. """
  44. 视频发布3日内,播放量大于2万(当前时间 - 发布时间 <= 3 天)
  45. :param env: 测试环境:dev;正式环境:prod
  46. :return: 下载并上传视频
  47. """
  48. get_sendtime_session = Common.get_session()
  49. Common.crawler_log().info("获取视频info时,session:{}".format(get_sendtime_session))
  50. lines = Common.read_txt("kanyikan_feeds.txt")
  51. for line in lines:
  52. v_id = line.strip().split(" + ")[1] # 视频外网 ID
  53. # v_send_date = line.strip().split(" + ")[9] # 发布时间
  54. url = "https://search.weixin.qq.com/cgi-bin/recwxa/recwxagetonevideoinfo?"
  55. param = {
  56. "session": get_sendtime_session,
  57. "vid": v_id,
  58. "wxaVersion": "3.9.2",
  59. "channelid": "208201",
  60. "scene": "32",
  61. "subscene": "1089",
  62. "model": "iPhone 11<iPhone12,1>14.7.1",
  63. "clientVersion": "8.0.18",
  64. "sharesearchid": "447665862521758270",
  65. "sharesource": "-1"
  66. }
  67. try:
  68. urllib3.disable_warnings()
  69. r = requests.get(url=url, params=param, proxies=proxies, verify=False)
  70. response = json.loads(r.content.decode("utf8"))
  71. if "data" not in response:
  72. Common.crawler_log().info("获取视频info时,session过期,等待 30 秒")
  73. # 如果返回空信息,则随机睡眠 30-35 秒
  74. time.sleep(random.randint(31, 35))
  75. else:
  76. data = response["data"]
  77. v_title = data["title"]
  78. v_duration = data["duration"]
  79. v_play_cnt_sendtime = data["played_cnt"]
  80. v_comment_cnt = data["comment_cnt"]
  81. v_liked_cnt = data["liked_cnt"]
  82. v_shared_cnt = data["shared_cnt"]
  83. v_width = data["width"]
  84. v_height = data["height"]
  85. v_resolution = str(v_width) + "*" + str(v_height)
  86. v_send_date = data["upload_time"]
  87. v_username = data["user_info"]["nickname"]
  88. v_user_cover = data["user_info"]["headimg_url"]
  89. v_video_cover = data["cover_url"]
  90. if "items" not in data["play_info"]:
  91. if len(data["play_info"]) > 2:
  92. download_url_up = data["play_info"][2]["play_url"]
  93. # Common.crawler_log().info('视频下载地址:{}'.format(download_url_up))
  94. else:
  95. download_url_up = data["play_info"][0]["play_url"]
  96. # Common.crawler_log().info('视频下载地址:{}'.format(download_url_up))
  97. else:
  98. if len(data["play_info"]["items"]) > 2:
  99. download_url_up = data["play_info"]["items"][2]["play_url"]
  100. # Common.crawler_log().info('视频下载地址:{}'.format(download_url_up))
  101. else:
  102. download_url_up = data["play_info"]["items"][0]["play_url"]
  103. # Common.crawler_log().info('视频下载地址:{}'.format(download_url_up))
  104. # 判断基本规则
  105. if cls.send_time_rule(v_width, v_height, v_duration, v_play_cnt_sendtime) is True \
  106. and v_id != "" and v_title != "" and v_duration != "" \
  107. and v_play_cnt_sendtime != "" and v_comment_cnt != "" and v_liked_cnt != "" \
  108. and v_shared_cnt != "" and v_width != "" and v_height != "" \
  109. and v_send_date != "" and v_username != "" and v_user_cover != "" \
  110. and v_video_cover != "" and download_url_up != "":
  111. # 满足下载条件:当前时间 - 发布时间 <= 3天,播放量大于2万
  112. if int(time.time()) - int(v_send_date) <= 604800:
  113. if int(v_play_cnt_sendtime) >= 10000:
  114. Common.crawler_log().info("该视频:{}".format(
  115. v_title) + " " + "在7天内的播放量{}>=10000".format(v_play_cnt_sendtime))
  116. # 下载封面
  117. Common.download_method("cover", v_title, v_video_cover)
  118. # 下载视频
  119. Common.download_method("video", v_title, download_url_up)
  120. # 保存视频 ID 到 "./txt/kanyikan_videoid.txt"
  121. with open(r"./txt/kanyikan_videoid.txt", "a", encoding="utf8") as f_a:
  122. f_a.write(v_id + "\n")
  123. # 保存视频信息到 "./files/{视频标题}/videoinfo.txt"
  124. with open(r"./videos/" + v_title + "/" + "info.txt",
  125. "a", encoding="utf8") as f_a2:
  126. f_a2.write(str(v_id) + "\n" +
  127. str(v_title) + "\n" +
  128. str(v_duration) + "\n" +
  129. str(v_play_cnt_sendtime) + "\n" +
  130. str(v_comment_cnt) + "\n" +
  131. str(v_liked_cnt) + "\n" +
  132. str(v_shared_cnt) + "\n" +
  133. str(v_resolution) + "\n" +
  134. str(v_send_date) + "\n" +
  135. str(v_username) + "\n" +
  136. str(v_user_cover) + "\n" +
  137. str(download_url_up) + "\n" +
  138. str(v_video_cover) + "\n" +
  139. str(get_sendtime_session))
  140. # 上传该视频
  141. Common.crawler_log().info("开始上传视频:{}".format(v_title))
  142. Publish.upload_and_publish(env, "send_time")
  143. # 删除该视频在kanyikan_feeds.txt中的信息
  144. Common.crawler_log().info("删除该视频在kanyikan_feeds.txt中的信息:{}".format(v_title))
  145. with open(r"./txt/kanyikan_feeds.txt", "r", encoding="utf8") as f1:
  146. lines = f1.readlines()
  147. with open(r"./txt/kanyikan_feeds.txt", "w", encoding="utf-8") as f_w1:
  148. for line1 in lines:
  149. if v_id in line1.split(" + ")[1]:
  150. continue
  151. f_w1.write(line1)
  152. else:
  153. # 删除之前保存的该视频信息
  154. Common.crawler_log().info("该视频7天播放量:{}<10000".format(
  155. int(v_play_cnt_sendtime)) + ";" + "不满足下载规则:{}".format(v_title))
  156. with open(r"./txt/kanyikan_feeds.txt", "r", encoding="utf8") as f_r:
  157. lines = f_r.readlines()
  158. with open(r"./txt/kanyikan_feeds.txt", "w", encoding="utf-8") as f_w:
  159. for line2 in lines:
  160. if v_id in line2.split(" + ")[1]:
  161. continue
  162. f_w.write(line2)
  163. else:
  164. Common.crawler_log().info("视频发布时间大于7天:{}天".format(
  165. int((int(time.time()) - int(v_send_date)) / 86400))
  166. + ";" + "标题:{}".format(v_title))
  167. with open(r"./txt/kanyikan_feeds.txt", "r", encoding="utf8") as f_r:
  168. lines = f_r.readlines()
  169. with open(r"./txt/kanyikan_feeds.txt", "w", encoding="utf-8") as f_w:
  170. for line2 in lines:
  171. if v_id in line2.split(" + ")[1]:
  172. continue
  173. f_w.write(line2)
  174. else:
  175. Common.crawler_log().info("不满足下载规则:{}".format(v_title))
  176. with open(r"./txt/kanyikan_feeds.txt", "r", encoding="utf8") as f_r:
  177. lines = f_r.readlines()
  178. with open(r"./txt/kanyikan_feeds.txt", "w", encoding="utf-8") as f_w:
  179. for line3 in lines:
  180. if v_id in line3.split(" + ")[1]:
  181. continue
  182. f_w.write(line3)
  183. except Exception as e:
  184. Common.crawler_log().error("获取视频info异常:{},删除该视频".format(e))
  185. with open(r"./txt/kanyikan_feeds.txt", "r", encoding="utf8") as f_r:
  186. lines = f_r.readlines()
  187. with open(r"./txt/kanyikan_feeds.txt", "w", encoding="utf-8") as f_w:
  188. for line4 in lines:
  189. if v_id in line4.split(" + ")[1]:
  190. continue
  191. f_w.write(line4)
  192. if __name__ == "__main__":
  193. download_sendtime = DownloadSendtime()
  194. get_feeds()
  195. download_sendtime.download_sendtime_video("dev")