download_sendtime.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 sys
  13. import time
  14. import requests
  15. import urllib3
  16. sys.path.append(os.getcwd())
  17. from main.common import Common
  18. from main.get_feeds import get_feeds
  19. from main.publish import Publish
  20. from main.feishu_lib import Feishu
  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. try:
  49. if len(Feishu.get_values_batch("SdCHOM")) == 1:
  50. pass
  51. else:
  52. for i in range(len(Feishu.get_values_batch("SdCHOM"))):
  53. time.sleep(1)
  54. try:
  55. sendtime_session = Common.get_session()
  56. Common.logger().info("获取视频info时,session:{}", sendtime_session)
  57. download_video_id = Feishu.get_values_batch("SdCHOM")[i+1][1]
  58. download_video_title = Feishu.get_values_batch("SdCHOM")[i+1][3]
  59. url = "https://search.weixin.qq.com/cgi-bin/recwxa/recwxagetonevideoinfo?"
  60. param = {
  61. "session": sendtime_session,
  62. "vid": download_video_id,
  63. "wxaVersion": "3.9.2",
  64. "channelid": "208201",
  65. "scene": "32",
  66. "subscene": "1089",
  67. "model": "iPhone 11<iPhone12,1>14.7.1",
  68. "clientVersion": "8.0.18",
  69. "sharesearchid": "447665862521758270",
  70. "sharesource": "-1"
  71. }
  72. urllib3.disable_warnings()
  73. r = requests.get(url=url, params=param, proxies=proxies, verify=False)
  74. response = json.loads(r.content.decode("utf8"))
  75. if "data" not in response:
  76. Common.logger().error("获取视频info时错误,删除该视频:{}", download_video_title)
  77. # 删除行或列,可选 ROWS、COLUMNS
  78. Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
  79. else:
  80. data = response["data"]
  81. v_duration = data["duration"]
  82. v_play_cnt_sendtime = data["played_cnt"]
  83. v_comment_cnt = data["comment_cnt"]
  84. v_liked_cnt = data["liked_cnt"]
  85. v_shared_cnt = data["shared_cnt"]
  86. v_width = data["width"]
  87. v_height = data["height"]
  88. v_resolution = str(v_width) + "*" + str(v_height)
  89. v_send_date = data["upload_time"]
  90. v_username = data["user_info"]["nickname"].strip().replace("\n", "")
  91. v_user_cover = data["user_info"]["headimg_url"]
  92. v_video_cover = data["cover_url"]
  93. if "items" not in data["play_info"]:
  94. if len(data["play_info"]) > 2:
  95. download_url_sendtime = data["play_info"][2]["play_url"]
  96. else:
  97. download_url_sendtime = data["play_info"][0]["play_url"]
  98. else:
  99. if len(data["play_info"]["items"]) > 2:
  100. download_url_sendtime = data["play_info"]["items"][2]["play_url"]
  101. else:
  102. download_url_sendtime = data["play_info"]["items"][0]["play_url"]
  103. # 判断基本规则
  104. if download_video_id not in [j for i in Feishu.get_values_batch("20ce0c") for j in i]\
  105. and cls.send_time_rule(v_width, v_height, v_duration, v_play_cnt_sendtime) is True \
  106. and download_video_id != "" and download_video_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_sendtime != "":
  111. # 满足下载条件:当前时间 - 发布时间 <= 3天,播放量大于1万
  112. if int(time.time()) - int(v_send_date) <= 604800:
  113. if int(v_play_cnt_sendtime) >= 10000:
  114. Common.logger().info("该视频:{} ,在7天内的播放量{}>=10000",
  115. download_video_title, v_play_cnt_sendtime)
  116. # 下载封面
  117. Common.download_method("cover", download_video_title, v_video_cover)
  118. # 下载视频
  119. Common.download_method("video", download_video_title, download_url_sendtime)
  120. # 保存视频信息到 "./files/{视频标题}/videoinfo.txt"
  121. with open(r"./videos/" + download_video_title +
  122. "/" + "info.txt", "a", encoding="utf8") as f_a2:
  123. f_a2.write(str(download_video_id) + "\n" +
  124. str(download_video_title) + "\n" +
  125. str(v_duration) + "\n" +
  126. str(v_play_cnt_sendtime) + "\n" +
  127. str(v_comment_cnt) + "\n" +
  128. str(v_liked_cnt) + "\n" +
  129. str(v_shared_cnt) + "\n" +
  130. str(v_resolution) + "\n" +
  131. str(v_send_date) + "\n" +
  132. str(v_username) + "\n" +
  133. str(v_user_cover) + "\n" +
  134. str(download_url_sendtime) + "\n" +
  135. str(v_video_cover) + "\n" +
  136. str(sendtime_session))
  137. Common.logger().info("==========视频信息已保存至info.txt==========")
  138. # 上传该视频
  139. Common.logger().info("开始上传视频:{}", download_video_title)
  140. Publish.upload_and_publish(env, "send_time")
  141. # 保存视频 ID 到云文档:
  142. # https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=20ce0c
  143. Common.logger().info("保存视频ID至云文档:{}", download_video_title)
  144. # 看一看+ ,视频ID工作表,插入首行
  145. Feishu.insert_columns("20ce0c")
  146. # 看一看+ ,视频ID工作表,首行写入数据
  147. upload_time = int(time.time())
  148. Feishu.update_values("20ce0c",
  149. str(time.strftime("%Y-%m-%d %H:%M:%S",
  150. time.localtime(upload_time))),
  151. str(download_video_id),
  152. str(v_play_cnt_sendtime),
  153. str(download_video_title),
  154. str(v_duration),
  155. str(v_comment_cnt),
  156. str(v_liked_cnt),
  157. str(v_shared_cnt),
  158. str(v_resolution),
  159. str(time.strftime("%Y-%m-%d %H:%M:%S",
  160. time.localtime(int(v_send_date)))),
  161. str(v_username),
  162. str(v_user_cover),
  163. str(v_video_cover),
  164. str(download_url_sendtime),
  165. str(sendtime_session))
  166. # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
  167. Common.logger().info("从云文档删除该视频信息:{}", download_video_title)
  168. # 删除行或列,可选 ROWS、COLUMNS
  169. Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
  170. else:
  171. # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
  172. Common.logger().info("该视频7天播放量:{}<10000 ;不满足下载规则:{}",
  173. int(v_play_cnt_sendtime), download_video_title)
  174. # 删除行或列,可选 ROWS、COLUMNS
  175. Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
  176. else:
  177. # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
  178. Common.logger().info("视频发布时间大于7天:{}天;标题:{}",
  179. int((int(time.time()) - int(v_send_date)) / 86400),
  180. download_video_title)
  181. # 删除行或列,可选 ROWS、COLUMNS
  182. Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
  183. else:
  184. # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
  185. Common.logger().info("不满足下载规则:{}", download_video_title)
  186. # 删除行或列,可选 ROWS、COLUMNS
  187. Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
  188. except Exception as e:
  189. Common.logger().error("获取视频info异常:{},删除该视频", e)
  190. # 删除行或列,可选 ROWS、COLUMNS
  191. Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
  192. cls.download_sendtime_video("prod")
  193. except Exception as e:
  194. Common.logger().error(e)
  195. if __name__ == "__main__":
  196. download_sendtime = DownloadSendtime()
  197. get_feeds()
  198. download_sendtime.download_sendtime_video("dev")