123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- # -*- coding: utf-8 -*-
- # @Author: wangkun
- # @Time: 2022/7/7
- import os
- import random
- import sys
- import time
- import requests
- import urllib3
- sys.path.append(os.getcwd())
- from main.common import Common
- from main.feishu_lib import Feishu
- from main.kuaishou_publish import Publish
- proxies = {"http": None, "https": None}
- class Follow:
- # 已抓取视频数量
- get_person_video_count = []
- get_all_video_count = []
- # 小程序:关注列表翻页参数
- follow_pcursor = ""
- # 小程序:个人主页视频列表翻页参数
- person_pcursor = ""
- # 视频发布时间
- send_time = 0
- # 配置微信
- wechat_sheet = Feishu.get_values_batch("follow", "kuaishou", "WFF4jw")
- Referer = wechat_sheet[2][3]
- NS_sig3 = wechat_sheet[3][3]
- NS_sig3_origin = wechat_sheet[4][3]
- did = wechat_sheet[5][3]
- session_key = wechat_sheet[6][3]
- unionid = wechat_sheet[7][3]
- eUserStableOpenId = wechat_sheet[8][3]
- openId = wechat_sheet[9][3]
- eOpenUserId = wechat_sheet[10][3]
- kuaishou_wechat_app_st = wechat_sheet[11][3]
- passToken = wechat_sheet[12][3]
- userId = wechat_sheet[13][3]
- # 过滤敏感词
- @classmethod
- def sensitive_words(cls):
- # 敏感词库列表
- word_list = []
- # 从云文档读取所有敏感词,添加到词库列表
- lists = Feishu.get_values_batch("follow", "kuaishou", "HIKVvs")
- for i in lists:
- for j in i:
- # 过滤空的单元格内容
- if j is None:
- pass
- else:
- word_list.append(j)
- return word_list
- # 下载规则
- @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) >= 5000:
- if int(d_like_cnt) >= 5000 or int(d_share_cnt) >= 1000:
- return True
- else:
- return False
- else:
- return False
- else:
- return False
- else:
- return False
- # 删除飞书关注人列表
- @classmethod
- def del_follow_user_from_feishu(cls, log_type):
- try:
- while True:
- follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "2OLxLr")
- if len(follow_sheet) == 1:
- Common.logger(log_type).info('删除完成\n')
- return
- else:
- for i in range(1, len(follow_sheet)):
- Feishu.dimension_range(log_type, "kuaishou", "2OLxLr", 'ROWS', i+1, i+1)
- time.sleep(0.5)
- break
- except Exception as e:
- Common.logger(log_type).error('del_follow_user_from_feishu异常:{}', e)
- # 从小程序中,关注用户列表同步至云文档
- @classmethod
- def get_follow_users_to_feishu(cls, log_type):
- try:
- follow_list = []
- follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "2OLxLr")
- url = "https://wxmini-api.uyouqu.com/rest/wd/wechatApp/relation/fol?"
- 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": str(cls.Referer),
- }
- params = {
- "__NS_sig3": str(cls.NS_sig3),
- "__NS_sig3_origin": str(cls.NS_sig3_origin)
- }
- cookies = {
- "did": str(cls.did),
- "preMinaVersion": "v3.109.0",
- "sid": "kuaishou.wechat.app",
- "appId": "ks_wechat_small_app_2",
- "clientid": "13",
- "client_key": "f60ac815",
- "kpn": "WECHAT_SMALL_APP",
- "kpf": "OUTSIDE_ANDROID_H5",
- "language": "zh_CN",
- "smallAppVersion": "v3.114.0",
- "session_key": str(cls.session_key),
- "unionid": str(cls.unionid),
- "eUserStableOpenId": str(cls.eUserStableOpenId),
- "openId": str(cls.openId),
- "eOpenUserId": str(cls.eOpenUserId),
- "kuaishou.wechat.app_st": str(cls.kuaishou_wechat_app_st),
- "passToken": str(cls.passToken),
- "userId": str(cls.userId)
- }
- json_text = {
- "count": 20,
- "pcursor": str(cls.follow_pcursor),
- "ftype": 1
- }
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, params=params,
- cookies=cookies, json=json_text, proxies=proxies, verify=False)
- if "fols" not in r.json():
- Common.logger(log_type).warning("从小程序中获取关注用户列表:{}", r.text)
- else:
- users = r.json()["fols"]
- for i in range(len(users)):
- uid = users[i]["targetId"]
- nick = users[i]["targetName"]
- sex = users[i]["targetSex"]
- description = users[i]["targetUserText"]
- if "followReason" in users[i]:
- follow_reason = users[i]["followReason"]
- else:
- follow_reason = ""
- follow_time = users[i]["time"]
- is_friend = users[i]["isFriend"]
- # print(f"uid:{uid}")
- follow_list.append(uid)
- # print(f"follow_list:{follow_list}")
- # 同步已关注的用户至云文档
- if uid not in [j for i in follow_sheet for j in i]:
- time.sleep(1)
- Feishu.insert_columns(log_type, "kuaishou", "2OLxLr", "ROWS", 1, 2)
- time.sleep(1)
- values = [[uid, nick, sex, description, follow_reason, follow_time, str(is_friend)]]
- Feishu.update_values(log_type, "kuaishou", "2OLxLr", "A2:L2", values)
- else:
- Common.logger(log_type).info("用户:{},在云文档中已存在", nick)
- cls.follow_pcursor = r.json()["pcursor"]
- # 翻页,直至到底了
- if cls.follow_pcursor != "no_more":
- cls.get_follow_users_to_feishu(log_type)
- else:
- Common.logger(log_type).info("从小程序中同步关注用户至云文档完成\n")
- except Exception as e:
- Common.logger(log_type).error("从小程序中,关注用户列表同步至云文档异常:{}\n", e)
- # 从云文档获取关注用户列表
- @classmethod
- def get_follow_users(cls, log_type):
- try:
- follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "2OLxLr")
- if len(follow_sheet) == 1:
- Common.logger(log_type).info("暂无关注用户")
- else:
- follow_dict = {}
- for i in range(1, len(follow_sheet)):
- uid = follow_sheet[i][0]
- nick = follow_sheet[i][1]
- if uid is None or nick is None:
- pass
- else:
- follow_dict[nick] = uid
- return follow_dict
- except Exception as e:
- Common.logger(log_type).error("从云文档获取关注用户列表异常:{}\n", e)
- # 从云文档获取取消关注用户列表
- @classmethod
- def get_unfollow_users(cls, log_type):
- try:
- unfollow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "WRveYg")
- if len(unfollow_sheet) == 1:
- Common.logger(log_type).info("暂无取消关注用户")
- else:
- unfollow_list = []
- nick_list = []
- for i in range(1, len(unfollow_sheet)):
- uid = unfollow_sheet[i][0]
- nick = unfollow_sheet[i][1]
- nick_list.append(nick)
- unfollow_list.append(uid)
- Common.logger(log_type).info("取消关注用户列表:{}", nick_list)
- return unfollow_list
- except Exception as e:
- Common.logger(log_type).error("从云文档获取取消关注用户列表异常:{}", e)
- # 小程序:关注/取消关注用户
- @classmethod
- def follow_unfollow(cls, log_type, is_follow, uid):
- try:
- url = "https://wxmini-api.uyouqu.com/rest/wd/wechatApp/relation/follow?"
- 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": str(cls.Referer),
- }
- params = {
- "__NS_sig3": str(cls.NS_sig3),
- "__NS_sig3_origin": str(cls.NS_sig3_origin)
- }
- cookies = {
- "did": str(cls.did),
- "preMinaVersion": "v3.109.0",
- "sid": "kuaishou.wechat.app",
- "appId": "ks_wechat_small_app_2",
- "clientid": "13",
- "client_key": "f60ac815",
- "kpn": "WECHAT_SMALL_APP",
- "kpf": "OUTSIDE_ANDROID_H5",
- "language": "zh_CN",
- "smallAppVersion": "v3.114.0",
- "session_key": str(cls.session_key),
- "unionid": str(cls.unionid),
- "eUserStableOpenId": str(cls.eUserStableOpenId),
- "openId": str(cls.openId),
- "eOpenUserId": str(cls.eOpenUserId),
- "kuaishou.wechat.app_st": str(cls.kuaishou_wechat_app_st),
- "passToken": str(cls.passToken),
- "userId": str(cls.userId)
- }
- if is_follow == "follow":
- ftype = 1
- elif is_follow == "unfollow":
- ftype = 2
- else:
- ftype = 1
- json_text = {
- "touid": uid,
- "ftype": ftype,
- "page_ref": 84
- }
- r = requests.post(url=url, headers=headers, cookies=cookies, params=params, json=json_text)
- if is_follow == "follow":
- if r.json()["result"] != 1:
- Common.logger(log_type).warning("{}", r.text)
- else:
- Common.logger(log_type).info("关注:{}, {}", uid, r)
- else:
- if r.json()["result"] != 1:
- Common.logger(log_type).warning("{}", r.text)
- else:
- Common.logger(log_type).info("取消关注:{}, {}", uid, r)
- except Exception as e:
- Common.logger(log_type).error("关注/取消关注异常:{}", e)
- # 获取个人主页视频
- @classmethod
- def get_user_videos(cls, log_type, uid):
- try:
- time.sleep(1)
- url = "https://wxmini-api.uyouqu.com/rest/wd/wechatApp/feed/profile?"
- 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.26(0x18001a34) NetType/WIFI Language/zh_CN',
- "Referer": str(cls.Referer),
- }
- params = {
- "__NS_sig3": str(cls.NS_sig3),
- "__NS_sig3_origin": str(cls.NS_sig3_origin)
- }
- cookies = {
- "did": str(cls.did),
- "sid": "kuaishou.wechat.app",
- "appId": "ks_wechat_small_app_2",
- "clientid": "13",
- "client_key": "f60ac815",
- "kpn": "WECHAT_SMALL_APP",
- "kpf": "OUTSIDE_IOS_H5",
- "language": "zh_CN",
- "smallAppVersion": "v3.131.0",
- "mod": "iPhone(11<iPhone12%2C1>)",
- "sys": "iOS%2014.7.1",
- 'wechatVersion': '8.0.26',
- "brand": "iPhone",
- "session_key": str(cls.session_key),
- "unionid": str(cls.unionid),
- "eUserStableOpenId": str(cls.eUserStableOpenId),
- "openId": str(cls.openId),
- "eOpenUserId": str(cls.eOpenUserId),
- "kuaishou.wechat.app_st": str(cls.kuaishou_wechat_app_st),
- "passToken": str(cls.passToken),
- "userId": str(cls.userId)
- }
- json_text = {
- "count": 12,
- "pcursor": str(cls.person_pcursor),
- "eid": str(uid)
- }
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, params=params, cookies=cookies,
- json=json_text, proxies=proxies, verify=False)
- # Common.logger(log_type).info("response:{}\n\n", r.text)
- if "feeds" not in r.json():
- # Feishu.bot(log_type, "follow:get_videos_from_person:"+r.text)
- Common.logger(log_type).warning("response:{}", r.text)
- elif r.json()["feeds"] == 0:
- Common.logger(log_type).warning("用户主页无视频\n")
- return
- else:
- feeds = r.json()["feeds"]
- for i in range(len(feeds)):
- # 视频标题过滤话题及处理特殊字符
- kuaishou_title = feeds[i]["caption"]
- title_split1 = kuaishou_title.split(" #")
- if title_split1[0] != "":
- title1 = title_split1[0]
- else:
- title1 = title_split1[-1]
- title_split2 = title1.split(" #")
- if title_split2[0] != "":
- title2 = title_split2[0]
- else:
- title2 = title_split2[-1]
- title_split3 = title2.split("@")
- if title_split3[0] != "":
- title3 = title_split3[0]
- else:
- title3 = title_split3[-1]
- video_title = title3.strip().replace("\n", "") \
- .replace("/", "").replace("快手", "").replace(" ", "") \
- .replace(" ", "").replace("&NBSP", "").replace("\r", "") \
- .replace("#", "").replace(".", "。").replace("\\", "") \
- .replace(":", "").replace("*", "").replace("?", "") \
- .replace("?", "").replace('"', "").replace("<", "") \
- .replace(">", "").replace("|", "").replace("@", "")[:40]
- if "photoId" not in feeds[i]:
- video_id = "0"
- else:
- video_id = feeds[i]["photoId"]
- if "viewCount" not in feeds[i]:
- video_play_cnt = "0"
- else:
- video_play_cnt = feeds[i]["viewCount"]
- if "likeCount" not in feeds[i]:
- video_like_cnt = "0"
- else:
- video_like_cnt = feeds[i]["likeCount"]
- if "shareCount" not in feeds[i]:
- video_share_cnt = "0"
- else:
- video_share_cnt = feeds[i]["shareCount"]
- if "commentCount" not in feeds[i]:
- video_comment_cnt = "0"
- else:
- video_comment_cnt = feeds[i]["commentCount"]
- if "duration" not in feeds[i]:
- video_duration = "0"
- else:
- video_duration = int(int(feeds[i]["duration"]) / 1000)
- if "width" not in feeds[i] or "height" not in feeds[i]:
- video_width = "0"
- video_height = "0"
- else:
- video_width = feeds[i]["width"]
- video_height = feeds[i]["height"]
- if "timestamp" not in feeds[i]:
- video_send_time = "0"
- else:
- video_send_time = feeds[i]["timestamp"]
- cls.send_time = int(int(video_send_time) / 1000)
- if "userName" not in feeds[i]:
- user_name = "0"
- else:
- user_name = feeds[i]["userName"].strip().replace("\n", "") \
- .replace("/", "").replace("快手", "").replace(" ", "") \
- .replace(" ", "").replace("&NBSP", "").replace("\r", "")
- if "userId" not in feeds[i]:
- user_id = "0"
- else:
- user_id = feeds[i]["userId"]
- if "headUrl" not in feeds[i]:
- head_url = "0"
- else:
- head_url = feeds[i]["headUrl"]
- if "webpCoverUrls" in feeds[i]:
- cover_url = feeds[i]["webpCoverUrls"][-1]["url"]
- elif "coverUrls" not in feeds[i]:
- cover_url = "0"
- elif len(feeds[i]["coverUrls"]) == 0:
- cover_url = "0"
- else:
- cover_url = feeds[i]["coverUrls"][0]["url"]
- if "mainMvUrls" not in feeds[i]:
- video_url = "0"
- elif len(feeds[i]["mainMvUrls"]) == 0:
- video_url = "0"
- else:
- video_url = feeds[i]["mainMvUrls"][0]["url"]
- Common.logger(log_type).info("video_title:{}".format(video_title))
- Common.logger(log_type).info("user_name:{}".format(user_name))
- Common.logger(log_type).info("video_play_cnt:{}".format(video_play_cnt))
- Common.logger(log_type).info("video_like_cnt:{}".format(video_like_cnt))
- Common.logger(log_type).info("video_duration:{}秒".format(video_duration))
- Common.logger(log_type).info("video_send_time:{}".format(
- time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000))))
- Common.logger(log_type).info("video_url:{}".format(video_url))
- # 过滤无效视频
- if video_id == "0" \
- or head_url == "0" \
- or cover_url == "0" \
- or video_url == "0" \
- or video_duration == "0" \
- or video_send_time == "0" \
- or user_name == "0" \
- or user_id == "0" \
- or video_title == "":
- Common.logger(log_type).info("无效视频\n")
- # 视频发布时间 <= 7 天
- elif int(time.time()) - int(int(video_send_time) / 1000) > 604800:
- Common.logger("follow").info("发布时间:{},超过7天\n", time.strftime(
- "%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
- cls.person_pcursor = ""
- return
- # 判断敏感词
- elif cls.download_rule(video_duration, video_width, video_height, video_play_cnt,
- video_like_cnt, video_share_cnt) is False:
- Common.logger(log_type).info("不满足下载规则\n".format(kuaishou_title))
- elif any(word if word in kuaishou_title else False for word in cls.sensitive_words()) is True:
- Common.logger(log_type).info("视频已中敏感词:{}\n".format(kuaishou_title))
- # 从云文档去重: 推荐榜_已下载表
- elif str(video_id) in [j for m in Feishu.get_values_batch(log_type, "kuaishou", "3cd128") for j in m]:
- Common.logger(log_type).info("该视频已下载:{}\n", video_title)
- # 从云文档去重: 用户主页_已下载表
- elif str(video_id) in [j for m in Feishu.get_values_batch(log_type, "kuaishou", "fYdA8F") for j in m]:
- Common.logger(log_type).info("该视频已下载:{}\n", video_title)
- # 从云文档去重:用户主页_feeds
- elif str(video_id) in [j for n in Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb") for j in n]:
- Common.logger(log_type).info("该视频已在feeds中:{}\n", video_title)
- else:
- Feishu.insert_columns("follow", "kuaishou", "wW5cyb", "ROWS", 1, 2)
- # 获取当前时间
- get_feeds_time = int(time.time())
- # 工作表中写入数据
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(get_feeds_time))),
- "用户主页",
- str(video_id),
- video_title,
- video_play_cnt,
- video_comment_cnt,
- video_like_cnt,
- video_share_cnt,
- video_duration,
- str(video_width) + "*" + str(video_height),
- time.strftime(
- "%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)),
- user_name,
- user_id,
- head_url,
- cover_url,
- video_url]]
- # 等待 1s,防止操作云文档太频繁,导致报错
- time.sleep(1)
- Feishu.update_values("follow", "kuaishou", "wW5cyb", "A2:T2", values)
- Common.logger("follow").info("添加视频至follow_feeds成功:{}\n", video_title)
- cls.get_person_video_count.append(video_id)
- # # 抓取足够多数量的视频
- # if len(cls.get_person_video_count) >= 1:
- # Common.logger(log_type).info('已抓取{}:{}条视频\n', user_name, len(cls.get_person_video_count))
- # cls.person_pcursor = ""
- # cls.get_person_video_count = []
- # return
- if r.json()["pcursor"] == 'no_more':
- Common.logger(log_type).info('没有更多作品了\n')
- return
- elif len(cls.get_person_video_count) < 1:
- Common.logger(log_type).info('休眠 10-20 秒,翻页')
- time.sleep(random.randint(10, 20))
- # 翻页
- cls.person_pcursor = r.json()["pcursor"]
- cls.get_user_videos(log_type, uid)
- except Exception as e:
- Common.logger(log_type).error("get_videos_from_person异常:{}\n", e)
- # 获取所有关注列表的用户视频
- @classmethod
- def get_videos_from_follow(cls, log_type, env):
- try:
- user_list = cls.get_follow_users(log_type)
- if len(user_list) == 0:
- Common.logger(log_type).warning('用户ID列表为空\n')
- else:
- while True:
- for k, v in user_list.items():
- Common.logger(log_type).info('正在获取 {} 主页视频\n', k)
- cls.person_pcursor = ""
- cls.get_user_videos(log_type, str(v))
- cls.run_download_publish(log_type, env)
- if len(cls.get_all_video_count) >= 100:
- cls.get_all_video_count = []
- Common.logger(log_type).info('今日已抓取{}条视频\n', len(cls.get_all_video_count))
- return
- else:
- Common.logger(log_type).info('随机休眠 10-30 秒\n')
- time.sleep(random.randint(10, 30))
- except Exception as e:
- Common.logger(log_type).error('get_videos_from_follow异常:{}\n', e)
- # 下载/上传
- @classmethod
- def download_publish(cls, log_type, env):
- try:
- follow_feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
- for i in range(1, len(follow_feeds_sheet)):
- time.sleep(1)
- download_video_id = follow_feeds_sheet[i][2]
- download_video_title = follow_feeds_sheet[i][3]
- download_video_play_cnt = follow_feeds_sheet[i][4]
- download_video_comment_cnt = follow_feeds_sheet[i][5]
- download_video_like_cnt = follow_feeds_sheet[i][6]
- download_video_share_cnt = follow_feeds_sheet[i][7]
- download_video_duration = follow_feeds_sheet[i][8]
- download_video_resolution = follow_feeds_sheet[i][9]
- download_video_send_time = follow_feeds_sheet[i][10]
- download_user_name = follow_feeds_sheet[i][11]
- download_user_id = follow_feeds_sheet[i][12]
- download_head_url = follow_feeds_sheet[i][13]
- download_cover_url = follow_feeds_sheet[i][14]
- download_video_url = follow_feeds_sheet[i][15]
- Common.logger(log_type).info("正在判断第{}行,视频:{}", i + 1, download_video_title)
- # 过滤空行及空标题视频
- if download_video_id is None \
- or download_video_id == "" \
- or download_video_title is None \
- or download_video_title == "":
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
- Common.logger(log_type).warning("标题为空或空行,删除成功\n")
- return
- # 从已下载视频表中去重:推荐榜_已下载表
- elif str(download_video_id) in [j for m in Feishu.get_values_batch(
- log_type, "kuaishou", "3cd128") for j in m]:
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
- Common.logger(log_type).info("视频已下载:{},删除成功\n", download_video_title)
- return
- # 从已下载视频表中去重:用户主页_已下载表
- elif str(download_video_id) in [j for m in Feishu.get_values_batch(
- log_type, "kuaishou", "fYdA8F") for j in m]:
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
- Common.logger(log_type).info("视频已下载:{},删除成功\n", download_video_title)
- return
- # 从已下载视频表中去重:用户主页_已下载表
- elif str(download_video_id) in [j for m in Feishu.get_values_batch(
- log_type, "kuaishou", "31kOdu") for j in m]:
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
- Common.logger(log_type).info("视频已下载:{},删除成功\n", download_video_title)
- return
- else:
- # 下载封面
- Common.download_method(log_type=log_type, text="cover",
- d_name=str(download_video_title), d_url=str(download_cover_url))
- # 下载视频
- Common.download_method(log_type=log_type, 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" +
- "kuaishou_person")
- Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
- # 上传视频
- Common.logger(log_type).info("开始上传视频:{}".format(download_video_title))
- our_video_id = Publish.upload_and_publish(log_type, env, "play")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
- Common.logger(log_type).info("视频上传完成:{}", download_video_title)
- # 视频ID工作表,插入首行
- time.sleep(1)
- Feishu.insert_columns(log_type, "kuaishou", "fYdA8F", "ROWS", 1, 2)
- # 视频ID工作表,首行写入数据
- upload_time = int(time.time())
- values = [[our_video_id,
- 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(log_type, "kuaishou", "fYdA8F", "E2:Z2", values)
- cls.get_all_video_count.append(download_video_id)
- Common.logger(log_type).info("保存视频ID至已下载云文档成功:{}", download_video_title)
- # 删除行或列,可选 ROWS、COLUMNS
- Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
- Common.logger(log_type).info("视频:{},下载/上传成功\n", download_video_title)
- return
- except Exception as e:
- Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", 2, 2)
- Common.logger(log_type).error("download_publish异常,删除成功:{}\n", e)
- # 执行下载/上传
- @classmethod
- def run_download_publish(cls, log_type, env):
- try:
- while True:
- follow_feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
- if len(follow_feeds_sheet) == 1:
- Common.logger(log_type).info("下载/上传完成\n")
- break
- else:
- cls.download_publish(log_type, env)
- except Exception as e:
- Common.logger(log_type).error("run_download_publish异常:{}\n", e)
- if __name__ == "__main__":
- Follow.get_user_videos('follow', '240529022')
- pass
|