123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- # -*- coding: utf-8 -*-
- # @Author: wangkun
- # @Time: 2023/6/21
- import os
- import random
- import sys
- import time
- import requests
- import urllib3
- sys.path.append(os.getcwd())
- proxies = {"http": None, "https": None}
- class Moment:
- # 过滤词库
- @classmethod
- def sensitive_words(cls):
- word_list = []
- # 从云文档读取所有敏感词,添加到词库列表
- lists = Feishu.get_values_batch("moment", "kanyikan", "rofdM5")
- for i in lists:
- for j in i:
- # 过滤空的单元格内容
- if j is None:
- pass
- else:
- word_list.append(j)
- return word_list
- # 朋友圈视频 ID
- @classmethod
- def moment_videoids(cls):
- try:
- videoid_list = []
- # 从云文档读取所有敏感词,添加到词库列表
- lists = Feishu.get_values_batch("moment", "kanyikan", "iK58HX")
- for i in lists:
- for j in i:
- # 过滤空的单元格内容
- if j is None:
- pass
- else:
- videoid_list.append(j)
- return videoid_list
- except Exception as e:
- Common.logger("moment").error("获取朋友圈视频ID异常:{}", e)
- return "t3256lo1cmk"
- # 抓取基础规则
- @staticmethod
- def download_rule(d_duration, d_width, d_height, d_play_cnt, d_like_cnt, d_share_cnt):
- """
- 抓取基础规则
- :param d_duration: 时长
- :param d_width: 宽
- :param d_height: 高
- :param d_play_cnt: 播放量
- :param d_like_cnt: 点赞量
- :param d_share_cnt: 分享量
- :return: 满足规则,返回 True;反之,返回 False
- """
- if int(float(d_duration)) >= 40:
- if int(d_width) >= 0 or int(d_height) >= 0:
- if int(d_play_cnt) >= 50000:
- if int(d_like_cnt) >= 0:
- if int(d_share_cnt) >= 0:
- return True
- else:
- return False
- else:
- return False
- else:
- return False
- return False
- return False
- # 获取推荐视频列表
- @classmethod
- def get_recommend(cls):
- url = "https://search.weixin.qq.com/cgi-bin/recwxa/snsgetvideoinfo?"
- headers = {
- "content-type": "application/json",
- "Accept-Encoding": "gzip,compress,br,deflate",
- "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)"
- " AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
- " MicroMessenger/8.0.20(0x18001442) NetType/WIFI Language/zh_CN",
- "Referer": "https://servicewechat.com/wxbb9a805eb4f9533c/236/page-frame.html"
- }
- time.sleep(1)
- videoid = random.choice(cls.moment_videoids())
- # Common.logger("moment").info("videoid:{}", videoid)
- params = {
- "vid": videoid,
- "openid": "1924336296754305",
- "model": "iPhone 11<iPhone12,1>14.7.1",
- "sharesearchid": "8406805193800900989",
- "shareOpenid": "oh_m45YffSEGxvDH--6s6g9ZkPxg",
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=url, headers=headers, params=params, proxies=proxies, verify=False)
- # Common.logger("moment").info("response:{}", r.json())
- if "rec_video_list" not in r.json()["data"]:
- Common.logger("moment").warning("该视频无推荐视频列表:{}", videoid)
- else:
- feeds = r.json()["data"]["rec_video_list"]
- for i in range(len(feeds)):
- # video_id
- if "vid" in feeds[i]:
- video_id = feeds[i]["vid"]
- else:
- video_id = 0
- # video_title
- if "title" in feeds[i]:
- video_title = feeds[i]["title"].strip().replace("\n", "") \
- .replace("/", "").replace("\\", "").replace("\r", "") \
- .replace(":", "").replace("*", "").replace("?", "") \
- .replace("?", "").replace('"', "").replace("<", "") \
- .replace(">", "").replace("|", "").replace(" ", "") \
- .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
- .replace("小年糕", "").replace("#", "").replace("Merge", "")
- else:
- video_title = 0
- # video_play_cnt
- if "played_cnt" in feeds[i]:
- video_play_cnt = feeds[i]["played_cnt"]
- else:
- video_play_cnt = 0
- # video_comment_cnt
- if "comment_cnt" in feeds[i]:
- video_comment_cnt = feeds[i]["comment_cnt"]
- else:
- video_comment_cnt = 0
- # video_liked_cnt
- if "liked_cnt" in feeds[i]:
- video_liked_cnt = feeds[i]["liked_cnt"]
- else:
- video_liked_cnt = 0
- # video_share_cnt
- if "shared_cnt" in feeds[i]:
- video_share_cnt = feeds[i]["shared_cnt"]
- else:
- video_share_cnt = 0
- # video_duration
- if "duration" in feeds[i]:
- video_duration = feeds[i]["duration"]
- else:
- video_duration = 0
- # video_width / video_height
- if "width" in feeds[i] or "height" in feeds[i]:
- video_width = feeds[i]["width"]
- video_height = feeds[i]["height"]
- else:
- video_width = 0
- video_height = 0
- # video_send_time
- if "upload_time" in feeds[i]:
- video_send_time = feeds[i]["upload_time"]
- else:
- video_send_time = 0
- # user_name
- if "user_info" not in feeds[i]:
- user_name = 0
- elif "nickname" not in feeds[i]["user_info"]:
- user_name = 0
- else:
- user_name = feeds[i]["user_info"]["nickname"].strip().replace("\n", "")
- # user_id
- if "user_info" not in feeds[i]:
- user_id = 0
- elif "openid" not in feeds[i]["user_info"]:
- user_id = 0
- else:
- user_id = feeds[i]["user_info"]["openid"]
- # head_url
- if "user_info" not in feeds[i]:
- head_url = 0
- elif "headimg_url" not in feeds[i]["user_info"]:
- head_url = 0
- else:
- head_url = feeds[i]["user_info"]["headimg_url"]
- # cover_url
- if "cover_url" not in feeds[i]:
- cover_url = 0
- else:
- cover_url = feeds[i]["cover_url"]
- # video_url
- if "play_info" not in feeds[i]:
- video_url = 0
- elif "items" not in feeds[i]["play_info"]:
- video_url = 0
- else:
- video_url = feeds[i]["play_info"]["items"][-1]["play_url"]
- Common.logger("moment").info("video_id:{}", video_id)
- Common.logger("moment").info("video_title:{}", video_title)
- Common.logger("moment").info("user_name:{}", user_name)
- Common.logger("moment").info("video_play_cnt:{}", video_play_cnt)
- Common.logger("moment").info("video_liked_cnt:{}", video_liked_cnt)
- Common.logger("moment").info("video_share_cnt:{}", video_share_cnt)
- Common.logger("moment").info("video_duration:{}", video_duration)
- Common.logger("moment").info("video_width * video_height:{}*{}", video_width, video_height)
- Common.logger("moment").info("video_url:{}", video_url)
- # 过滤无效视频
- if video_id == 0 or video_title == 0 or video_duration == 0 or video_send_time == 0 or user_id == 0\
- or head_url == 0 or cover_url == 0 or video_url == 0:
- Common.logger("moment").warning("无效视频")
- # 抓取基础规则
- elif cls.download_rule(
- d_duration=video_duration, d_width=video_width, d_height=video_height,
- d_play_cnt=video_play_cnt, d_like_cnt=video_liked_cnt,
- d_share_cnt=video_share_cnt) is False:
- Common.logger("moment").info("不满足基础规则:{}", video_title)
- elif int(video_send_time) < 1659283200:
- Common.logger("moment").info('发布时间{}<2022-08-01', video_send_time)
- # 过滤词库
- elif any(word if word in video_title else False for word in cls.sensitive_words()) is True:
- Common.logger("moment").info("视频已中过滤词:{}".format(video_title))
- # 从已下载视频表去重:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=20ce0c
- elif video_id in [j for m in Feishu.get_values_batch("moment", "kanyikan", "20ce0c") for j in m]:
- Common.logger("moment").info("该视频已下载:{}", video_title)
- # 从feeds视频表去重:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=tGqZMX
- elif video_id in [j for n in Feishu.get_values_batch("moment", "kanyikan", "tGqZMX") for j in n]:
- Common.logger("moment").info("该视频已在moment_feeds中:{}", video_title)
- else:
- Common.logger("moment").info("该视频未下载,添加至moment_feeds中:{}", video_title)
- # 看一看+工作表,插入首行
- Feishu.insert_columns("moment", "kanyikan", "tGqZMX", "ROWS", 1, 2)
- # 获取当前时间
- get_feeds_time = int(time.time())
- # 准备写入云文档的数据
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(get_feeds_time)),
- "朋友圈",
- video_id,
- video_title,
- video_play_cnt,
- video_comment_cnt,
- video_liked_cnt,
- video_share_cnt,
- video_duration,
- str(video_width)+"*"+str(video_height),
- time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(video_send_time)),
- user_name,
- user_id,
- head_url,
- cover_url,
- video_url]]
- time.sleep(1)
- Feishu.update_values("moment", "kanyikan", "tGqZMX", "A2:P2", values)
- except Exception as e:
- Common.logger("moment").error("获取视频列表异常:{}", e)
- # 下载/上传视频
- @classmethod
- def download_publish(cls, env):
- try:
- moment_feeds = Feishu.get_values_batch("moment", "kanyikan", "tGqZMX")
- for i in range(1, len(moment_feeds) + 1):
- time.sleep(1)
- # download_push_time = moment_feeds[i][0]
- download_video_id = moment_feeds[i][2]
- download_video_title = moment_feeds[i][3]
- download_video_play_cnt = moment_feeds[i][4]
- download_video_comment_cnt = moment_feeds[i][5]
- download_video_like_cnt = moment_feeds[i][6]
- download_video_share_cnt = moment_feeds[i][7]
- download_video_duration = moment_feeds[i][8]
- download_video_resolution = moment_feeds[i][9]
- download_video_send_time = moment_feeds[i][10]
- download_user_name = moment_feeds[i][11]
- download_user_id = moment_feeds[i][12]
- download_head_url = moment_feeds[i][13]
- download_cover_url = moment_feeds[i][14]
- download_video_url = moment_feeds[i][15]
- Common.logger("moment").info("正在判断第{}行,视频:{}", i, download_video_title)
- # 发布时间的时间戳格式(秒为单位)
- v_send_time = int(time.mktime(time.strptime(download_video_send_time, "%Y/%m/%d %H:%M:%S")))
- # 抓取时间的时间戳格式(秒为单位)
- # v_push_time = int(time.mktime(time.strptime(download_push_time, "%Y/%m/%d %H:%M:%S")))
- # 过滤空行及空标题视频
- if download_video_id is None\
- or download_video_id == ""\
- or download_video_title is None\
- or download_video_title == "":
- Common.logger("moment").warning("标题为空或空行,删除")
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("moment", "kanyikan", "tGqZMX", "ROWS", i + 1, i + 1)
- return
- # # 视频的抓取时间小于 2 天
- # elif int(time.time()) - v_push_time > 172800:
- # Common.logger("moment").info("抓取时间超过2天:{}", download_video_title)
- # # 删除行或列,可选 ROWS、COLUMNS
- # Feishu.dimension_range("tGqZMX", "ROWS", i + 1, i + 1)
- # return
- # 视频发布时间不小于 2021-06-01 00:00:00
- elif v_send_time < 1622476800:
- Common.logger("moment").info(
- "发布时间小于2021年6月:{},{}", download_video_title, download_video_send_time)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("moment", "kanyikan", "tGqZMX", "ROWS", i + 1, i + 1)
- return
- # 从已下载视频表中去重
- elif download_video_id in [j for m in Feishu.get_values_batch(
- "moment", "kanyikan", "20ce0c") for j in m]:
- Common.logger("moment").info("视频已下载:{}", download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("moment", "kanyikan", "tGqZMX", "ROWS", i + 1, i + 1)
- return
- # 从已下载视频表中去重
- elif download_video_id in [j for m in Feishu.get_values_batch(
- "moment", "kanyikan", "ho98Ov") for j in m]:
- Common.logger("moment").info("视频已下载:{}", download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("moment", "kanyikan", "tGqZMX", "ROWS", i + 1, i + 1)
- return
- else:
- Common.logger("moment").info("开始下载视频:{}", download_video_title)
- # 下载封面
- Common.download_method(log_type="moment", text="cover",
- d_name=str(download_video_title), d_url=str(download_cover_url))
- # 下载视频
- Common.download_method(log_type="moment", text="video",
- d_name=str(download_video_title), d_url=str(download_video_url))
- # 保存视频信息至 "./videos/{download_video_title}/info.txt"
- with open("./videos/" + download_video_title + "/" + "info.txt",
- "a", encoding="UTF-8") as f_a:
- f_a.write(str(download_video_id) + "\n" +
- str(download_video_title) + "\n" +
- str(download_video_duration) + "\n" +
- str(download_video_play_cnt) + "\n" +
- str(download_video_comment_cnt) + "\n" +
- str(download_video_like_cnt) + "\n" +
- str(download_video_share_cnt) + "\n" +
- str(download_video_resolution) + "\n" +
- str(int(time.mktime(
- time.strptime(download_video_send_time, "%Y/%m/%d %H:%M:%S")))) + "\n" +
- str(download_user_name) + "\n" +
- str(download_head_url) + "\n" +
- str(download_video_url) + "\n" +
- str(download_cover_url) + "\n" +
- "KANYIKAN_MOMENT")
- Common.logger("moment").info("==========视频信息已保存至info.txt==========")
- # 上传视频
- Common.logger("moment").info("开始上传视频:{}".format(download_video_title))
- our_video_id = Publish.upload_and_publish(log_type="moment",
- crawler="kanyikan",
- strategy="朋友圈抓取策略",
- our_uid="moment",
- env=env,
- oss_endpoint="out")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
- Common.logger("moment").info("视频上传完成:{}", download_video_title)
- # 保存视频 ID 到云文档:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=20ce0c
- Common.logger("moment").info("保存视频ID至云文档:{}", download_video_title)
- # 视频ID工作表,插入首行
- Feishu.insert_columns("moment", "kanyikan", "20ce0c", "ROWS", 1, 2)
- # 视频ID工作表,首行写入数据
- upload_time = int(time.time())
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
- "朋友圈",
- str(download_video_id),
- str(download_video_title),
- our_video_link,
- download_video_play_cnt,
- download_video_comment_cnt,
- download_video_like_cnt,
- download_video_share_cnt,
- download_video_duration,
- str(download_video_resolution),
- str(download_video_send_time),
- str(download_user_name),
- str(download_user_id),
- str(download_head_url),
- str(download_cover_url),
- str(download_video_url)]]
- time.sleep(1)
- Feishu.update_values("moment", "kanyikan", "20ce0c", "F2:W2", values)
- # 保存视频信息到监控表
- Common.logger("moment").info("添加视频到监控表:{}", download_video_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns("moment", "monitor", "6fed97", "ROWS", 1, 2)
- # 视频信息写入监控表
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- str(download_video_id),
- download_video_title,
- our_video_link,
- download_video_duration,
- str(download_video_send_time),
- download_video_play_cnt]]
- time.sleep(1)
- Feishu.update_values("moment", "monitor", "6fed97", "F2:L2", values)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("moment", "kanyikan", "tGqZMX", "ROWS", i + 1, i + 1)
- return
- except Exception as e:
- Common.logger("moment").error("下载视频异常:{}", e)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range("moment", "kanyikan", "tGqZMX", "ROWS", 2, 2)
- # 执行下载/上传
- @classmethod
- def run_download_publish(cls, env):
- try:
- while True:
- if len(Feishu.get_values_batch("moment", "kanyikan", "tGqZMX")) == 1:
- break
- else:
- cls.download_publish(env)
- except Exception as e:
- Common.logger("moment").error("执行下载/上传异常:{}", e)
- if __name__ == "__main__":
- kuaishou = Moment()
- kuaishou.run_download_publish("dev")
- pass
|