123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584 |
- # -*- coding: utf-8 -*-
- # @Author: wangkun
- # @Time: 2022/5/16
- import datetime
- 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.publish import Publish
- proxies = {"http": None, "https": None}
- class HourList:
- # 配置微信
- time.sleep(1)
- wechat_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "dzcWHw")
- hour_x_b3_traceid = wechat_sheet[2][1]
- hour_x_token_id = wechat_sheet[3][1]
- hour_referer = wechat_sheet[4][1]
- hour_uid = wechat_sheet[5][1]
- hour_token = wechat_sheet[6][1]
- # # 小时级数据表
- # hour_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4")
- # 生成 uid、token
- @classmethod
- def get_uid_token(cls):
- uid_token = "abcdefghijklmnopqrstuvwxyz0123456789"
- uid = ("".join(random.sample(uid_token, 8))) + "-" + ("".join(random.sample(uid_token, 4))) + "-" + (
- "".join(random.sample(uid_token, 4))) + "-" + ("".join(random.sample(uid_token, 4))) + "-" + (
- "".join(random.sample(uid_token, 12)))
- token = ("".join(random.sample(uid_token, 32)))
- Feishu.update_values("hour", "xiaoniangao", "dzcWHw", "B6:B6", [[uid]])
- time.sleep(1)
- Feishu.update_values("hour", "xiaoniangao", "dzcWHw", "B7:B7", [[token]])
- Common.logger("hour").info("生成 uid:{}, token:{},写入飞书成功\n", uid, token)
- # 过滤敏感词
- @classmethod
- def sensitive_words(cls, log_type):
- # 敏感词库列表
- word_list = []
- # 从云文档读取所有敏感词,添加到词库列表
- time.sleep(1)
- lists = Feishu.get_values_batch(log_type, "xiaoniangao", "DRAnZh")
- for i in lists:
- for j in i:
- # 过滤空的单元格内容
- if j is None:
- pass
- else:
- word_list.append(j)
- return word_list
- # 视频ID过滤字母
- @classmethod
- def sensitive_videoid_words(cls):
- # 字母列表
- words_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
- "t", "u", "v", "w", "x", "y", "z",
- "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
- "T", "U", "V", "W", "X", "Y", "Z"]
- return words_list
- # 基础门槛规则
- @staticmethod
- def download_rule(d_duration, d_width, d_height, d_play_cnt, d_like_cnt, d_share_cnt, d_send_time):
- """
- 下载视频的基本规则
- :param d_duration: 时长
- :param d_width: 宽
- :param d_height: 高
- :param d_play_cnt: 播放量
- :param d_like_cnt: 点赞量
- :param d_share_cnt: 分享量
- :param d_send_time: 发布时间
- :return: 满足规则,返回 True;反之,返回 False
- """
- # 视频时长
- if int(float(d_duration)) >= 40:
- # 宽或高
- if int(d_width) >= 0 or int(d_height) >= 0:
- # 播放量
- if int(d_play_cnt) >= 4000:
- # 点赞量
- if int(d_like_cnt) >= 0:
- # 分享量
- if int(d_share_cnt) >= 0:
- # 发布时间 <= 10 天
- if int(time.time()) - int(d_send_time) / 1000 <= 864000:
- return True
- else:
- return False
- else:
- return False
- else:
- return False
- else:
- return False
- return False
- return False
- # 检查是否有今日的上升榜日期
- @classmethod
- def check_hour_list_data(cls, log_type, date):
- # 判断J1单元格的日期是否为今天
- time.sleep(1)
- if Feishu.get_range_value(log_type, "xiaoniangao", "ba0da4", "L1:L1")[0] != date:
- # 插入3列 L1:N1,并写入日期和时间数据
- values = [[date], ["10:00", "15:00", "20:00"]]
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "ba0da4", "COLUMNS", 11, 14)
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "ba0da4", "L1:N2", values)
- time.sleep(1)
- Feishu.merge_cells(log_type, "xiaoniangao", "ba0da4", "L1:N1")
- Common.logger(log_type).info("插入今天日期成功")
- else:
- Common.logger(log_type).info("今日上升榜日期已存在")
- # 获取表情及符号
- @classmethod
- def get_expression(cls):
- expression_list = []
- char_list = []
- char_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "BhlbST")
- for i in range(len(char_sheet)):
- if char_sheet[i][0] is not None:
- expression_list.append(char_sheet[i][0])
- if char_sheet[i][1] is not None:
- char_list.append(char_sheet[i][1])
- # print(f"expression_list:{expression_list}")
- # print(f"char_list:{char_list}")
- return expression_list, char_list
- # 获取列表
- @classmethod
- def get_hour_list_feeds(cls, log_type):
- """
- 1.从列表获取视频,7 天内,播放量>=5000
- 2.时长 1-10min
- 3.每天10:00、15:00、20:00 把符合规则的视频,写入云文档
- https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
- """
- url = "https://kapi.xiaoniangao.cn/trends/get_recommend_trends"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "log_params": {
- "page": "discover_rec",
- "common": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.2",
- "net": "wifi",
- "scene": 1089
- }
- },
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!750x500r/crop/750x500/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "ext": {
- "fmid": 0,
- "items": {}
- },
- "app": "xng",
- "rec_scene": "discover_rec",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "discoverIndexPage",
- "topic": "recommend"
- },
- "ab": {}
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- },
- "refresh": False,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0"
- }
- # try:
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
- if "data" not in r.json():
- Common.logger(log_type).warning("获取视频feeds错误:{}", r.text)
- elif "list" not in r.json()["data"]:
- Common.logger(log_type).warning("获取视频feeds无数据,休眠10s:{}", r.json()["data"])
- else:
- # 视频列表数据
- feeds = r.json()["data"]["list"]
- for i in range(len(feeds)):
- # 标题,表情随机加在片头、片尾,或替代句子中间的标点符号
- if "title" in feeds[i]:
- befor_video_title = feeds[i]["title"].strip().replace("\n", "") \
- .replace("/", "").replace("\r", "").replace("#", "") \
- .replace(".", "。").replace("\\", "").replace("&NBSP", "") \
- .replace(":", "").replace("*", "").replace("?", "") \
- .replace("?", "").replace('"', "").replace("<", "") \
- .replace(">", "").replace("|", "").replace(" ", "").replace("#表情", "").replace("#符号", "")
- expression = cls.get_expression()
- expression_list = expression[0]
- char_list = expression[1]
- # print(f"expression_list:{expression_list}")
- # print(f"char_list:{char_list}")
- # 随机取一个表情
- expression = random.choice(expression_list)
- # print(f"expression:{expression}")
- # 生成标题list[表情+title, title+表情]
- expression_title_list = [expression + befor_video_title, befor_video_title + expression]
- # print(f"expression_title_list:{expression_title_list}")
- # 从标题list中随机取一个标题
- title_list1 = random.choice(expression_title_list)
- # print(f"title_list1:{title_list1}")
- # 生成标题:原标题+符号
- title_list2 = befor_video_title + random.choice(char_list)
- # print(f"befor_video_title:{befor_video_title}")
- # title_list3 = befor_video_title.replace(
- # ",", random.choice(expression_list)).replace(",", random.choice(expression_list))
- # 表情和标题组合,与标题和符号组合,汇总成待使用的标题列表
- title_list4 = [title_list2, title_list1]
- # print(f"title_list4:{title_list4}")
- # 最终标题
- video_title = random.choice(title_list4)
- # print(f"video_title:{video_title}\n")
- else:
- video_title = 0
- # 视频 ID
- if "vid" in feeds[i]:
- video_id = feeds[i]["vid"]
- else:
- video_id = 0
- # 播放量
- if "play_pv" in feeds[i]:
- video_play_cnt = feeds[i]["play_pv"]
- else:
- video_play_cnt = 0
- # 点赞量
- if "favor" in feeds[i]:
- video_like_cnt = feeds[i]["favor"]["total"]
- else:
- video_like_cnt = 0
- # 分享量
- if "share" in feeds[i]:
- video_share_cnt = feeds[i]["share"]
- else:
- video_share_cnt = 0
- # # 评论量
- # if "comment_count" in feeds[i]:
- # video_comment_cnt = feeds[i]["comment_count"]
- # else:
- # video_comment_cnt = 0
- # 时长
- if "du" in feeds[i]:
- video_duration = int(feeds[i]["du"] / 1000)
- else:
- video_duration = 0
- # 宽和高
- if "w" or "h" in feeds[i]:
- video_width = feeds[i]["w"]
- video_height = feeds[i]["h"]
- else:
- video_width = 0
- video_height = 0
- # 发布时间
- if "t" in feeds[i]:
- video_send_time = feeds[i]["t"]
- else:
- video_send_time = 0
- # 用户名 / 头像
- if "user" in feeds[i]:
- user_name = feeds[i]["user"]["nick"].strip().replace("\n", "") \
- .replace("/", "").replace("快手", "").replace(" ", "") \
- .replace(" ", "").replace("&NBSP", "").replace("\r", "")
- head_url = feeds[i]["user"]["hurl"]
- else:
- user_name = 0
- head_url = 0
- # 用户 ID
- profile_id = feeds[i]["id"]
- # 用户 mid
- profile_mid = feeds[i]["user"]["mid"]
- # 视频封面
- if "url" in feeds[i]:
- cover_url = feeds[i]["url"]
- else:
- cover_url = 0
- # 视频播放地址
- if "v_url" in feeds[i]:
- video_url = feeds[i]["v_url"]
- else:
- video_url = 0
- Common.logger(log_type).info("标题:{}", video_title)
- Common.logger(log_type).info("视频ID:{}", video_id)
- Common.logger(log_type).info("播放量:{}", video_play_cnt)
- # Common.logger(log_type).info("点赞量:{}", video_like_cnt)
- # Common.logger(log_type).info("分享量:{}", video_share_cnt)
- # Common.logger(log_type).info("评论数:{}", video_comment_cnt)
- Common.logger(log_type).info("时长:{}秒", video_duration)
- # Common.logger(log_type).info("宽高:{}*{}", video_width, video_height)
- Common.logger(log_type).info(
- "视频发布时间:{}", time.strftime(
- "%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
- Common.logger(log_type).info("用户名:{}", user_name)
- # Common.logger(log_type).info("用户头像:{}", head_url)
- # Common.logger(log_type).info("封面:{}", cover_url)
- Common.logger(log_type).info("播放地址:{}", video_url)
- # 过滤无效视频
- if video_title == 0 or video_id == 0 or video_duration == 0 \
- or video_send_time == 0 or user_name == 0 or head_url == 0 \
- or cover_url == 0 or video_url == 0:
- Common.logger(log_type).warning("无效视频")
- elif cls.download_rule(video_duration, video_width, video_height, video_play_cnt,
- video_like_cnt, video_share_cnt, video_send_time) is False:
- Common.logger(log_type).info("不满足基础门槛规则")
- # 过滤敏感词
- elif any(word if word in video_title else False for word in cls.sensitive_words(log_type)) is True:
- Common.logger(log_type).info("视频已中敏感词:{}".format(video_title))
- time.sleep(1)
- # 从云文档中去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
- elif video_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2") for j in i]:
- Common.logger(log_type).info("该视频已下载:{}", video_title)
- time.sleep(1)
- # 从云文档去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
- elif video_id in [j for i in Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4") for j in i]:
- Common.logger(log_type).info("该视频已保存过:{}", video_title)
- time.sleep(1)
- else:
- Common.logger(log_type).info("该视频未下载,添加至feeds中:{}".format(video_title))
- # feeds工作表,插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "ba0da4", "ROWS", 2, 3)
- # 获取当前时间
- get_feeds_time = int(time.time())
- # 看一看云文档,工作表中写入数据
- values = [[profile_id,
- profile_mid,
- video_id,
- video_title,
- user_name,
- video_duration,
- cover_url,
- video_url,
- time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)),
- str(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(get_feeds_time))),
- video_play_cnt]]
- # 等待 1s,防止操作云文档太频繁,导致报错
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "ba0da4", "A3:K3", values)
- # except Exception as e:
- # Common.logger(log_type).error("获取小时榜视频列表异常:{}", e)
- # 更新小时榜数据
- @classmethod
- def update_hour_list_data(cls, log_type, today, yesterday, before_yesterday):
- """
- 更新小时榜数据
- """
- try:
- update_hour_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4")
- if len(update_hour_sheet) == 2:
- Common.logger(log_type).info("当前工作表无数据")
- else:
- for i in range(2, len(update_hour_sheet) + 1):
- Common.logger(log_type).info("更新第:{}行视频信息", i + 1)
- # 略过空行
- if update_hour_sheet[i][0] is None \
- or update_hour_sheet[i][1] is None or update_hour_sheet[i][2] is None:
- Common.logger(log_type).info("空行,略过")
- else:
- # 视频标题
- v_title = update_hour_sheet[i][3]
- Common.logger(log_type).info("video_title:{}", v_title)
- # 视频 ID
- v_id = update_hour_sheet[i][2]
- Common.logger(log_type).info("video_id:{}", v_id)
- # profile_id,用户 ID
- p_id = update_hour_sheet[i][0]
- Common.logger(log_type).info("profile_id:{}", p_id)
- # profile_mid
- p_mid = update_hour_sheet[i][1]
- Common.logger(log_type).info("profile_mid:{}", p_mid)
- # 抓取时的播放量
- v_play_cnt = update_hour_sheet[i][10]
- Common.logger(log_type).info("video_play_cnt:{}", v_play_cnt)
- # 抓取时间
- v_upload_time = update_hour_sheet[i][9]
- Common.logger(log_type).info("video_send_time:{}", v_upload_time)
- # 抓取时间的时间戳格式(秒为单位)
- v_time = int(time.mktime(time.strptime(v_upload_time, "%Y/%m/%d %H:%M:%S")))
- # 抓取时间:日期
- upload_data = v_upload_time.split(" ")[0]
- # 抓取时间:小时
- upload_hour = v_upload_time.split(" ")[-1].split(":")[0]
- url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "play_src": "1",
- "profile_id": int(p_id),
- "profile_mid": int(p_mid),
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
- "!400x400r/crop/400x400/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
- "/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "no_comments": True,
- "no_follow": True,
- "vid": v_id,
- "hot_l1_comment": True,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "dynamicSharePage"
- }
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- }
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
- hour_play_cnt = r.json()["data"]["play_pv"]
- Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
- # 固定时间获取符合规则的视频,写入云文档:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=ba0da4
- update_hour = datetime.datetime.now()
- if int(time.time()) - v_time >= 172800:
- Common.logger(log_type).info("抓取时间超过 2 天")
- return
- elif upload_data == today and update_hour.hour == 10 and int(upload_hour) <= 10:
- Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:10点 and 抓取时间<=10点")
- # 当天 10:00 视频播放量
- ten_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 10:00 视频播放量:{}", ten_hour_play_cnt)
- # 10:00 的上升榜写入数据
- values = int(ten_hour_play_cnt) - int(v_play_cnt)
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "L" + str(i + 1) + ":" + "L" + str(i + 1), [[values]])
- Common.logger(log_type).info("10:00数据更新成功:{}", values)
- elif upload_data == today and update_hour.hour == 15 and int(upload_hour) <= 10:
- Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:15点 and 抓取时间<=10点")
- # 当天 15:00 视频播放量
- fifteen_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
- # 当天 10:00 上升的数据
- if update_hour_sheet[i][11] is None:
- ten_up_cnt = 0
- else:
- ten_up_cnt = update_hour_sheet[i][11]
- # 15:00 的上升榜写入数据
- values = int(fifteen_hour_play_cnt) - (int(v_play_cnt) + int(ten_up_cnt))
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "M" + str(i + 1) + ":" + "M" + str(i + 1), [[values]])
- Common.logger(log_type).info("15:00数据更新成功:{}", values)
- elif upload_data == today and update_hour.hour == 15 and 10 < int(upload_hour) <= 15:
- Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:15点 and 10<抓取时间<=15点")
- # 当天 15:00 视频播放量
- fifteen_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
- # 15:00 的上升榜写入数据
- values = int(fifteen_hour_play_cnt) - int(v_play_cnt)
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "M" + str(i + 1) + ":" + "M" + str(i + 1), [[values]])
- Common.logger(log_type).info("15:00数据更新成功:{}", values)
- elif upload_data == today and update_hour.hour == 20 and int(upload_hour) <= 10:
- Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 抓取时间<=10点")
- # 当天 20:00 视频播放量
- twenty_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
- # 当天 10:00 上升的数据
- if update_hour_sheet[i][11] is None:
- ten_up_cnt = 0
- else:
- ten_up_cnt = update_hour_sheet[i][11]
- # 当天 15:00 上升的数据
- if update_hour_sheet[i][12] is None:
- fifteen_up_cnt = 0
- else:
- fifteen_up_cnt = update_hour_sheet[i][12]
- # 20:00 的上升榜写入数据
- values = int(twenty_hour_play_cnt) - (
- int(v_play_cnt) + int(ten_up_cnt) + int(fifteen_up_cnt))
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
- Common.logger(log_type).info("20:00数据更新成功:{}", values)
- elif upload_data == today and update_hour.hour == 20 and 10 < int(upload_hour) <= 15:
- Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 10<抓取时间<=15点")
- # 当天 20:00 视频播放量
- twenty_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
- # 当天 15:00 上升的数据
- if update_hour_sheet[i][12] is None:
- fifteen_up_cnt = 0
- else:
- fifteen_up_cnt = update_hour_sheet[i][12]
- # 20:00 的上升榜写入数据
- values = int(twenty_hour_play_cnt) - (int(v_play_cnt) + int(fifteen_up_cnt))
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
- Common.logger(log_type).info("20:00数据更新成功:{}", values)
- elif upload_data == today and update_hour.hour == 20 and 15 < int(upload_hour) <= 20:
- Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 15<抓取时间<=20点")
- # 当天 20:00 视频播放量
- twenty_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
- # 20:00 的上升榜写入数据
- values = int(twenty_hour_play_cnt) - int(v_play_cnt)
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
- Common.logger(log_type).info("20:00数据更新成功:{}", values)
- elif (upload_data == yesterday or upload_data == before_yesterday) \
- and update_hour.hour == 10:
- Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:10点")
- # 当天 10:00 视频播放量
- ten_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 10:00 视频播放量:{}", ten_hour_play_cnt)
- # 10:00 的上升榜写入数据
- values = int(ten_hour_play_cnt) - int(v_play_cnt)
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "L" + str(i + 1) + ":" + "L" + str(i + 1), [[values]])
- Common.logger(log_type).info("10:00数据更新成功:{}", values)
- elif (upload_data == yesterday or upload_data == before_yesterday) \
- and update_hour.hour == 15:
- Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:15点")
- # 当天 15:00 视频播放量
- fifteen_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
- # 当天 10:00 上升的数据
- if update_hour_sheet[i][11] is None:
- ten_up_cnt = 0
- else:
- ten_up_cnt = update_hour_sheet[i][11]
- # 15:00 的上升榜写入数据
- values = int(fifteen_hour_play_cnt) - (int(v_play_cnt) + int(ten_up_cnt))
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "M" + str(i + 1) + ":" + "M" + str(i + 1), [[values]])
- Common.logger(log_type).info("15:00数据更新成功:{}", values)
- elif (upload_data == yesterday or upload_data == before_yesterday) \
- and update_hour.hour == 20:
- Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:20点")
- # 当天 20:00 视频播放量
- twenty_hour_play_cnt = hour_play_cnt
- Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
- # 当天 10:00 上升的数据
- if update_hour_sheet[i][11] is None:
- ten_up_cnt = 0
- else:
- ten_up_cnt = update_hour_sheet[i][11]
- # 当天 15:00 上升的数据
- if update_hour_sheet[i][12] is None:
- fifteen_up_cnt = 0
- else:
- fifteen_up_cnt = update_hour_sheet[i][12]
- # 20:00 的上升榜写入数据
- values = int(twenty_hour_play_cnt) - (
- int(v_play_cnt) + int(ten_up_cnt) + int(fifteen_up_cnt))
- time.sleep(1)
- Feishu.update_values(
- log_type, "xiaoniangao", "ba0da4",
- "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
- Common.logger(log_type).info("20:00数据更新成功:{}", values)
- except Exception as e:
- Common.logger(log_type).error("视频详情:{},异常:{}", v_title, e)
- except Exception as e:
- Common.logger(log_type).error("获取小时榜数据异常:{}", e)
- # 下载/上传
- @classmethod
- def download_and_publish(cls, log_type):
- """
- 1.从云文档中去重: https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
- 2.从云文档中下载符合规则的视频:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
- 2.1 当日 10:00 or 15:00 or 20:00 视频播放量上升 > 5000
- 2.2 当日 10:00 and 15:00 视频播放量上升 > 2000
- 2.3 当日 15:00 and 20:00 视频播放量上升 > 2000
- 2.4 昨日 20:00 and 今日 10:00 视频播放量上升 > 2000
- 3.上传
- """
- try:
- time.sleep(1)
- download_hour_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4")
- if len(download_hour_sheet) == 2:
- Common.logger(log_type).info("当前工作表无数据")
- else:
- for i in range(2, len(download_hour_sheet) + 1):
- Common.logger(log_type).info("分析第:{}行视频信息是否符合下载规则", i + 1)
- # 略过空行
- if download_hour_sheet[i][0] is None \
- or download_hour_sheet[i][1] is None or download_hour_sheet[i][2] is None:
- Common.logger(log_type).info("空行,略过")
- else:
- # 今日 10:00 数据上升量
- if download_hour_sheet[i][11] is None:
- ten_cnt = 0
- else:
- ten_cnt = download_hour_sheet[i][11]
- # 今日 15:00 数据上升量
- if download_hour_sheet[i][12] is None:
- fifteen_cnt = 0
- else:
- fifteen_cnt = download_hour_sheet[i][12]
- # 今日 20:00 数据上升量
- if download_hour_sheet[i][13] is None:
- twenty_cnt = 0
- else:
- twenty_cnt = download_hour_sheet[i][13]
- # 昨日 20:00 数据上升量
- if download_hour_sheet[i][16] is None:
- yesterday_twenty_cnt = 0
- else:
- yesterday_twenty_cnt = download_hour_sheet[i][16]
- # 视频标题
- v_title = download_hour_sheet[i][3]
- Common.logger(log_type).info("video_title:{}", v_title)
- # 视频 ID
- v_id = download_hour_sheet[i][2]
- Common.logger(log_type).info("video_id:{}", v_id)
- # profile_id,用户 ID
- p_id = download_hour_sheet[i][0]
- # 视频时长
- v_duration = download_hour_sheet[i][5]
- # profile_mid
- p_mid = download_hour_sheet[i][1]
- Common.logger(log_type).info("10:00 / 15:00 / 20:00 上升量: {} / {} / {}",
- ten_cnt, fifteen_cnt, twenty_cnt)
- # 抓取时间
- v_upload_time = download_hour_sheet[i][9]
- v_send_time = int(time.mktime(time.strptime(v_upload_time, "%Y/%m/%d %H:%M:%S")))
- # 播放量
- v_play_cnt = download_hour_sheet[i][10]
- # # 判断视频 ID 长度,不大于 13 位
- # if len(str(v_id)) > 13:
- # Common.logger(log_type).info("视频ID长度大于13位:{}", v_id)
- #
- # el
- if int(time.time()) - int(v_send_time) >= 259200:
- Common.logger(log_type).info("抓取时间超过 3 天")
- return
- # 判断视频时长:1-10min
- elif int(v_duration) < 40:
- Common.logger(log_type).info("视频时长小于 40s")
- # # 过滤带字母的视频ID
- # elif any(word if word in v_id else False for word in cls.sensitive_videoid_words()) is True:
- # Common.logger(log_type).info("视频ID带字母:{}".format(v_id))
- # 从云文档中去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
- elif v_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2") for j in i]:
- Common.logger(log_type).info("该视频已下载:{}", v_title)
- # 播放量大于 100000,直接下载
- elif int(v_play_cnt) >= 100000:
- Common.logger(log_type).info("播放量:{} >= 100000,满足下载规则,开始下载视频", v_play_cnt)
- try:
- url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "play_src": "1",
- "profile_id": int(p_id),
- "profile_mid": int(p_mid),
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
- "!400x400r/crop/400x400/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
- "/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "no_comments": True,
- "no_follow": True,
- "vid": v_id,
- "hot_l1_comment": True,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "dynamicSharePage"
- }
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- }
- }
- urllib3.disable_warnings()
- r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
- hour_play_cnt = r.json()["data"]["play_pv"]
- hour_cover_url = r.json()["data"]["url"]
- hour_video_url = r.json()["data"]["v_url"]
- hour_video_duration = r.json()["data"]["du"]
- hour_video_comment_cnt = r.json()["data"]["comment_count"]
- hour_video_like_cnt = r.json()["data"]["favor"]["total"]
- hour_video_share_cnt = r.json()["data"]["share"]
- hour_video_width = r.json()["data"]["w"]
- hour_video_height = r.json()["data"]["h"]
- hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
- hour_video_send_time = r.json()["data"]["t"]
- hour_user_name = r.json()["data"]["user"]["nick"]
- hour_head_url = r.json()["data"]["user"]["hurl"]
- Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
- # 下载封面
- Common.download_method(log_type, "cover", v_title, hour_cover_url)
- # 下载视频
- Common.download_method(log_type, "video", v_title, hour_video_url)
- # 保存视频信息至 "./videos/{download_video_title}/info.txt"
- with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
- f_a.write(str(v_id) + "\n" +
- str(v_title) + "\n" +
- str(int(int(hour_video_duration) / 1000)) + "\n" +
- str(hour_play_cnt) + "\n" +
- str(hour_video_comment_cnt) + "\n" +
- str(hour_video_like_cnt) + "\n" +
- str(hour_video_share_cnt) + "\n" +
- str(hour_video_resolution) + "\n" +
- str(hour_video_send_time) + "\n" +
- str(hour_user_name) + "\n" +
- str(hour_head_url) + "\n" +
- str(hour_video_url) + "\n" +
- str(hour_cover_url) + "\n" +
- str("90747742180aeb22c0fe3a3c6a38f3d9"))
- Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
- # 上传视频
- Common.logger(log_type).info("开始上传视频")
- our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
- our_video_id) + "/info"
- Common.logger(log_type).info("视频上传完成:{}", v_title)
- # 上传完成时间
- upload_time = int(time.time())
- # 保存视频信息到云文档
- Common.logger(log_type).info("添加视频到云文档:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
- # 视频信息写入云文档
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- "小时级上升榜",
- v_id,
- v_title,
- our_video_link,
- hour_play_cnt,
- hour_video_comment_cnt,
- hour_video_like_cnt,
- hour_video_share_cnt,
- int(hour_video_duration) / 1000,
- hour_video_resolution,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_user_name,
- p_id,
- p_mid,
- hour_head_url,
- hour_cover_url,
- hour_video_url]]
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
- # 保存视频信息到监控表
- Common.logger(log_type).info("添加视频到监控表:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
- # 视频信息写入监控表
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- v_id,
- v_title,
- our_video_link,
- p_id,
- p_mid,
- hour_user_name,
- int(hour_video_duration) / 1000,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_play_cnt]]
- time.sleep(1)
- Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
- except Exception as e:
- Common.logger(log_type).error("下载视频异常:{}", e)
- # 上升榜判断逻辑,任意时间段上升量>=5000,连续两个时间段上升量>=2000
- elif int(ten_cnt) >= 5000 or int(fifteen_cnt) >= 5000 or int(twenty_cnt) >= 5000:
- Common.logger(log_type).info("10:00 or 15:00 or 20:00 数据上升量:{} or {} or {} >= 5000",
- ten_cnt, fifteen_cnt, twenty_cnt)
- Common.logger(log_type).info("满足下载规则,开始下载视频")
- try:
- url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "play_src": "1",
- "profile_id": int(p_id),
- "profile_mid": int(p_mid),
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
- "!400x400r/crop/400x400/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
- "/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "no_comments": True,
- "no_follow": True,
- "vid": v_id,
- "hot_l1_comment": True,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "dynamicSharePage"
- }
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- }
- }
- urllib3.disable_warnings()
- r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
- hour_play_cnt = r.json()["data"]["play_pv"]
- hour_cover_url = r.json()["data"]["url"]
- hour_video_url = r.json()["data"]["v_url"]
- hour_video_duration = r.json()["data"]["du"]
- hour_video_comment_cnt = r.json()["data"]["comment_count"]
- hour_video_like_cnt = r.json()["data"]["favor"]["total"]
- hour_video_share_cnt = r.json()["data"]["share"]
- hour_video_width = r.json()["data"]["w"]
- hour_video_height = r.json()["data"]["h"]
- hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
- hour_video_send_time = r.json()["data"]["t"]
- hour_user_name = r.json()["data"]["user"]["nick"]
- hour_head_url = r.json()["data"]["user"]["hurl"]
- Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
- # 下载封面
- Common.download_method(log_type, "cover", v_title, hour_cover_url)
- # 下载视频
- Common.download_method(log_type, "video", v_title, hour_video_url)
- # 保存视频信息至 "./videos/{download_video_title}/info.txt"
- with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
- f_a.write(str(v_id) + "\n" +
- str(v_title) + "\n" +
- str(int(int(hour_video_duration) / 1000)) + "\n" +
- str(hour_play_cnt) + "\n" +
- str(hour_video_comment_cnt) + "\n" +
- str(hour_video_like_cnt) + "\n" +
- str(hour_video_share_cnt) + "\n" +
- str(hour_video_resolution) + "\n" +
- str(hour_video_send_time) + "\n" +
- str(hour_user_name) + "\n" +
- str(hour_head_url) + "\n" +
- str(hour_video_url) + "\n" +
- str(hour_cover_url) + "\n" +
- str("90747742180aeb22c0fe3a3c6a38f3d9"))
- Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
- # 上传视频
- Common.logger(log_type).info("开始上传视频")
- our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
- our_video_id) + "/info"
- Common.logger(log_type).info("视频上传完成:{}", v_title)
- # 上传完成时间
- upload_time = int(time.time())
- # 保存视频信息到云文档
- Common.logger(log_type).info("添加视频到云文档:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
- # 视频信息写入云文档
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- "小时级上升榜",
- v_id,
- v_title,
- our_video_link,
- hour_play_cnt,
- hour_video_comment_cnt,
- hour_video_like_cnt,
- hour_video_share_cnt,
- int(hour_video_duration) / 1000,
- hour_video_resolution,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_user_name,
- p_id,
- p_mid,
- hour_head_url,
- hour_cover_url,
- hour_video_url]]
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
- # 保存视频信息到监控表
- Common.logger(log_type).info("添加视频到监控表:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
- # 视频信息写入监控表
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- v_id,
- v_title,
- our_video_link,
- p_id,
- p_mid,
- hour_user_name,
- int(hour_video_duration) / 1000,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_play_cnt]]
- time.sleep(1)
- Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
- except Exception as e:
- Common.logger(log_type).error("下载视频异常:{}", e)
- elif int(ten_cnt) >= 2000 and int(fifteen_cnt) >= 2000:
- Common.logger(log_type).info(
- "10:00 and 15:00 数据上升量:{} and {} >= 2000", ten_cnt, fifteen_cnt)
- Common.logger(log_type).info("满足下载规则,开始下载视频")
- try:
- url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "play_src": "1",
- "profile_id": int(p_id),
- "profile_mid": int(p_mid),
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
- "!400x400r/crop/400x400/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
- "/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "no_comments": True,
- "no_follow": True,
- "vid": v_id,
- "hot_l1_comment": True,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "dynamicSharePage"
- }
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- }
- }
- urllib3.disable_warnings()
- r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
- hour_play_cnt = r.json()["data"]["play_pv"]
- hour_cover_url = r.json()["data"]["url"]
- hour_video_url = r.json()["data"]["v_url"]
- hour_video_duration = r.json()["data"]["du"]
- hour_video_comment_cnt = r.json()["data"]["comment_count"]
- hour_video_like_cnt = r.json()["data"]["favor"]["total"]
- hour_video_share_cnt = r.json()["data"]["share"]
- hour_video_width = r.json()["data"]["w"]
- hour_video_height = r.json()["data"]["h"]
- hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
- hour_video_send_time = r.json()["data"]["t"]
- hour_user_name = r.json()["data"]["user"]["nick"]
- hour_head_url = r.json()["data"]["user"]["hurl"]
- Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
- # 下载封面
- Common.download_method(log_type, "cover", v_title, hour_cover_url)
- # 下载视频
- Common.download_method(log_type, "video", v_title, hour_video_url)
- # 保存视频信息至 "./videos/{download_video_title}/info.txt"
- with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
- f_a.write(str(v_id) + "\n" +
- str(v_title) + "\n" +
- str(int(int(hour_video_duration) / 1000)) + "\n" +
- str(hour_play_cnt) + "\n" +
- str(hour_video_comment_cnt) + "\n" +
- str(hour_video_like_cnt) + "\n" +
- str(hour_video_share_cnt) + "\n" +
- str(hour_video_resolution) + "\n" +
- str(hour_video_send_time) + "\n" +
- str(hour_user_name) + "\n" +
- str(hour_head_url) + "\n" +
- str(hour_video_url) + "\n" +
- str(hour_cover_url) + "\n" +
- str("90747742180aeb22c0fe3a3c6a38f3d9"))
- Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
- # 上传
- Common.logger(log_type).info("开始上传视频")
- our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
- our_video_id) + "/info"
- Common.logger(log_type).info("视频上传完成:{}", v_title)
- # 上传完成时间
- upload_time = int(time.time())
- # 保存视频信息到云文档
- Common.logger(log_type).info("添加视频到云文档:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
- # 视频信息写入云文档
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- "小时级上升榜",
- v_id,
- v_title,
- our_video_link,
- hour_play_cnt,
- hour_video_comment_cnt,
- hour_video_like_cnt,
- hour_video_share_cnt,
- int(hour_video_duration) / 1000,
- hour_video_resolution,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_user_name,
- p_id,
- p_mid,
- hour_head_url,
- hour_cover_url,
- hour_video_url]]
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
- # 保存视频信息到监控表
- Common.logger(log_type).info("添加视频到监控表:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
- # 视频信息写入监控表
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- v_id,
- v_title,
- our_video_link,
- p_id,
- p_mid,
- hour_user_name,
- int(hour_video_duration) / 1000,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_play_cnt]]
- time.sleep(1)
- Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
- except Exception as e:
- Common.logger(log_type).error("下载视频异常:{}", e)
- elif int(fifteen_cnt) >= 2000 and int(twenty_cnt) >= 2000:
- Common.logger(log_type).info(
- "15:00 and 20:00 数据上升量:{} and {} >= 2000", fifteen_cnt, twenty_cnt)
- Common.logger(log_type).info("满足下载规则,开始下载视频")
- try:
- url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "play_src": "1",
- "profile_id": int(p_id),
- "profile_mid": int(p_mid),
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
- "!400x400r/crop/400x400/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
- "/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "no_comments": True,
- "no_follow": True,
- "vid": v_id,
- "hot_l1_comment": True,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "dynamicSharePage"
- }
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- }
- }
- urllib3.disable_warnings()
- r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
- hour_play_cnt = r.json()["data"]["play_pv"]
- hour_cover_url = r.json()["data"]["url"]
- hour_video_url = r.json()["data"]["v_url"]
- hour_video_duration = r.json()["data"]["du"]
- hour_video_comment_cnt = r.json()["data"]["comment_count"]
- hour_video_like_cnt = r.json()["data"]["favor"]["total"]
- hour_video_share_cnt = r.json()["data"]["share"]
- hour_video_width = r.json()["data"]["w"]
- hour_video_height = r.json()["data"]["h"]
- hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
- hour_video_send_time = r.json()["data"]["t"]
- hour_user_name = r.json()["data"]["user"]["nick"]
- hour_head_url = r.json()["data"]["user"]["hurl"]
- Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
- # 下载封面
- Common.download_method(log_type, "cover", v_title, hour_cover_url)
- # 下载视频
- Common.download_method(log_type, "video", v_title, hour_video_url)
- # 保存视频信息至 "./videos/{download_video_title}/info.txt"
- with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
- f_a.write(str(v_id) + "\n" +
- str(v_title) + "\n" +
- str(int(int(hour_video_duration) / 1000)) + "\n" +
- str(hour_play_cnt) + "\n" +
- str(hour_video_comment_cnt) + "\n" +
- str(hour_video_like_cnt) + "\n" +
- str(hour_video_share_cnt) + "\n" +
- str(hour_video_resolution) + "\n" +
- str(hour_video_send_time) + "\n" +
- str(hour_user_name) + "\n" +
- str(hour_head_url) + "\n" +
- str(hour_video_url) + "\n" +
- str(hour_cover_url) + "\n" +
- str("90747742180aeb22c0fe3a3c6a38f3d9"))
- Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
- # 上传
- Common.logger(log_type).info("开始上传视频")
- our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
- our_video_id) + "/info"
- Common.logger(log_type).info("视频上传完成:{}", v_title)
- # 上传完成时间
- upload_time = int(time.time())
- # 保存视频信息到云文档
- Common.logger(log_type).info("添加视频到云文档:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
- # 视频信息写入云文档
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- "小时级上升榜",
- v_id,
- v_title,
- our_video_link,
- hour_play_cnt,
- hour_video_comment_cnt,
- hour_video_like_cnt,
- hour_video_share_cnt,
- int(hour_video_duration) / 1000,
- hour_video_resolution,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_user_name,
- p_id,
- p_mid,
- hour_head_url,
- hour_cover_url,
- hour_video_url]]
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
- # 保存视频信息到监控表
- Common.logger(log_type).info("添加视频到监控表:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
- # 视频信息写入监控表
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- v_id,
- v_title,
- our_video_link,
- p_id,
- p_mid,
- hour_user_name,
- int(hour_video_duration) / 1000,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_play_cnt]]
- time.sleep(1)
- Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
- except Exception as e:
- Common.logger(log_type).error("下载视频异常:{}", e)
- elif int(yesterday_twenty_cnt) >= 2000 and int(ten_cnt) >= 2000:
- Common.logger(log_type).info("昨日20:00 and 今日10:00 数据上升量:{} and {} >= 2000",
- yesterday_twenty_cnt, ten_cnt)
- Common.logger(log_type).info("满足下载规则,开始下载视频")
- try:
- url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
- headers = {
- "x-b3-traceid": cls.hour_x_b3_traceid,
- "X-Token-Id": cls.hour_x_token_id,
- "uid": cls.hour_uid,
- "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(0x18001432) NetType/WIFI Language/zh_CN',
- "Referer": cls.hour_referer
- }
- data = {
- "play_src": "1",
- "profile_id": int(p_id),
- "profile_mid": int(p_mid),
- "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
- "!400x400r/crop/400x400/interlace/1/format/jpg",
- "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
- "/!80x80r/crop/80x80/interlace/1/format/jpg",
- "share_width": 625,
- "share_height": 500,
- "no_comments": True,
- "no_follow": True,
- "vid": v_id,
- "hot_l1_comment": True,
- "token": cls.hour_token,
- "uid": cls.hour_uid,
- "proj": "ma",
- "wx_ver": "8.0.20",
- "code_ver": "3.62.0",
- "log_common_params": {
- "e": [{
- "data": {
- "page": "dynamicSharePage"
- }
- }],
- "ext": {
- "brand": "iPhone",
- "device": "iPhone 11",
- "os": "iOS 14.7.1",
- "weixinver": "8.0.20",
- "srcver": "2.24.3",
- "net": "wifi",
- "scene": "1089"
- },
- "pj": "1",
- "pf": "2",
- "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
- }
- }
- urllib3.disable_warnings()
- r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
- hour_play_cnt = r.json()["data"]["play_pv"]
- hour_cover_url = r.json()["data"]["url"]
- hour_video_url = r.json()["data"]["v_url"]
- hour_video_duration = r.json()["data"]["du"]
- hour_video_comment_cnt = r.json()["data"]["comment_count"]
- hour_video_like_cnt = r.json()["data"]["favor"]["total"]
- hour_video_share_cnt = r.json()["data"]["share"]
- hour_video_width = r.json()["data"]["w"]
- hour_video_height = r.json()["data"]["h"]
- hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
- hour_video_send_time = r.json()["data"]["t"]
- hour_user_name = r.json()["data"]["user"]["nick"]
- hour_head_url = r.json()["data"]["user"]["hurl"]
- Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
- # 下载封面
- Common.download_method(log_type, "cover", v_title, hour_cover_url)
- # 下载视频
- Common.download_method(log_type, "video", v_title, hour_video_url)
- # 保存视频信息至 "./videos/{download_video_title}/info.txt"
- with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
- f_a.write(str(v_id) + "\n" +
- str(v_title) + "\n" +
- str(int(int(hour_video_duration) / 1000)) + "\n" +
- str(hour_play_cnt) + "\n" +
- str(hour_video_comment_cnt) + "\n" +
- str(hour_video_like_cnt) + "\n" +
- str(hour_video_share_cnt) + "\n" +
- str(hour_video_resolution) + "\n" +
- str(hour_video_send_time) + "\n" +
- str(hour_user_name) + "\n" +
- str(hour_head_url) + "\n" +
- str(hour_video_url) + "\n" +
- str(hour_cover_url) + "\n" +
- str("90747742180aeb22c0fe3a3c6a38f3d9"))
- Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
- # 上传
- Common.logger(log_type).info("开始上传视频")
- our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
- our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
- our_video_id) + "/info"
- Common.logger(log_type).info("视频上传完成:{}", v_title)
- # 上传完成时间
- upload_time = int(time.time())
- # 保存视频信息到云文档
- Common.logger(log_type).info("添加视频到云文档:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
- # 视频信息写入云文档
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- "小时级上升榜",
- v_id,
- v_title,
- our_video_link,
- hour_play_cnt,
- hour_video_comment_cnt,
- hour_video_like_cnt,
- hour_video_share_cnt,
- int(hour_video_duration) / 1000,
- hour_video_resolution,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_user_name,
- p_id,
- p_mid,
- hour_head_url,
- hour_cover_url,
- hour_video_url]]
- time.sleep(1)
- Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
- # 保存视频信息到监控表
- Common.logger(log_type).info("添加视频到监控表:{}", v_title)
- # 插入空行
- time.sleep(1)
- Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
- # 视频信息写入监控表
- values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
- v_id,
- v_title,
- our_video_link,
- p_id,
- p_mid,
- hour_user_name,
- int(hour_video_duration) / 1000,
- time.strftime("%Y/%m/%d %H:%M:%S",
- time.localtime(int(hour_video_send_time) / 1000)),
- hour_play_cnt]]
- time.sleep(1)
- Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
- except Exception as e:
- Common.logger(log_type).error("下载视频异常:{}", e)
- else:
- Common.logger(log_type).info("上升量不满足下载规则")
- except Exception as e:
- Common.logger(log_type).error("下载视频时,获取飞书信息异常:{}", e)
- if __name__ == "__main__":
- HourList.get_hour_list_feeds("hour")
- # HourList.get_expression("hour")
|