hour_list.py 79 KB

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