download_sendtime.py 11 KB

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