hour_list.py 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/5/16
  4. import datetime
  5. import os
  6. import sys
  7. import time
  8. import requests
  9. import urllib3
  10. sys.path.append(os.getcwd())
  11. from main.common import Common
  12. from main.feishu_lib import Feishu
  13. from main.publish import Publish
  14. proxies = {"http": None, "https": None}
  15. class HourList:
  16. hour_x_b3_traceid = Feishu.get_range_value("hour", "xiaoniangao", "dzcWHw", "B3:B3")[0]
  17. hour_x_token_id = Feishu.get_range_value("hour", "xiaoniangao", "dzcWHw", "B4:B4")[0]
  18. hour_referer = Feishu.get_range_value("hour", "xiaoniangao", "dzcWHw", "B5:B5")[0][0]["link"]
  19. hour_uid = Feishu.get_range_value("hour", "xiaoniangao", "dzcWHw", "B6:B6")[0]
  20. hour_token = Feishu.get_range_value("hour", "xiaoniangao", "dzcWHw", "B7:B7")[0]
  21. # 过滤敏感词
  22. @classmethod
  23. def sensitive_words(cls, log_type):
  24. # 敏感词库列表
  25. word_list = []
  26. # 从云文档读取所有敏感词,添加到词库列表
  27. time.sleep(1)
  28. lists = Feishu.get_values_batch(log_type, "xiaoniangao", "DRAnZh")
  29. for i in lists:
  30. for j in i:
  31. # 过滤空的单元格内容
  32. if j is None:
  33. pass
  34. else:
  35. word_list.append(j)
  36. return word_list
  37. # 视频ID过滤字母
  38. @classmethod
  39. def sensitive_videoid_words(cls):
  40. # 字母列表
  41. words_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  42. "t", "u", "v", "w", "x", "y", "z",
  43. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
  44. "T", "U", "V", "W", "X", "Y", "Z"]
  45. return words_list
  46. # 基础门槛规则
  47. @staticmethod
  48. def download_rule(d_duration, d_width, d_height, d_play_cnt, d_like_cnt, d_share_cnt, d_send_time):
  49. """
  50. 下载视频的基本规则
  51. :param d_duration: 时长
  52. :param d_width: 宽
  53. :param d_height: 高
  54. :param d_play_cnt: 播放量
  55. :param d_like_cnt: 点赞量
  56. :param d_share_cnt: 分享量
  57. :param d_send_time: 发布时间
  58. :return: 满足规则,返回 True;反之,返回 False
  59. """
  60. # 视频时长
  61. if int(float(d_duration)) >= 40:
  62. # 宽或高
  63. if int(d_width) >= 0 or int(d_height) >= 0:
  64. # 播放量
  65. if int(d_play_cnt) >= 4000:
  66. # 点赞量
  67. if int(d_like_cnt) >= 0:
  68. # 分享量
  69. if int(d_share_cnt) >= 0:
  70. # 发布时间 <= 10 天
  71. if int(time.time()) - int(d_send_time)/1000 <= 864000:
  72. return True
  73. else:
  74. return False
  75. else:
  76. return False
  77. else:
  78. return False
  79. else:
  80. return False
  81. return False
  82. return False
  83. # 检查是否有今日的上升榜日期
  84. @classmethod
  85. def check_hour_list_data(cls, log_type, date):
  86. # 判断J1单元格的日期是否为今天
  87. time.sleep(1)
  88. if Feishu.get_range_value(log_type, "xiaoniangao", "ba0da4", "L1:N1")[0] != date:
  89. # 插入3列 L1:N1,并写入日期和时间数据
  90. values = [[date], ["10:00", "15:00", "20:00"]]
  91. time.sleep(1)
  92. Feishu.insert_columns(log_type, "xiaoniangao", "ba0da4", "COLUMNS", 11, 14)
  93. time.sleep(1)
  94. Feishu.update_values(log_type, "xiaoniangao", "ba0da4", "L1:N2", values)
  95. time.sleep(1)
  96. Feishu.merge_cells(log_type, "xiaoniangao", "ba0da4", "L1:N1")
  97. Common.logger(log_type).info("插入今天日期成功")
  98. else:
  99. Common.logger(log_type).info("今日上升榜日期已存在")
  100. # 获取列表
  101. @classmethod
  102. def get_hour_list_feeds(cls, log_type):
  103. """
  104. 1.从列表获取视频,7 天内,播放量>=5000
  105. 2.时长 1-10min
  106. 3.每天10:00、15:00、20:00 把符合规则的视频,写入云文档
  107. https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
  108. """
  109. url = "https://kapi.xiaoniangao.cn/trends/get_recommend_trends"
  110. headers = {
  111. "x-b3-traceid": cls.hour_x_b3_traceid,
  112. "X-Token-Id": cls.hour_x_token_id,
  113. "uid": cls.hour_uid,
  114. "content-type": "application/json",
  115. "Accept-Encoding": "gzip,compress,br,deflate",
  116. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  117. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  118. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  119. "Referer": cls.hour_referer
  120. }
  121. data = {
  122. "log_params": {
  123. "page": "discover_rec",
  124. "common": {
  125. "brand": "iPhone",
  126. "device": "iPhone 11",
  127. "os": "iOS 14.7.1",
  128. "weixinver": "8.0.20",
  129. "srcver": "2.24.2",
  130. "net": "wifi",
  131. "scene": 1089
  132. }
  133. },
  134. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!750x500r/crop/750x500/interlace/1/format/jpg",
  135. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!80x80r/crop/80x80/interlace/1/format/jpg",
  136. "share_width": 625,
  137. "share_height": 500,
  138. "ext": {
  139. "fmid": 0,
  140. "items": {}
  141. },
  142. "app": "xng",
  143. "rec_scene": "discover_rec",
  144. "log_common_params": {
  145. "e": [{
  146. "data": {
  147. "page": "discoverIndexPage",
  148. "topic": "recommend"
  149. },
  150. "ab": {}
  151. }],
  152. "ext": {
  153. "brand": "iPhone",
  154. "device": "iPhone 11",
  155. "os": "iOS 14.7.1",
  156. "weixinver": "8.0.20",
  157. "srcver": "2.24.3",
  158. "net": "wifi",
  159. "scene": "1089"
  160. },
  161. "pj": "1",
  162. "pf": "2",
  163. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  164. },
  165. "refresh": False,
  166. "token": cls.hour_token,
  167. "uid": cls.hour_uid,
  168. "proj": "ma",
  169. "wx_ver": "8.0.20",
  170. "code_ver": "3.62.0"
  171. }
  172. try:
  173. urllib3.disable_warnings()
  174. r = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
  175. if "data" not in r.json():
  176. Common.logger(log_type).warning("获取视频feeds错误:{}", r.text)
  177. elif "list" not in r.json()["data"]:
  178. Common.logger(log_type).warning("获取视频feeds无数据,休眠10s:{}", r.json()["data"])
  179. else:
  180. # 视频列表数据
  181. feeds = r.json()["data"]["list"]
  182. for i in range(len(feeds)):
  183. # 标题
  184. if "title" in feeds[i]:
  185. video_title = feeds[i]["title"].strip().replace("\n", "") \
  186. .replace("/", "").replace("\r", "").replace("#", "") \
  187. .replace(".", "。").replace("\\", "").replace("&NBSP", "") \
  188. .replace(":", "").replace("*", "").replace("?", "") \
  189. .replace("?", "").replace('"', "").replace("<", "") \
  190. .replace(">", "").replace("|", "").replace(" ", "")
  191. else:
  192. video_title = 0
  193. # 视频 ID
  194. if "vid" in feeds[i]:
  195. video_id = feeds[i]["vid"]
  196. else:
  197. video_id = 0
  198. # 播放量
  199. if "play_pv" in feeds[i]:
  200. video_play_cnt = feeds[i]["play_pv"]
  201. else:
  202. video_play_cnt = 0
  203. # 点赞量
  204. if "favor" in feeds[i]:
  205. video_like_cnt = feeds[i]["favor"]["total"]
  206. else:
  207. video_like_cnt = 0
  208. # 分享量
  209. if "share" in feeds[i]:
  210. video_share_cnt = feeds[i]["share"]
  211. else:
  212. video_share_cnt = 0
  213. # # 评论量
  214. # if "comment_count" in feeds[i]:
  215. # video_comment_cnt = feeds[i]["comment_count"]
  216. # else:
  217. # video_comment_cnt = 0
  218. # 时长
  219. if "du" in feeds[i]:
  220. video_duration = int(feeds[i]["du"] / 1000)
  221. else:
  222. video_duration = 0
  223. # 宽和高
  224. if "w" or "h" in feeds[i]:
  225. video_width = feeds[i]["w"]
  226. video_height = feeds[i]["h"]
  227. else:
  228. video_width = 0
  229. video_height = 0
  230. # 发布时间
  231. if "t" in feeds[i]:
  232. video_send_time = feeds[i]["t"]
  233. else:
  234. video_send_time = 0
  235. # 用户名 / 头像
  236. if "user" in feeds[i]:
  237. user_name = feeds[i]["user"]["nick"].strip().replace("\n", "") \
  238. .replace("/", "").replace("快手", "").replace(" ", "") \
  239. .replace(" ", "").replace("&NBSP", "").replace("\r", "")
  240. head_url = feeds[i]["user"]["hurl"]
  241. else:
  242. user_name = 0
  243. head_url = 0
  244. # 用户 ID
  245. profile_id = feeds[i]["id"]
  246. # 用户 mid
  247. profile_mid = feeds[i]["user"]["mid"]
  248. # 视频封面
  249. if "url" in feeds[i]:
  250. cover_url = feeds[i]["url"]
  251. else:
  252. cover_url = 0
  253. # 视频播放地址
  254. if "v_url" in feeds[i]:
  255. video_url = feeds[i]["v_url"]
  256. else:
  257. video_url = 0
  258. Common.logger(log_type).info("标题:{}", video_title)
  259. Common.logger(log_type).info("视频ID:{}", video_id)
  260. Common.logger(log_type).info("播放量:{}", video_play_cnt)
  261. # Common.logger(log_type).info("点赞量:{}", video_like_cnt)
  262. # Common.logger(log_type).info("分享量:{}", video_share_cnt)
  263. # Common.logger(log_type).info("评论数:{}", video_comment_cnt)
  264. Common.logger(log_type).info("时长:{}秒", video_duration)
  265. # Common.logger(log_type).info("宽高:{}*{}", video_width, video_height)
  266. Common.logger(log_type).info(
  267. "视频发布时间:{}", time.strftime(
  268. "%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
  269. Common.logger(log_type).info("用户名:{}", user_name)
  270. # Common.logger(log_type).info("用户头像:{}", head_url)
  271. # Common.logger(log_type).info("封面:{}", cover_url)
  272. Common.logger(log_type).info("播放地址:{}", video_url)
  273. # 过滤无效视频
  274. if video_title == 0 or video_id == 0 or video_duration == 0 \
  275. or video_send_time == 0 or user_name == 0 or head_url == 0 \
  276. or cover_url == 0 or video_url == 0:
  277. Common.logger(log_type).warning("无效视频")
  278. elif cls.download_rule(video_duration, video_width, video_height, video_play_cnt,
  279. video_like_cnt, video_share_cnt, video_send_time) is False:
  280. Common.logger(log_type).info("不满足基础门槛规则")
  281. # 过滤敏感词
  282. elif any(word if word in video_title else False for word in cls.sensitive_words(log_type)) is True:
  283. Common.logger(log_type).info("视频已中敏感词:{}".format(video_title))
  284. time.sleep(1)
  285. # 从云文档中去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
  286. elif video_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2") for j in i]:
  287. Common.logger(log_type).info("该视频已下载:{}", video_title)
  288. time.sleep(1)
  289. # 从云文档去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
  290. elif video_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4") for j in i]:
  291. Common.logger(log_type).info("该视频已保存过:{}", video_title)
  292. time.sleep(1)
  293. else:
  294. Common.logger(log_type).info("该视频未下载,添加至feeds中:{}".format(video_title))
  295. # feeds工作表,插入空行
  296. time.sleep(1)
  297. Feishu.insert_columns(log_type, "xiaoniangao", "ba0da4", "ROWS", 2, 3)
  298. # 获取当前时间
  299. get_feeds_time = int(time.time())
  300. # 看一看云文档,工作表中写入数据
  301. values = [[profile_id,
  302. profile_mid,
  303. video_id,
  304. video_title,
  305. user_name,
  306. video_duration,
  307. cover_url,
  308. video_url,
  309. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)),
  310. str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(get_feeds_time))),
  311. video_play_cnt]]
  312. # 等待 1s,防止操作云文档太频繁,导致报错
  313. time.sleep(1)
  314. Feishu.update_values(log_type, "xiaoniangao", "ba0da4", "A3:K3", values)
  315. except Exception as e:
  316. Common.logger(log_type).error("获取小时榜视频列表异常:{}", e)
  317. # 更新小时榜数据
  318. @classmethod
  319. def update_hour_list_data(cls, log_type, today, yesterday, before_yesterday):
  320. """
  321. 更新小时榜数据
  322. """
  323. try:
  324. time.sleep(1)
  325. if len(Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")) == 2:
  326. Common.logger(log_type).info("当前工作表无数据")
  327. else:
  328. time.sleep(1)
  329. for i in range(3, len(Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4"))+1):
  330. time.sleep(1)
  331. Common.logger(log_type).info("更新第:{}行视频信息", i)
  332. # 略过空行
  333. if Feishu.get_range_value(
  334. log_type, "xiaoniangao", "ba0da4", "D" + str(i) + ":" + "D" + str(i))[0] is None\
  335. and Feishu.get_range_value(
  336. log_type, "xiaoniangao", "ba0da4", "C"+str(i)+":"+"C"+str(i))[0] is None\
  337. and Feishu.get_range_value(
  338. log_type, "xiaoniangao", "ba0da4", "A"+str(i)+":"+"A"+str(i))[0] is None:
  339. Common.logger(log_type).info("空行,略过")
  340. else:
  341. # 视频标题
  342. time.sleep(1)
  343. v_title = Feishu.get_range_value(
  344. log_type, "xiaoniangao", "ba0da4", "D" + str(i) + ":" + "D" + str(i))[0]
  345. Common.logger(log_type).info("视频详情,video_title:{}", v_title)
  346. # 视频 ID
  347. time.sleep(1)
  348. v_id = Feishu.get_range_value(
  349. log_type, "xiaoniangao", "ba0da4", "C" + str(i) + ":" + "C" + str(i))[0]
  350. Common.logger(log_type).info("视频详情,video_id:{}", v_id)
  351. # profile_id,用户 ID
  352. time.sleep(1)
  353. p_id = Feishu.get_range_value(
  354. log_type, "xiaoniangao", "ba0da4", "A" + str(i) + ":" + "A" + str(i))[0]
  355. Common.logger(log_type).info("视频详情,profile_id:{}", p_id)
  356. # profile_mid
  357. time.sleep(1)
  358. p_mid = Feishu.get_range_value(
  359. log_type, "xiaoniangao", "ba0da4", "B" + str(i) + ":" + "B" + str(i))[0]
  360. Common.logger(log_type).info("视频详情,profile_mid:{}", p_mid)
  361. # 抓取时的播放量
  362. time.sleep(1)
  363. v_play_cnt = Feishu.get_range_value(
  364. log_type, "xiaoniangao", "ba0da4", "K" + str(i) + ":" + "K" + str(i))[0]
  365. Common.logger(log_type).info("视频详情,video_play_cnt:{}", v_play_cnt)
  366. # 抓取时间
  367. time.sleep(1)
  368. v_upload_time = Feishu.get_range_value(
  369. log_type, "xiaoniangao", "ba0da4", "J" + str(i) + ":" + "J" + str(i))[0]
  370. Common.logger(log_type).info("视频详情,video_send_time:{}", v_upload_time)
  371. # 抓取时间的时间戳格式(秒为单位)
  372. v_time = int(time.mktime(time.strptime(v_upload_time, "%Y-%m-%d %H:%M:%S")))
  373. # 抓取时间:日期
  374. upload_data = v_upload_time.split(" ")[0]
  375. # 抓取时间:小时
  376. upload_hour = v_upload_time.split(" ")[-1].split(":")[0]
  377. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  378. headers = {
  379. "x-b3-traceid": cls.hour_x_b3_traceid,
  380. "X-Token-Id": cls.hour_x_token_id,
  381. "uid": cls.hour_uid,
  382. "content-type": "application/json",
  383. "Accept-Encoding": "gzip,compress,br,deflate",
  384. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  385. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  386. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  387. "Referer": cls.hour_referer
  388. }
  389. data = {
  390. "play_src": "1",
  391. "profile_id": int(p_id),
  392. "profile_mid": int(p_mid),
  393. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  394. "!400x400r/crop/400x400/interlace/1/format/jpg",
  395. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  396. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  397. "share_width": 625,
  398. "share_height": 500,
  399. "no_comments": True,
  400. "no_follow": True,
  401. "vid": v_id,
  402. "hot_l1_comment": True,
  403. "token": cls.hour_token,
  404. "uid": cls.hour_uid,
  405. "proj": "ma",
  406. "wx_ver": "8.0.20",
  407. "code_ver": "3.62.0",
  408. "log_common_params": {
  409. "e": [{
  410. "data": {
  411. "page": "dynamicSharePage"
  412. }
  413. }],
  414. "ext": {
  415. "brand": "iPhone",
  416. "device": "iPhone 11",
  417. "os": "iOS 14.7.1",
  418. "weixinver": "8.0.20",
  419. "srcver": "2.24.3",
  420. "net": "wifi",
  421. "scene": "1089"
  422. },
  423. "pj": "1",
  424. "pf": "2",
  425. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  426. }
  427. }
  428. try:
  429. urllib3.disable_warnings()
  430. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  431. hour_play_cnt = r.json()["data"]["play_pv"]
  432. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  433. # 固定时间获取符合规则的视频,写入云文档:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=ba0da4
  434. update_hour = datetime.datetime.now()
  435. if int(time.time()) - v_time >= 172800:
  436. Common.logger(log_type).info("抓取时间超过 2 天")
  437. return
  438. elif upload_data == today and update_hour.hour == 10 and int(upload_hour) <= 10:
  439. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:10点 and 抓取时间<=10点")
  440. # 当天 10:00 视频播放量
  441. ten_hour_play_cnt = hour_play_cnt
  442. Common.logger(log_type).info("当天 10:00 视频播放量:{}", ten_hour_play_cnt)
  443. # 10:00 的上升榜写入数据
  444. values = int(ten_hour_play_cnt) - int(v_play_cnt)
  445. time.sleep(1)
  446. Feishu.update_values(
  447. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i), [[values]])
  448. Common.logger(log_type).info("10:00数据更新成功:{}", values)
  449. elif upload_data == today and update_hour.hour == 15 and int(upload_hour) <= 10:
  450. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:15点 and 抓取时间<=10点")
  451. # 当天 15:00 视频播放量
  452. fifteen_hour_play_cnt = hour_play_cnt
  453. Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
  454. # 当天 10:00 上升的数据
  455. time.sleep(1)
  456. if Feishu.get_range_value(
  457. log_type, "xiaoniangao", "ba0da4", "L"+str(i) + ":" + "L"+str(i))[0] is None:
  458. ten_up_cnt = 0
  459. else:
  460. ten_up_cnt = Feishu.get_range_value(
  461. log_type, "xiaoniangao", "ba0da4", "L"+str(i) + ":" + "L"+str(i))[0]
  462. # 15:00 的上升榜写入数据
  463. values = int(fifteen_hour_play_cnt) - (int(v_play_cnt) + int(ten_up_cnt))
  464. time.sleep(1)
  465. Feishu.update_values(
  466. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i), [[values]])
  467. Common.logger(log_type).info("15:00数据更新成功:{}", values)
  468. elif upload_data == today and update_hour.hour == 15 and 10 < int(upload_hour) <= 15:
  469. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:15点 and 10<抓取时间<=15点")
  470. # 当天 15:00 视频播放量
  471. fifteen_hour_play_cnt = hour_play_cnt
  472. Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
  473. # 15:00 的上升榜写入数据
  474. values = int(fifteen_hour_play_cnt) - int(v_play_cnt)
  475. time.sleep(1)
  476. Feishu.update_values(
  477. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i), [[values]])
  478. Common.logger(log_type).info("15:00数据更新成功:{}", values)
  479. elif upload_data == today and update_hour.hour == 20 and int(upload_hour) <= 10:
  480. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 抓取时间<=10点")
  481. # 当天 20:00 视频播放量
  482. twenty_hour_play_cnt = hour_play_cnt
  483. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  484. # 当天 10:00 上升的数据
  485. time.sleep(1)
  486. if Feishu.get_range_value(
  487. log_type, "xiaoniangao", "ba0da4",
  488. "L" + str(i) + ":" + "L" + str(i))[0] is None:
  489. ten_up_cnt = 0
  490. else:
  491. ten_up_cnt = Feishu.get_range_value(
  492. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i))[0]
  493. # 当天 15:00 上升的数据
  494. time.sleep(1)
  495. if Feishu.get_range_value(
  496. log_type, "xiaoniangao", "ba0da4",
  497. "M" + str(i) + ":" + "M" + str(i))[0] is None:
  498. fifteen_up_cnt = 0
  499. else:
  500. fifteen_up_cnt = Feishu.get_range_value(
  501. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i))[0]
  502. # 20:00 的上升榜写入数据
  503. values = int(twenty_hour_play_cnt) - (
  504. int(v_play_cnt) + int(ten_up_cnt) + int(fifteen_up_cnt))
  505. time.sleep(1)
  506. Feishu.update_values(
  507. log_type, "xiaoniangao", "ba0da4", "N" + str(i) + ":" + "N" + str(i), [[values]])
  508. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  509. elif upload_data == today and update_hour.hour == 20 and 10 < int(upload_hour) <= 15:
  510. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 10<抓取时间<=15点")
  511. # 当天 20:00 视频播放量
  512. twenty_hour_play_cnt = hour_play_cnt
  513. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  514. # 当天 15:00 上升的数据
  515. time.sleep(1)
  516. if Feishu.get_range_value(
  517. log_type, "xiaoniangao", "ba0da4",
  518. "M" + str(i) + ":" + "M" + str(i))[0] is None:
  519. fifteen_up_cnt = 0
  520. else:
  521. fifteen_up_cnt = Feishu.get_range_value(
  522. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i))[0]
  523. # 20:00 的上升榜写入数据
  524. values = int(twenty_hour_play_cnt) - (int(v_play_cnt) + int(fifteen_up_cnt))
  525. time.sleep(1)
  526. Feishu.update_values(
  527. log_type, "xiaoniangao", "ba0da4", "N" + str(i) + ":" + "N" + str(i), [[values]])
  528. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  529. elif upload_data == today and update_hour.hour == 20 and 15 < int(upload_hour) <= 20:
  530. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 15<抓取时间<=20点")
  531. # 当天 20:00 视频播放量
  532. twenty_hour_play_cnt = hour_play_cnt
  533. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  534. # 20:00 的上升榜写入数据
  535. values = int(twenty_hour_play_cnt) - int(v_play_cnt)
  536. time.sleep(1)
  537. Feishu.update_values(
  538. log_type, "xiaoniangao", "ba0da4", "N" + str(i) + ":" + "N" + str(i), [[values]])
  539. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  540. elif (upload_data == yesterday or upload_data == before_yesterday)\
  541. and update_hour.hour == 10:
  542. Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:10点")
  543. # 当天 10:00 视频播放量
  544. ten_hour_play_cnt = hour_play_cnt
  545. Common.logger(log_type).info("当天 10:00 视频播放量:{}", ten_hour_play_cnt)
  546. # 10:00 的上升榜写入数据
  547. values = int(ten_hour_play_cnt) - int(v_play_cnt)
  548. time.sleep(1)
  549. Feishu.update_values(
  550. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i), [[values]])
  551. Common.logger(log_type).info("10:00数据更新成功:{}", values)
  552. elif (upload_data == yesterday or upload_data == before_yesterday)\
  553. and update_hour.hour == 15:
  554. Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:15点")
  555. # 当天 15:00 视频播放量
  556. fifteen_hour_play_cnt = hour_play_cnt
  557. Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
  558. # 当天 10:00 上升的数据
  559. time.sleep(1)
  560. if Feishu.get_range_value(
  561. log_type, "xiaoniangao", "ba0da4",
  562. "L" + str(i) + ":" + "L" + str(i))[0] is None:
  563. ten_up_cnt = 0
  564. else:
  565. ten_up_cnt = Feishu.get_range_value(
  566. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i))[0]
  567. # 15:00 的上升榜写入数据
  568. values = int(fifteen_hour_play_cnt) - (int(v_play_cnt) + int(ten_up_cnt))
  569. time.sleep(1)
  570. Feishu.update_values(
  571. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i), [[values]])
  572. Common.logger(log_type).info("15:00数据更新成功:{}", values)
  573. elif (upload_data == yesterday or upload_data == before_yesterday)\
  574. and update_hour.hour == 20:
  575. Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:20点")
  576. # 当天 20:00 视频播放量
  577. twenty_hour_play_cnt = hour_play_cnt
  578. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  579. # 当天 10:00 上升的数据
  580. time.sleep(1)
  581. if Feishu.get_range_value(
  582. log_type, "xiaoniangao", "ba0da4",
  583. "L" + str(i) + ":" + "L" + str(i))[0] is None:
  584. ten_up_cnt = 0
  585. else:
  586. ten_up_cnt = Feishu.get_range_value(
  587. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i))[0]
  588. # 当天 15:00 上升的数据
  589. time.sleep(1)
  590. if Feishu.get_range_value(
  591. log_type, "xiaoniangao", "ba0da4",
  592. "M" + str(i) + ":" + "M" + str(i))[0] is None:
  593. fifteen_up_cnt = 0
  594. else:
  595. fifteen_up_cnt = Feishu.get_range_value(
  596. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i))[0]
  597. # 20:00 的上升榜写入数据
  598. values = int(twenty_hour_play_cnt) - (
  599. int(v_play_cnt) + int(ten_up_cnt) + int(fifteen_up_cnt))
  600. time.sleep(1)
  601. Feishu.update_values(
  602. log_type, "xiaoniangao", "ba0da4", "N" + str(i) + ":" + "N" + str(i), [[values]])
  603. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  604. except Exception as e:
  605. Common.logger(log_type).error("视频详情:{},异常:{}", v_title, e)
  606. except Exception as e:
  607. Common.logger(log_type).error("获取小时榜数据异常:{}", e)
  608. # 下载/上传
  609. @classmethod
  610. def download_and_publish(cls, log_type):
  611. """
  612. 1.从云文档中去重: https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
  613. 2.从云文档中下载符合规则的视频:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
  614. 2.1 当日 10:00 or 15:00 or 20:00 视频播放量上升 > 5000
  615. 2.2 当日 10:00 and 15:00 视频播放量上升 > 2000
  616. 2.3 当日 15:00 and 20:00 视频播放量上升 > 2000
  617. 2.4 昨日 20:00 and 今日 10:00 视频播放量上升 > 2000
  618. 3.上传
  619. """
  620. try:
  621. time.sleep(1)
  622. if len(Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")) == 2:
  623. Common.logger(log_type).info("当前工作表无数据")
  624. else:
  625. time.sleep(1)
  626. for i in range(3, len(Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4"))+1):
  627. time.sleep(1)
  628. Common.logger(log_type).info("分析第:{}行视频信息是否符合下载规则", i)
  629. # 略过空行
  630. if Feishu.get_range_value(
  631. log_type, "xiaoniangao", "ba0da4", "D" + str(i) + ":" + "D" + str(i))[0] is None\
  632. and Feishu.get_range_value(
  633. log_type, "xiaoniangao", "ba0da4", "C"+str(i)+":"+"C"+str(i))[0] is None\
  634. and Feishu.get_range_value(
  635. log_type, "xiaoniangao", "ba0da4", "A"+str(i)+":"+"A"+str(i))[0] is None:
  636. Common.logger(log_type).info("空行,略过")
  637. else:
  638. # 今日 10:00 数据上升量
  639. time.sleep(1)
  640. if Feishu.get_range_value(
  641. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i))[0] is None:
  642. ten_cnt = 0
  643. else:
  644. ten_cnt = Feishu.get_range_value(
  645. log_type, "xiaoniangao", "ba0da4", "L" + str(i) + ":" + "L" + str(i))[0]
  646. # 今日 15:00 数据上升量
  647. time.sleep(1)
  648. if Feishu.get_range_value(
  649. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i))[0] is None:
  650. fifteen_cnt = 0
  651. else:
  652. fifteen_cnt = Feishu.get_range_value(
  653. log_type, "xiaoniangao", "ba0da4", "M" + str(i) + ":" + "M" + str(i))[0]
  654. # 今日 20:00 数据上升量
  655. time.sleep(1)
  656. if Feishu.get_range_value(
  657. log_type, "xiaoniangao", "ba0da4", "N" + str(i) + ":" + "N" + str(i))[0] is None:
  658. twenty_cnt = 0
  659. else:
  660. twenty_cnt = Feishu.get_range_value(
  661. log_type, "xiaoniangao", "ba0da4", "N" + str(i) + ":" + "N" + str(i))[0]
  662. # 昨日 20:00 数据上升量
  663. time.sleep(1)
  664. if Feishu.get_range_value(
  665. log_type, "xiaoniangao", "ba0da4", "Q" + str(i) + ":" + "Q" + str(i))[0] is None:
  666. yesterday_twenty_cnt = 0
  667. else:
  668. yesterday_twenty_cnt = Feishu.get_range_value(
  669. log_type, "xiaoniangao", "ba0da4", "Q" + str(i) + ":" + "Q" + str(i))[0]
  670. # 视频标题
  671. time.sleep(1)
  672. v_title = Feishu.get_range_value(
  673. log_type, "xiaoniangao", "ba0da4", "D" + str(i) + ":" + "D" + str(i))[0]
  674. Common.logger(log_type).info("视频详情,video_title:{}", v_title)
  675. # 视频 ID
  676. time.sleep(1)
  677. v_id = Feishu.get_range_value(
  678. log_type, "xiaoniangao", "ba0da4", "C" + str(i) + ":" + "C" + str(i))[0]
  679. Common.logger(log_type).info("视频详情,video_id:{}", v_id)
  680. # profile_id,用户 ID
  681. time.sleep(1)
  682. p_id = Feishu.get_range_value(
  683. log_type, "xiaoniangao", "ba0da4", "A" + str(i) + ":" + "A" + str(i))[0]
  684. # 视频时长
  685. time.sleep(1)
  686. v_duration = Feishu.get_range_value(
  687. log_type, "xiaoniangao", "ba0da4", "F" + str(i) + ":" + "F" + str(i))[0]
  688. # profile_mid
  689. time.sleep(1)
  690. p_mid = Feishu.get_range_value(
  691. log_type, "xiaoniangao", "ba0da4", "B" + str(i) + ":" + "B" + str(i))[0]
  692. Common.logger(log_type).info("10:00 / 15:00 / 20:00 上升量: {} / {} / {}",
  693. ten_cnt, fifteen_cnt, twenty_cnt)
  694. # 发布时间
  695. time.sleep(1)
  696. v_upload_time = Feishu.get_range_value(
  697. log_type, "xiaoniangao", "ba0da4", "J" + str(i) + ":" + "J" + str(i))[0]
  698. v_send_time = int(time.mktime(time.strptime(v_upload_time, "%Y-%m-%d %H:%M:%S")))
  699. # # 判断视频 ID 长度,不大于 13 位
  700. # if len(str(v_id)) > 13:
  701. # Common.logger(log_type).info("视频ID长度大于13位:{}", v_id)
  702. #
  703. # el
  704. if int(time.time()) - int(v_send_time) >= 259200:
  705. Common.logger(log_type).info("抓取时间超过 3 天")
  706. return
  707. # 判断视频时长:1-10min
  708. elif int(v_duration) < 40:
  709. Common.logger(log_type).info("视频时长小于 40s")
  710. # # 过滤带字母的视频ID
  711. # elif any(word if word in v_id else False for word in cls.sensitive_videoid_words()) is True:
  712. # Common.logger(log_type).info("视频ID带字母:{}".format(v_id))
  713. # 从云文档中去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
  714. elif v_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2") for j in i]:
  715. Common.logger(log_type).info("该视频已下载:{}", v_title)
  716. time.sleep(1)
  717. # 上升榜判断逻辑,任意时间段上升量>=5000,连续两个时间段上升量>=2000
  718. elif int(ten_cnt) >= 5000 or int(fifteen_cnt) >= 5000 or int(twenty_cnt) >= 5000:
  719. Common.logger(log_type).info("10:00 or 15:00 or 20:00 数据上升量:{} or {} or {} >= 5000",
  720. ten_cnt, fifteen_cnt, twenty_cnt)
  721. Common.logger(log_type).info("满足下载规则,开始下载视频")
  722. try:
  723. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  724. headers = {
  725. "x-b3-traceid": cls.hour_x_b3_traceid,
  726. "X-Token-Id": cls.hour_x_token_id,
  727. "uid": cls.hour_uid,
  728. "content-type": "application/json",
  729. "Accept-Encoding": "gzip,compress,br,deflate",
  730. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  731. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  732. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  733. "Referer": cls.hour_referer
  734. }
  735. data = {
  736. "play_src": "1",
  737. "profile_id": int(p_id),
  738. "profile_mid": int(p_mid),
  739. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  740. "!400x400r/crop/400x400/interlace/1/format/jpg",
  741. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  742. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  743. "share_width": 625,
  744. "share_height": 500,
  745. "no_comments": True,
  746. "no_follow": True,
  747. "vid": v_id,
  748. "hot_l1_comment": True,
  749. "token": cls.hour_token,
  750. "uid": cls.hour_uid,
  751. "proj": "ma",
  752. "wx_ver": "8.0.20",
  753. "code_ver": "3.62.0",
  754. "log_common_params": {
  755. "e": [{
  756. "data": {
  757. "page": "dynamicSharePage"
  758. }
  759. }],
  760. "ext": {
  761. "brand": "iPhone",
  762. "device": "iPhone 11",
  763. "os": "iOS 14.7.1",
  764. "weixinver": "8.0.20",
  765. "srcver": "2.24.3",
  766. "net": "wifi",
  767. "scene": "1089"
  768. },
  769. "pj": "1",
  770. "pf": "2",
  771. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  772. }
  773. }
  774. urllib3.disable_warnings()
  775. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  776. hour_play_cnt = r.json()["data"]["play_pv"]
  777. hour_cover_url = r.json()["data"]["url"]
  778. hour_video_url = r.json()["data"]["v_url"]
  779. hour_video_duration = r.json()["data"]["du"]
  780. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  781. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  782. hour_video_share_cnt = r.json()["data"]["share"]
  783. hour_video_width = r.json()["data"]["w"]
  784. hour_video_height = r.json()["data"]["h"]
  785. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  786. hour_video_send_time = r.json()["data"]["t"]
  787. hour_user_name = r.json()["data"]["user"]["nick"]
  788. hour_head_url = r.json()["data"]["user"]["hurl"]
  789. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  790. # 下载封面
  791. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  792. # 下载视频
  793. Common.download_method(log_type, "video", v_title, hour_video_url)
  794. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  795. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  796. f_a.write(str(v_id) + "\n" +
  797. str(v_title) + "\n" +
  798. str(int(int(hour_video_duration) / 1000)) + "\n" +
  799. str(hour_play_cnt) + "\n" +
  800. str(hour_video_comment_cnt) + "\n" +
  801. str(hour_video_like_cnt) + "\n" +
  802. str(hour_video_share_cnt) + "\n" +
  803. str(hour_video_resolution) + "\n" +
  804. str(hour_video_send_time) + "\n" +
  805. str(hour_user_name) + "\n" +
  806. str(hour_head_url) + "\n" +
  807. str(hour_video_url) + "\n" +
  808. str(hour_cover_url) + "\n" +
  809. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  810. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  811. # 上传视频
  812. Common.logger(log_type).info("开始上传视频")
  813. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  814. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  815. our_video_id) + "/info"
  816. Common.logger(log_type).info("视频上传完成:{}", v_title)
  817. # 上传完成时间
  818. upload_time = int(time.time())
  819. # 保存视频信息到云文档
  820. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  821. # 插入空行
  822. time.sleep(1)
  823. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  824. # 视频信息写入云文档
  825. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  826. "小时级上升榜",
  827. v_id,
  828. v_title,
  829. our_video_link,
  830. hour_play_cnt,
  831. hour_video_comment_cnt,
  832. hour_video_like_cnt,
  833. hour_video_share_cnt,
  834. int(hour_video_duration)/1000,
  835. hour_video_resolution,
  836. time.strftime("%Y-%m-%d %H:%M:%S",
  837. time.localtime(int(hour_video_send_time) / 1000)),
  838. hour_user_name,
  839. p_id,
  840. p_mid,
  841. hour_head_url,
  842. hour_cover_url,
  843. hour_video_url]]
  844. time.sleep(1)
  845. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  846. # 保存视频信息到监控表
  847. Common.logger(log_type).info("添加视频到监控表:{}", v_title)
  848. # 插入空行
  849. time.sleep(1)
  850. Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
  851. # 视频信息写入监控表
  852. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  853. v_id,
  854. v_title,
  855. our_video_link,
  856. p_id,
  857. p_mid,
  858. hour_user_name,
  859. int(hour_video_duration)/1000,
  860. time.strftime("%Y/%m/%d %H:%M:%S",
  861. time.localtime(int(hour_video_send_time) / 1000)),
  862. hour_play_cnt]]
  863. time.sleep(1)
  864. Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
  865. except Exception as e:
  866. Common.logger(log_type).error("下载视频异常:{}", e)
  867. elif int(ten_cnt) >= 2000 and int(fifteen_cnt) >= 2000:
  868. Common.logger(log_type).info(
  869. "10:00 and 15:00 数据上升量:{} and {} >= 2000", ten_cnt, fifteen_cnt)
  870. Common.logger(log_type).info("满足下载规则,开始下载视频")
  871. try:
  872. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  873. headers = {
  874. "x-b3-traceid": cls.hour_x_b3_traceid,
  875. "X-Token-Id": cls.hour_x_token_id,
  876. "uid": cls.hour_uid,
  877. "content-type": "application/json",
  878. "Accept-Encoding": "gzip,compress,br,deflate",
  879. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  880. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  881. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  882. "Referer": cls.hour_referer
  883. }
  884. data = {
  885. "play_src": "1",
  886. "profile_id": int(p_id),
  887. "profile_mid": int(p_mid),
  888. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  889. "!400x400r/crop/400x400/interlace/1/format/jpg",
  890. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  891. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  892. "share_width": 625,
  893. "share_height": 500,
  894. "no_comments": True,
  895. "no_follow": True,
  896. "vid": v_id,
  897. "hot_l1_comment": True,
  898. "token": cls.hour_token,
  899. "uid": cls.hour_uid,
  900. "proj": "ma",
  901. "wx_ver": "8.0.20",
  902. "code_ver": "3.62.0",
  903. "log_common_params": {
  904. "e": [{
  905. "data": {
  906. "page": "dynamicSharePage"
  907. }
  908. }],
  909. "ext": {
  910. "brand": "iPhone",
  911. "device": "iPhone 11",
  912. "os": "iOS 14.7.1",
  913. "weixinver": "8.0.20",
  914. "srcver": "2.24.3",
  915. "net": "wifi",
  916. "scene": "1089"
  917. },
  918. "pj": "1",
  919. "pf": "2",
  920. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  921. }
  922. }
  923. urllib3.disable_warnings()
  924. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  925. hour_play_cnt = r.json()["data"]["play_pv"]
  926. hour_cover_url = r.json()["data"]["url"]
  927. hour_video_url = r.json()["data"]["v_url"]
  928. hour_video_duration = r.json()["data"]["du"]
  929. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  930. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  931. hour_video_share_cnt = r.json()["data"]["share"]
  932. hour_video_width = r.json()["data"]["w"]
  933. hour_video_height = r.json()["data"]["h"]
  934. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  935. hour_video_send_time = r.json()["data"]["t"]
  936. hour_user_name = r.json()["data"]["user"]["nick"]
  937. hour_head_url = r.json()["data"]["user"]["hurl"]
  938. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  939. # 下载封面
  940. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  941. # 下载视频
  942. Common.download_method(log_type, "video", v_title, hour_video_url)
  943. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  944. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  945. f_a.write(str(v_id) + "\n" +
  946. str(v_title) + "\n" +
  947. str(int(int(hour_video_duration) / 1000)) + "\n" +
  948. str(hour_play_cnt) + "\n" +
  949. str(hour_video_comment_cnt) + "\n" +
  950. str(hour_video_like_cnt) + "\n" +
  951. str(hour_video_share_cnt) + "\n" +
  952. str(hour_video_resolution) + "\n" +
  953. str(hour_video_send_time) + "\n" +
  954. str(hour_user_name) + "\n" +
  955. str(hour_head_url) + "\n" +
  956. str(hour_video_url) + "\n" +
  957. str(hour_cover_url) + "\n" +
  958. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  959. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  960. # 上传
  961. Common.logger(log_type).info("开始上传视频")
  962. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  963. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  964. our_video_id) + "/info"
  965. Common.logger(log_type).info("视频上传完成:{}", v_title)
  966. # 上传完成时间
  967. upload_time = int(time.time())
  968. # 保存视频信息到云文档
  969. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  970. # 插入空行
  971. time.sleep(1)
  972. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  973. # 视频信息写入云文档
  974. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  975. "小时级上升榜",
  976. v_id,
  977. v_title,
  978. our_video_link,
  979. hour_play_cnt,
  980. hour_video_comment_cnt,
  981. hour_video_like_cnt,
  982. hour_video_share_cnt,
  983. int(hour_video_duration) / 1000,
  984. hour_video_resolution,
  985. time.strftime("%Y-%m-%d %H:%M:%S",
  986. time.localtime(int(hour_video_send_time) / 1000)),
  987. hour_user_name,
  988. p_id,
  989. p_mid,
  990. hour_head_url,
  991. hour_cover_url,
  992. hour_video_url]]
  993. time.sleep(1)
  994. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  995. # 保存视频信息到监控表
  996. Common.logger(log_type).info("添加视频到监控表:{}", v_title)
  997. # 插入空行
  998. time.sleep(1)
  999. Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
  1000. # 视频信息写入监控表
  1001. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  1002. v_id,
  1003. v_title,
  1004. our_video_link,
  1005. p_id,
  1006. p_mid,
  1007. hour_user_name,
  1008. int(hour_video_duration) / 1000,
  1009. time.strftime("%Y/%m/%d %H:%M:%S",
  1010. time.localtime(int(hour_video_send_time) / 1000)),
  1011. hour_play_cnt]]
  1012. time.sleep(1)
  1013. Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
  1014. except Exception as e:
  1015. Common.logger(log_type).error("下载视频异常:{}", e)
  1016. elif int(fifteen_cnt) >= 2000 and int(twenty_cnt) >= 2000:
  1017. Common.logger(log_type).info(
  1018. "15:00 and 20:00 数据上升量:{} and {} >= 2000", fifteen_cnt, twenty_cnt)
  1019. Common.logger(log_type).info("满足下载规则,开始下载视频")
  1020. try:
  1021. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  1022. headers = {
  1023. "x-b3-traceid": cls.hour_x_b3_traceid,
  1024. "X-Token-Id": cls.hour_x_token_id,
  1025. "uid": cls.hour_uid,
  1026. "content-type": "application/json",
  1027. "Accept-Encoding": "gzip,compress,br,deflate",
  1028. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  1029. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  1030. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  1031. "Referer": cls.hour_referer
  1032. }
  1033. data = {
  1034. "play_src": "1",
  1035. "profile_id": int(p_id),
  1036. "profile_mid": int(p_mid),
  1037. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  1038. "!400x400r/crop/400x400/interlace/1/format/jpg",
  1039. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  1040. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  1041. "share_width": 625,
  1042. "share_height": 500,
  1043. "no_comments": True,
  1044. "no_follow": True,
  1045. "vid": v_id,
  1046. "hot_l1_comment": True,
  1047. "token": cls.hour_token,
  1048. "uid": cls.hour_uid,
  1049. "proj": "ma",
  1050. "wx_ver": "8.0.20",
  1051. "code_ver": "3.62.0",
  1052. "log_common_params": {
  1053. "e": [{
  1054. "data": {
  1055. "page": "dynamicSharePage"
  1056. }
  1057. }],
  1058. "ext": {
  1059. "brand": "iPhone",
  1060. "device": "iPhone 11",
  1061. "os": "iOS 14.7.1",
  1062. "weixinver": "8.0.20",
  1063. "srcver": "2.24.3",
  1064. "net": "wifi",
  1065. "scene": "1089"
  1066. },
  1067. "pj": "1",
  1068. "pf": "2",
  1069. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1070. }
  1071. }
  1072. urllib3.disable_warnings()
  1073. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1074. hour_play_cnt = r.json()["data"]["play_pv"]
  1075. hour_cover_url = r.json()["data"]["url"]
  1076. hour_video_url = r.json()["data"]["v_url"]
  1077. hour_video_duration = r.json()["data"]["du"]
  1078. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1079. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1080. hour_video_share_cnt = r.json()["data"]["share"]
  1081. hour_video_width = r.json()["data"]["w"]
  1082. hour_video_height = r.json()["data"]["h"]
  1083. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1084. hour_video_send_time = r.json()["data"]["t"]
  1085. hour_user_name = r.json()["data"]["user"]["nick"]
  1086. hour_head_url = r.json()["data"]["user"]["hurl"]
  1087. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  1088. # 下载封面
  1089. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  1090. # 下载视频
  1091. Common.download_method(log_type, "video", v_title, hour_video_url)
  1092. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1093. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1094. f_a.write(str(v_id) + "\n" +
  1095. str(v_title) + "\n" +
  1096. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1097. str(hour_play_cnt) + "\n" +
  1098. str(hour_video_comment_cnt) + "\n" +
  1099. str(hour_video_like_cnt) + "\n" +
  1100. str(hour_video_share_cnt) + "\n" +
  1101. str(hour_video_resolution) + "\n" +
  1102. str(hour_video_send_time) + "\n" +
  1103. str(hour_user_name) + "\n" +
  1104. str(hour_head_url) + "\n" +
  1105. str(hour_video_url) + "\n" +
  1106. str(hour_cover_url) + "\n" +
  1107. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1108. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  1109. # 上传
  1110. Common.logger(log_type).info("开始上传视频")
  1111. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  1112. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  1113. our_video_id) + "/info"
  1114. Common.logger(log_type).info("视频上传完成:{}", v_title)
  1115. # 上传完成时间
  1116. upload_time = int(time.time())
  1117. # 保存视频信息到云文档
  1118. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  1119. # 插入空行
  1120. time.sleep(1)
  1121. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1122. # 视频信息写入云文档
  1123. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  1124. "小时级上升榜",
  1125. v_id,
  1126. v_title,
  1127. our_video_link,
  1128. hour_play_cnt,
  1129. hour_video_comment_cnt,
  1130. hour_video_like_cnt,
  1131. hour_video_share_cnt,
  1132. int(hour_video_duration) / 1000,
  1133. hour_video_resolution,
  1134. time.strftime("%Y-%m-%d %H:%M:%S",
  1135. time.localtime(int(hour_video_send_time) / 1000)),
  1136. hour_user_name,
  1137. p_id,
  1138. p_mid,
  1139. hour_head_url,
  1140. hour_cover_url,
  1141. hour_video_url]]
  1142. time.sleep(1)
  1143. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  1144. # 保存视频信息到监控表
  1145. Common.logger(log_type).info("添加视频到监控表:{}", v_title)
  1146. # 插入空行
  1147. time.sleep(1)
  1148. Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
  1149. # 视频信息写入监控表
  1150. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  1151. v_id,
  1152. v_title,
  1153. our_video_link,
  1154. p_id,
  1155. p_mid,
  1156. hour_user_name,
  1157. int(hour_video_duration) / 1000,
  1158. time.strftime("%Y/%m/%d %H:%M:%S",
  1159. time.localtime(int(hour_video_send_time) / 1000)),
  1160. hour_play_cnt]]
  1161. time.sleep(1)
  1162. Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
  1163. except Exception as e:
  1164. Common.logger(log_type).error("下载视频异常:{}", e)
  1165. elif int(yesterday_twenty_cnt) >= 2000 and int(ten_cnt) >= 2000:
  1166. Common.logger(log_type).info("昨日20:00 and 今日10:00 数据上升量:{} and {} >= 2000",
  1167. yesterday_twenty_cnt, ten_cnt)
  1168. Common.logger(log_type).info("满足下载规则,开始下载视频")
  1169. try:
  1170. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  1171. headers = {
  1172. "x-b3-traceid": cls.hour_x_b3_traceid,
  1173. "X-Token-Id": cls.hour_x_token_id,
  1174. "uid": cls.hour_uid,
  1175. "content-type": "application/json",
  1176. "Accept-Encoding": "gzip,compress,br,deflate",
  1177. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  1178. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  1179. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  1180. "Referer": cls.hour_referer
  1181. }
  1182. data = {
  1183. "play_src": "1",
  1184. "profile_id": int(p_id),
  1185. "profile_mid": int(p_mid),
  1186. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  1187. "!400x400r/crop/400x400/interlace/1/format/jpg",
  1188. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  1189. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  1190. "share_width": 625,
  1191. "share_height": 500,
  1192. "no_comments": True,
  1193. "no_follow": True,
  1194. "vid": v_id,
  1195. "hot_l1_comment": True,
  1196. "token": cls.hour_token,
  1197. "uid": cls.hour_uid,
  1198. "proj": "ma",
  1199. "wx_ver": "8.0.20",
  1200. "code_ver": "3.62.0",
  1201. "log_common_params": {
  1202. "e": [{
  1203. "data": {
  1204. "page": "dynamicSharePage"
  1205. }
  1206. }],
  1207. "ext": {
  1208. "brand": "iPhone",
  1209. "device": "iPhone 11",
  1210. "os": "iOS 14.7.1",
  1211. "weixinver": "8.0.20",
  1212. "srcver": "2.24.3",
  1213. "net": "wifi",
  1214. "scene": "1089"
  1215. },
  1216. "pj": "1",
  1217. "pf": "2",
  1218. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1219. }
  1220. }
  1221. urllib3.disable_warnings()
  1222. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1223. hour_play_cnt = r.json()["data"]["play_pv"]
  1224. hour_cover_url = r.json()["data"]["url"]
  1225. hour_video_url = r.json()["data"]["v_url"]
  1226. hour_video_duration = r.json()["data"]["du"]
  1227. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1228. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1229. hour_video_share_cnt = r.json()["data"]["share"]
  1230. hour_video_width = r.json()["data"]["w"]
  1231. hour_video_height = r.json()["data"]["h"]
  1232. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1233. hour_video_send_time = r.json()["data"]["t"]
  1234. hour_user_name = r.json()["data"]["user"]["nick"]
  1235. hour_head_url = r.json()["data"]["user"]["hurl"]
  1236. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  1237. # 下载封面
  1238. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  1239. # 下载视频
  1240. Common.download_method(log_type, "video", v_title, hour_video_url)
  1241. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1242. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1243. f_a.write(str(v_id) + "\n" +
  1244. str(v_title) + "\n" +
  1245. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1246. str(hour_play_cnt) + "\n" +
  1247. str(hour_video_comment_cnt) + "\n" +
  1248. str(hour_video_like_cnt) + "\n" +
  1249. str(hour_video_share_cnt) + "\n" +
  1250. str(hour_video_resolution) + "\n" +
  1251. str(hour_video_send_time) + "\n" +
  1252. str(hour_user_name) + "\n" +
  1253. str(hour_head_url) + "\n" +
  1254. str(hour_video_url) + "\n" +
  1255. str(hour_cover_url) + "\n" +
  1256. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1257. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  1258. # 上传
  1259. Common.logger(log_type).info("开始上传视频")
  1260. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  1261. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  1262. our_video_id) + "/info"
  1263. Common.logger(log_type).info("视频上传完成:{}", v_title)
  1264. # 上传完成时间
  1265. upload_time = int(time.time())
  1266. # 保存视频信息到云文档
  1267. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  1268. # 插入空行
  1269. time.sleep(1)
  1270. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1271. # 视频信息写入云文档
  1272. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  1273. "小时级上升榜",
  1274. v_id,
  1275. v_title,
  1276. our_video_link,
  1277. hour_play_cnt,
  1278. hour_video_comment_cnt,
  1279. hour_video_like_cnt,
  1280. hour_video_share_cnt,
  1281. int(hour_video_duration) / 1000,
  1282. hour_video_resolution,
  1283. time.strftime("%Y-%m-%d %H:%M:%S",
  1284. time.localtime(int(hour_video_send_time) / 1000)),
  1285. hour_user_name,
  1286. p_id,
  1287. p_mid,
  1288. hour_head_url,
  1289. hour_cover_url,
  1290. hour_video_url]]
  1291. time.sleep(1)
  1292. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  1293. # 保存视频信息到监控表
  1294. Common.logger(log_type).info("添加视频到监控表:{}", v_title)
  1295. # 插入空行
  1296. time.sleep(1)
  1297. Feishu.insert_columns(log_type, "monitor", "N7e2yI", "ROWS", 1, 2)
  1298. # 视频信息写入监控表
  1299. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  1300. v_id,
  1301. v_title,
  1302. our_video_link,
  1303. p_id,
  1304. p_mid,
  1305. hour_user_name,
  1306. int(hour_video_duration) / 1000,
  1307. time.strftime("%Y/%m/%d %H:%M:%S",
  1308. time.localtime(int(hour_video_send_time) / 1000)),
  1309. hour_play_cnt]]
  1310. time.sleep(1)
  1311. Feishu.update_values(log_type, "monitor", "N7e2yI", "F2:O2", values)
  1312. except Exception as e:
  1313. Common.logger(log_type).error("下载视频异常:{}", e)
  1314. else:
  1315. Common.logger(log_type).info("上升量不满足下载规则")
  1316. except Exception as e:
  1317. Common.logger(log_type).error("下载视频时,获取飞书信息异常:{}", e)
  1318. if __name__ == "__main__":
  1319. hour_list = HourList()
  1320. # hour_list.get_hour_list_feeds()
  1321. # hour_list.del_null_rows("xiaoniangao", "ba0da4", 3)
  1322. # hour_list.update_hour_list_data(
  1323. # today=datetime.datetime.now().strftime("%Y-%m-%d"),
  1324. # yesterday=(datetime.date.today() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d"),
  1325. # before_yesterday=(datetime.date.today() + datetime.timedelta(days=-2)).strftime("%Y-%m-%d"))
  1326. hour_list.download_and_publish("hour")