xiaoniangao_hour_list.py 86 KB

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