123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- # -*- coding: utf-8 -*-
- # @Author: wangkun
- # @Time: 2022/4/18
- """
- 下载并上传:发布时间榜
- 规则:
- 1.基本规则:send_time_rule()
- 2.视频发布3日内,播放量大于2万(当前时间 - 发布时间 <= 3 天)
- """
- import json
- import os
- import sys
- import time
- import requests
- import urllib3
- sys.path.append(os.getcwd())
- from main.common import Common
- from main.get_feeds import get_feeds
- from main.publish import Publish
- from main.feishu_lib import Feishu
- proxies = {"http": None, "https": None}
- class DownloadSendtime:
- @staticmethod
- def send_time_rule(send_time_width, send_time_height, send_time_duration, send_time_share_cnt):
- """
- 1.分辨率,宽或者高 >= 720 or == 0
- 2.600s >= 时长 >= 60s
- 3.视频播放量 >= 0
- """
- if int(send_time_width) >= 720 or int(send_time_height) >= 720 \
- or send_time_width == "0" or send_time_height == "0":
- if 600 >= int(send_time_duration) >= 60:
- if int(send_time_share_cnt) > 0:
- return True
- else:
- return False
- else:
- return False
- else:
- return False
- @classmethod
- def download_sendtime_video(cls, env):
- """
- 视频发布3日内,播放量大于2万(当前时间 - 发布时间 <= 3 天)
- :param env: 测试环境:dev;正式环境:prod
- :return: 下载并上传视频
- """
- try:
- if len(Feishu.get_values_batch("SdCHOM")) == 1:
- pass
- else:
- for i in range(len(Feishu.get_values_batch("SdCHOM"))):
- time.sleep(1)
- try:
- sendtime_session = Common.get_session()
- Common.logger().info("获取视频info时,session:{}", sendtime_session)
- download_video_id = Feishu.get_values_batch("SdCHOM")[i+1][1]
- download_video_title = Feishu.get_values_batch("SdCHOM")[i+1][3]
- url = "https://search.weixin.qq.com/cgi-bin/recwxa/recwxagetonevideoinfo?"
- param = {
- "session": sendtime_session,
- "vid": download_video_id,
- "wxaVersion": "3.9.2",
- "channelid": "208201",
- "scene": "32",
- "subscene": "1089",
- "model": "iPhone 11<iPhone12,1>14.7.1",
- "clientVersion": "8.0.18",
- "sharesearchid": "447665862521758270",
- "sharesource": "-1"
- }
- urllib3.disable_warnings()
- r = requests.get(url=url, params=param, proxies=proxies, verify=False)
- response = json.loads(r.content.decode("utf8"))
- if "data" not in response:
- Common.logger().error("获取视频info时错误,删除该视频:{}", download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
- else:
- data = response["data"]
- v_duration = data["duration"]
- v_play_cnt_sendtime = data["played_cnt"]
- v_comment_cnt = data["comment_cnt"]
- v_liked_cnt = data["liked_cnt"]
- v_shared_cnt = data["shared_cnt"]
- v_width = data["width"]
- v_height = data["height"]
- v_resolution = str(v_width) + "*" + str(v_height)
- v_send_date = data["upload_time"]
- v_username = data["user_info"]["nickname"].strip().replace("\n", "")
- v_user_cover = data["user_info"]["headimg_url"]
- v_video_cover = data["cover_url"]
- if "items" not in data["play_info"]:
- if len(data["play_info"]) > 2:
- download_url_sendtime = data["play_info"][2]["play_url"]
- else:
- download_url_sendtime = data["play_info"][0]["play_url"]
- else:
- if len(data["play_info"]["items"]) > 2:
- download_url_sendtime = data["play_info"]["items"][2]["play_url"]
- else:
- download_url_sendtime = data["play_info"]["items"][0]["play_url"]
- # 判断基本规则
- if download_video_id not in [j for i in Feishu.get_values_batch("20ce0c") for j in i]\
- and cls.send_time_rule(v_width, v_height, v_duration, v_play_cnt_sendtime) is True \
- and download_video_id != "" and download_video_title != "" and v_duration != "" \
- and v_play_cnt_sendtime != "" and v_comment_cnt != "" and v_liked_cnt != "" \
- and v_shared_cnt != "" and v_width != "" and v_height != "" \
- and v_send_date != "" and v_username != "" and v_user_cover != "" \
- and v_video_cover != "" and download_url_sendtime != "":
- # 满足下载条件:当前时间 - 发布时间 <= 3天,播放量大于1万
- if int(time.time()) - int(v_send_date) <= 604800:
- if int(v_play_cnt_sendtime) >= 10000:
- Common.logger().info("该视频:{} ,在7天内的播放量{}>=10000",
- download_video_title, v_play_cnt_sendtime)
- # 下载封面
- Common.download_method("cover", download_video_title, v_video_cover)
- # 下载视频
- Common.download_method("video", download_video_title, download_url_sendtime)
- # 保存视频信息到 "./files/{视频标题}/videoinfo.txt"
- with open(r"./videos/" + download_video_title +
- "/" + "info.txt", "a", encoding="utf8") as f_a2:
- f_a2.write(str(download_video_id) + "\n" +
- str(download_video_title) + "\n" +
- str(v_duration) + "\n" +
- str(v_play_cnt_sendtime) + "\n" +
- str(v_comment_cnt) + "\n" +
- str(v_liked_cnt) + "\n" +
- str(v_shared_cnt) + "\n" +
- str(v_resolution) + "\n" +
- str(v_send_date) + "\n" +
- str(v_username) + "\n" +
- str(v_user_cover) + "\n" +
- str(download_url_sendtime) + "\n" +
- str(v_video_cover) + "\n" +
- str(sendtime_session))
- Common.logger().info("==========视频信息已保存至info.txt==========")
- # 上传该视频
- Common.logger().info("开始上传视频:{}", download_video_title)
- Publish.upload_and_publish(env, "send_time")
- # 保存视频 ID 到云文档:
- # https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=20ce0c
- Common.logger().info("保存视频ID至云文档:{}", download_video_title)
- # 看一看+ ,视频ID工作表,插入首行
- Feishu.insert_columns("20ce0c")
- # 看一看+ ,视频ID工作表,首行写入数据
- upload_time = int(time.time())
- Feishu.update_values("20ce0c",
- str(time.strftime("%Y-%m-%d %H:%M:%S",
- time.localtime(upload_time))),
- str(download_video_id),
- str(v_play_cnt_sendtime),
- str(download_video_title),
- str(v_duration),
- str(v_comment_cnt),
- str(v_liked_cnt),
- str(v_shared_cnt),
- str(v_resolution),
- str(time.strftime("%Y-%m-%d %H:%M:%S",
- time.localtime(int(v_send_date)))),
- str(v_username),
- str(v_user_cover),
- str(v_video_cover),
- str(download_url_sendtime),
- str(sendtime_session))
- # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
- Common.logger().info("从云文档删除该视频信息:{}", download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
- else:
- # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
- Common.logger().info("该视频7天播放量:{}<10000 ;不满足下载规则:{}",
- int(v_play_cnt_sendtime), download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
- else:
- # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
- Common.logger().info("视频发布时间大于7天:{}天;标题:{}",
- int((int(time.time()) - int(v_send_date)) / 86400),
- download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
- else:
- # 从云文档删除该视频信息:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
- Common.logger().info("不满足下载规则:{}", download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
- except Exception as e:
- Common.logger().error("获取视频info异常:{},删除该视频", e)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("SdCHOM", "ROWS", i + 2, i + 2)
- cls.download_sendtime_video("prod")
- except Exception as e:
- Common.logger().error(e)
- if __name__ == "__main__":
- download_sendtime = DownloadSendtime()
- get_feeds()
- download_sendtime.download_sendtime_video("dev")
|