xiaoniangao_hour_list.py 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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(word if 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. time.sleep(1)
  324. # 从云文档中去重: 用户主页_已下载表
  325. elif video_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "Wu0CeL") for j in i]:
  326. Common.logger(log_type).info("该视频已下载:{}", video_title)
  327. time.sleep(1)
  328. # 从云文档去重: 播放量_已下载表
  329. elif video_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "c85k1C") for j in i]:
  330. Common.logger(log_type).info("该视频已下载:{}", video_title)
  331. time.sleep(1)
  332. # 从云文档去重: 小年糕小时级数据_feeds
  333. elif video_id in [j for i in Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4") for j in i]:
  334. Common.logger(log_type).info("该视频已保存过:{}", video_title)
  335. time.sleep(1)
  336. else:
  337. # feeds工作表,插入空行
  338. time.sleep(1)
  339. Feishu.insert_columns(log_type, "xiaoniangao", "ba0da4", "ROWS", 2, 3)
  340. # 获取当前时间
  341. get_feeds_time = int(time.time())
  342. # 看一看云文档,工作表中写入数据
  343. values = [[profile_id,
  344. profile_mid,
  345. video_id,
  346. video_title,
  347. user_name,
  348. video_duration,
  349. cover_url,
  350. video_url,
  351. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)),
  352. str(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(get_feeds_time))),
  353. video_play_cnt]]
  354. # 等待 1s,防止操作云文档太频繁,导致报错
  355. time.sleep(1)
  356. Feishu.update_values(log_type, "xiaoniangao", "ba0da4", "A3:K3", values)
  357. Common.logger(log_type).info("视频添加至小时级数据_feeds成功:{}\n".format(video_title))
  358. except Exception as e:
  359. Common.logger(log_type).error("获取小时榜视频列表异常:{}\n", e)
  360. # 更新小时榜数据
  361. @classmethod
  362. def update_hour_list_data(cls, log_type, today, yesterday, before_yesterday):
  363. """
  364. 更新小时榜数据
  365. """
  366. try:
  367. update_hour_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4")
  368. if len(update_hour_sheet) == 2:
  369. Common.logger(log_type).info("当前工作表无数据")
  370. else:
  371. for i in range(2, len(update_hour_sheet) + 1):
  372. Common.logger(log_type).info("更新第:{}行视频信息", i + 1)
  373. # 略过空行
  374. if update_hour_sheet[i][0] is None \
  375. or update_hour_sheet[i][1] is None or update_hour_sheet[i][2] is None:
  376. Common.logger(log_type).info("空行,略过")
  377. else:
  378. # 视频标题
  379. v_title = update_hour_sheet[i][3]
  380. Common.logger(log_type).info("video_title:{}", v_title)
  381. # 视频 ID
  382. v_id = update_hour_sheet[i][2]
  383. Common.logger(log_type).info("video_id:{}", v_id)
  384. # profile_id,用户 ID
  385. p_id = update_hour_sheet[i][0]
  386. Common.logger(log_type).info("profile_id:{}", p_id)
  387. # profile_mid
  388. p_mid = update_hour_sheet[i][1]
  389. Common.logger(log_type).info("profile_mid:{}", p_mid)
  390. # 抓取时的播放量
  391. v_play_cnt = update_hour_sheet[i][10]
  392. Common.logger(log_type).info("video_play_cnt:{}", v_play_cnt)
  393. # 抓取时间
  394. v_upload_time = update_hour_sheet[i][9]
  395. Common.logger(log_type).info("video_send_time:{}", v_upload_time)
  396. # 抓取时间的时间戳格式(秒为单位)
  397. v_time = int(time.mktime(time.strptime(v_upload_time, "%Y/%m/%d %H:%M:%S")))
  398. # 抓取时间:日期
  399. upload_data = v_upload_time.split(" ")[0]
  400. # 抓取时间:小时
  401. upload_hour = v_upload_time.split(" ")[-1].split(":")[0]
  402. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  403. headers = {
  404. "x-b3-traceid": cls.hour_x_b3_traceid,
  405. "X-Token-Id": cls.hour_x_token_id,
  406. "uid": cls.hour_uid,
  407. "content-type": "application/json",
  408. "Accept-Encoding": "gzip,compress,br,deflate",
  409. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  410. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  411. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  412. "Referer": cls.hour_referer
  413. }
  414. data = {
  415. "play_src": "1",
  416. "profile_id": int(p_id),
  417. "profile_mid": int(p_mid),
  418. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  419. "!400x400r/crop/400x400/interlace/1/format/jpg",
  420. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  421. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  422. "share_width": 625,
  423. "share_height": 500,
  424. "no_comments": True,
  425. "no_follow": True,
  426. "vid": v_id,
  427. "hot_l1_comment": True,
  428. "token": cls.hour_token,
  429. "uid": cls.hour_uid,
  430. "proj": "ma",
  431. "wx_ver": "8.0.20",
  432. "code_ver": "3.62.0",
  433. "log_common_params": {
  434. "e": [{
  435. "data": {
  436. "page": "dynamicSharePage"
  437. }
  438. }],
  439. "ext": {
  440. "brand": "iPhone",
  441. "device": "iPhone 11",
  442. "os": "iOS 14.7.1",
  443. "weixinver": "8.0.20",
  444. "srcver": "2.24.3",
  445. "net": "wifi",
  446. "scene": "1089"
  447. },
  448. "pj": "1",
  449. "pf": "2",
  450. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  451. }
  452. }
  453. try:
  454. urllib3.disable_warnings()
  455. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  456. hour_play_cnt = r.json()["data"]["play_pv"]
  457. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  458. # 固定时间获取符合规则的视频,写入云文档:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=ba0da4
  459. update_hour = datetime.datetime.now()
  460. if int(time.time()) - v_time >= 172800:
  461. Common.logger(log_type).info("抓取时间超过 2 天")
  462. return
  463. elif upload_data == today and update_hour.hour == 10 and int(upload_hour) <= 10:
  464. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:10点 and 抓取时间<=10点")
  465. # 当天 10:00 视频播放量
  466. ten_hour_play_cnt = hour_play_cnt
  467. Common.logger(log_type).info("当天 10:00 视频播放量:{}", ten_hour_play_cnt)
  468. # 10:00 的上升榜写入数据
  469. values = int(ten_hour_play_cnt) - int(v_play_cnt)
  470. time.sleep(1)
  471. Feishu.update_values(
  472. log_type, "xiaoniangao", "ba0da4",
  473. "L" + str(i + 1) + ":" + "L" + str(i + 1), [[values]])
  474. Common.logger(log_type).info("10:00数据更新成功:{}", values)
  475. elif upload_data == today and update_hour.hour == 15 and int(upload_hour) <= 10:
  476. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:15点 and 抓取时间<=10点")
  477. # 当天 15:00 视频播放量
  478. fifteen_hour_play_cnt = hour_play_cnt
  479. Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
  480. # 当天 10:00 上升的数据
  481. if update_hour_sheet[i][11] is None:
  482. ten_up_cnt = 0
  483. else:
  484. ten_up_cnt = update_hour_sheet[i][11]
  485. # 15:00 的上升榜写入数据
  486. values = int(fifteen_hour_play_cnt) - (int(v_play_cnt) + int(ten_up_cnt))
  487. time.sleep(1)
  488. Feishu.update_values(
  489. log_type, "xiaoniangao", "ba0da4",
  490. "M" + str(i + 1) + ":" + "M" + str(i + 1), [[values]])
  491. Common.logger(log_type).info("15:00数据更新成功:{}", values)
  492. elif upload_data == today and update_hour.hour == 15 and 10 < int(upload_hour) <= 15:
  493. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:15点 and 10<抓取时间<=15点")
  494. # 当天 15:00 视频播放量
  495. fifteen_hour_play_cnt = hour_play_cnt
  496. Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
  497. # 15:00 的上升榜写入数据
  498. values = int(fifteen_hour_play_cnt) - int(v_play_cnt)
  499. time.sleep(1)
  500. Feishu.update_values(
  501. log_type, "xiaoniangao", "ba0da4",
  502. "M" + str(i + 1) + ":" + "M" + str(i + 1), [[values]])
  503. Common.logger(log_type).info("15:00数据更新成功:{}", values)
  504. elif upload_data == today and update_hour.hour == 20 and int(upload_hour) <= 10:
  505. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 抓取时间<=10点")
  506. # 当天 20:00 视频播放量
  507. twenty_hour_play_cnt = hour_play_cnt
  508. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  509. # 当天 10:00 上升的数据
  510. if update_hour_sheet[i][11] is None:
  511. ten_up_cnt = 0
  512. else:
  513. ten_up_cnt = update_hour_sheet[i][11]
  514. # 当天 15:00 上升的数据
  515. if update_hour_sheet[i][12] is None:
  516. fifteen_up_cnt = 0
  517. else:
  518. fifteen_up_cnt = update_hour_sheet[i][12]
  519. # 20:00 的上升榜写入数据
  520. values = int(twenty_hour_play_cnt) - (
  521. int(v_play_cnt) + int(ten_up_cnt) + int(fifteen_up_cnt))
  522. time.sleep(1)
  523. Feishu.update_values(
  524. log_type, "xiaoniangao", "ba0da4",
  525. "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
  526. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  527. elif upload_data == today and update_hour.hour == 20 and 10 < int(upload_hour) <= 15:
  528. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 10<抓取时间<=15点")
  529. # 当天 20:00 视频播放量
  530. twenty_hour_play_cnt = hour_play_cnt
  531. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  532. # 当天 15:00 上升的数据
  533. if update_hour_sheet[i][12] is None:
  534. fifteen_up_cnt = 0
  535. else:
  536. fifteen_up_cnt = update_hour_sheet[i][12]
  537. # 20:00 的上升榜写入数据
  538. values = int(twenty_hour_play_cnt) - (int(v_play_cnt) + int(fifteen_up_cnt))
  539. time.sleep(1)
  540. Feishu.update_values(
  541. log_type, "xiaoniangao", "ba0da4",
  542. "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
  543. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  544. elif upload_data == today and update_hour.hour == 20 and 15 < int(upload_hour) <= 20:
  545. Common.logger(log_type).info("满足条件: 抓取日期为今天 and 当前时间:20点 and 15<抓取时间<=20点")
  546. # 当天 20:00 视频播放量
  547. twenty_hour_play_cnt = hour_play_cnt
  548. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  549. # 20:00 的上升榜写入数据
  550. values = int(twenty_hour_play_cnt) - int(v_play_cnt)
  551. time.sleep(1)
  552. Feishu.update_values(
  553. log_type, "xiaoniangao", "ba0da4",
  554. "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
  555. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  556. elif (upload_data == yesterday or upload_data == before_yesterday) \
  557. and update_hour.hour == 10:
  558. Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:10点")
  559. # 当天 10:00 视频播放量
  560. ten_hour_play_cnt = hour_play_cnt
  561. Common.logger(log_type).info("当天 10:00 视频播放量:{}", ten_hour_play_cnt)
  562. # 10:00 的上升榜写入数据
  563. values = int(ten_hour_play_cnt) - int(v_play_cnt)
  564. time.sleep(1)
  565. Feishu.update_values(
  566. log_type, "xiaoniangao", "ba0da4",
  567. "L" + str(i + 1) + ":" + "L" + str(i + 1), [[values]])
  568. Common.logger(log_type).info("10:00数据更新成功:{}", values)
  569. elif (upload_data == yesterday or upload_data == before_yesterday) \
  570. and update_hour.hour == 15:
  571. Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:15点")
  572. # 当天 15:00 视频播放量
  573. fifteen_hour_play_cnt = hour_play_cnt
  574. Common.logger(log_type).info("当天 15:00 视频播放量:{}", fifteen_hour_play_cnt)
  575. # 当天 10:00 上升的数据
  576. if update_hour_sheet[i][11] is None:
  577. ten_up_cnt = 0
  578. else:
  579. ten_up_cnt = update_hour_sheet[i][11]
  580. # 15:00 的上升榜写入数据
  581. values = int(fifteen_hour_play_cnt) - (int(v_play_cnt) + int(ten_up_cnt))
  582. time.sleep(1)
  583. Feishu.update_values(
  584. log_type, "xiaoniangao", "ba0da4",
  585. "M" + str(i + 1) + ":" + "M" + str(i + 1), [[values]])
  586. Common.logger(log_type).info("15:00数据更新成功:{}", values)
  587. elif (upload_data == yesterday or upload_data == before_yesterday) \
  588. and update_hour.hour == 20:
  589. Common.logger(log_type).info("满足条件: 抓取时间小于今天 and 当前时间:20点")
  590. # 当天 20:00 视频播放量
  591. twenty_hour_play_cnt = hour_play_cnt
  592. Common.logger(log_type).info("当天 20:00 视频播放量:{}", twenty_hour_play_cnt)
  593. # 当天 10:00 上升的数据
  594. if update_hour_sheet[i][11] is None:
  595. ten_up_cnt = 0
  596. else:
  597. ten_up_cnt = update_hour_sheet[i][11]
  598. # 当天 15:00 上升的数据
  599. if update_hour_sheet[i][12] is None:
  600. fifteen_up_cnt = 0
  601. else:
  602. fifteen_up_cnt = update_hour_sheet[i][12]
  603. # 20:00 的上升榜写入数据
  604. values = int(twenty_hour_play_cnt) - (
  605. int(v_play_cnt) + int(ten_up_cnt) + int(fifteen_up_cnt))
  606. time.sleep(1)
  607. Feishu.update_values(
  608. log_type, "xiaoniangao", "ba0da4",
  609. "N" + str(i + 1) + ":" + "N" + str(i + 1), [[values]])
  610. Common.logger(log_type).info("20:00数据更新成功:{}", values)
  611. except Exception as e:
  612. Common.logger(log_type).error("视频详情:{},异常:{}", v_title, e)
  613. except Exception as e:
  614. Common.logger(log_type).error("获取小时榜数据异常:{}\n", e)
  615. # 下载/上传
  616. @classmethod
  617. def download_and_publish(cls, log_type):
  618. """
  619. 1.从云文档中去重: https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
  620. 2.从云文档中下载符合规则的视频:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
  621. 2.1 当日 10:00 or 15:00 or 20:00 视频播放量上升 > 5000
  622. 2.2 当日 10:00 and 15:00 视频播放量上升 > 2000
  623. 2.3 当日 15:00 and 20:00 视频播放量上升 > 2000
  624. 2.4 昨日 20:00 and 今日 10:00 视频播放量上升 > 2000
  625. 3.上传
  626. """
  627. try:
  628. time.sleep(1)
  629. download_hour_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4")
  630. if len(download_hour_sheet) == 2:
  631. Common.logger(log_type).info("当前工作表无数据")
  632. else:
  633. for i in range(2, len(download_hour_sheet) + 1):
  634. Common.logger(log_type).info("分析第:{}行视频信息是否符合下载规则", i + 1)
  635. # 略过空行
  636. if download_hour_sheet[i][0] is None \
  637. or download_hour_sheet[i][1] is None or download_hour_sheet[i][2] is None:
  638. Common.logger(log_type).info("空行,略过")
  639. else:
  640. # 今日 10:00 数据上升量
  641. if download_hour_sheet[i][11] is None:
  642. ten_cnt = 0
  643. else:
  644. ten_cnt = download_hour_sheet[i][11]
  645. # 今日 15:00 数据上升量
  646. if download_hour_sheet[i][12] is None:
  647. fifteen_cnt = 0
  648. else:
  649. fifteen_cnt = download_hour_sheet[i][12]
  650. # 今日 20:00 数据上升量
  651. if download_hour_sheet[i][13] is None:
  652. twenty_cnt = 0
  653. else:
  654. twenty_cnt = download_hour_sheet[i][13]
  655. # 昨日 20:00 数据上升量
  656. if download_hour_sheet[i][16] is None:
  657. yesterday_twenty_cnt = 0
  658. else:
  659. yesterday_twenty_cnt = download_hour_sheet[i][16]
  660. # 视频标题
  661. v_title = download_hour_sheet[i][3]
  662. Common.logger(log_type).info("video_title:{}", v_title)
  663. # 视频 ID
  664. v_id = download_hour_sheet[i][2]
  665. Common.logger(log_type).info("video_id:{}", v_id)
  666. # profile_id,用户 ID
  667. p_id = download_hour_sheet[i][0]
  668. # 视频时长
  669. v_duration = download_hour_sheet[i][5]
  670. # profile_mid
  671. p_mid = download_hour_sheet[i][1]
  672. Common.logger(log_type).info("10:00 / 15:00 / 20:00 上升量: {} / {} / {}",
  673. ten_cnt, fifteen_cnt, twenty_cnt)
  674. # 抓取时间
  675. v_upload_time = download_hour_sheet[i][9]
  676. v_send_time = int(time.mktime(time.strptime(v_upload_time, "%Y/%m/%d %H:%M:%S")))
  677. # 播放量
  678. v_play_cnt = download_hour_sheet[i][10]
  679. # # 判断视频 ID 长度,不大于 13 位
  680. # if len(str(v_id)) > 13:
  681. # Common.logger(log_type).info("视频ID长度大于13位:{}", v_id)
  682. #
  683. # el
  684. if int(time.time()) - int(v_send_time) >= 259200:
  685. Common.logger(log_type).info("抓取时间超过 3 天")
  686. return
  687. # 判断视频时长:1-10min
  688. elif int(v_duration) < 40:
  689. Common.logger(log_type).info("视频时长小于 40s")
  690. # # 过滤带字母的视频ID
  691. # elif any(word if word in v_id else False for word in cls.sensitive_videoid_words()) is True:
  692. # Common.logger(log_type).info("视频ID带字母:{}".format(v_id))
  693. # 从云文档中去重: 小时级_已下载表
  694. elif v_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2") for j in i]:
  695. Common.logger(log_type).info("该视频已下载:{}", v_title)
  696. # 从云文档中去重: 用户主页_已下载表
  697. elif v_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "Wu0CeL") for j in i]:
  698. Common.logger(log_type).info("该视频已下载:{}", v_title)
  699. # 从云文档中去重: 播放量_已下载表
  700. elif v_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "c85k1C") for j in i]:
  701. Common.logger(log_type).info("该视频已下载:{}", v_title)
  702. # 播放量大于 50000,直接下载
  703. elif int(v_play_cnt) >= 50000:
  704. Common.logger(log_type).info("播放量:{} >= 50000,满足下载规则,开始下载视频", v_play_cnt)
  705. try:
  706. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  707. headers = {
  708. "x-b3-traceid": cls.hour_x_b3_traceid,
  709. "X-Token-Id": cls.hour_x_token_id,
  710. "uid": cls.hour_uid,
  711. "content-type": "application/json",
  712. "Accept-Encoding": "gzip,compress,br,deflate",
  713. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  714. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  715. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  716. "Referer": cls.hour_referer
  717. }
  718. data = {
  719. "play_src": "1",
  720. "profile_id": int(p_id),
  721. "profile_mid": int(p_mid),
  722. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  723. "!400x400r/crop/400x400/interlace/1/format/jpg",
  724. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  725. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  726. "share_width": 625,
  727. "share_height": 500,
  728. "no_comments": True,
  729. "no_follow": True,
  730. "vid": v_id,
  731. "hot_l1_comment": True,
  732. "token": cls.hour_token,
  733. "uid": cls.hour_uid,
  734. "proj": "ma",
  735. "wx_ver": "8.0.20",
  736. "code_ver": "3.62.0",
  737. "log_common_params": {
  738. "e": [{
  739. "data": {
  740. "page": "dynamicSharePage"
  741. }
  742. }],
  743. "ext": {
  744. "brand": "iPhone",
  745. "device": "iPhone 11",
  746. "os": "iOS 14.7.1",
  747. "weixinver": "8.0.20",
  748. "srcver": "2.24.3",
  749. "net": "wifi",
  750. "scene": "1089"
  751. },
  752. "pj": "1",
  753. "pf": "2",
  754. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  755. }
  756. }
  757. urllib3.disable_warnings()
  758. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  759. hour_play_cnt = r.json()["data"]["play_pv"]
  760. hour_cover_url = r.json()["data"]["url"]
  761. hour_video_url = r.json()["data"]["v_url"]
  762. hour_video_duration = r.json()["data"]["du"]
  763. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  764. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  765. hour_video_share_cnt = r.json()["data"]["share"]
  766. hour_video_width = r.json()["data"]["w"]
  767. hour_video_height = r.json()["data"]["h"]
  768. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  769. hour_video_send_time = r.json()["data"]["t"]
  770. hour_user_name = r.json()["data"]["user"]["nick"]
  771. hour_head_url = r.json()["data"]["user"]["hurl"]
  772. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  773. # 下载封面
  774. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  775. # 下载视频
  776. Common.download_method(log_type, "video", v_title, hour_video_url)
  777. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  778. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  779. f_a.write(str(v_id) + "\n" +
  780. str(v_title) + "\n" +
  781. str(int(int(hour_video_duration) / 1000)) + "\n" +
  782. str(hour_play_cnt) + "\n" +
  783. str(hour_video_comment_cnt) + "\n" +
  784. str(hour_video_like_cnt) + "\n" +
  785. str(hour_video_share_cnt) + "\n" +
  786. str(hour_video_resolution) + "\n" +
  787. str(hour_video_send_time) + "\n" +
  788. str(hour_user_name) + "\n" +
  789. str(hour_head_url) + "\n" +
  790. str(hour_video_url) + "\n" +
  791. str(hour_cover_url) + "\n" +
  792. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  793. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  794. # 上传视频
  795. Common.logger(log_type).info("开始上传视频")
  796. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  797. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  798. our_video_id) + "/info"
  799. Common.logger(log_type).info("视频上传完成:{}", v_title)
  800. # 上传完成时间
  801. upload_time = int(time.time())
  802. # 保存视频信息到云文档
  803. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  804. # 插入空行
  805. time.sleep(1)
  806. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  807. # 视频信息写入云文档
  808. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  809. "小时级上升榜",
  810. v_id,
  811. v_title,
  812. our_video_link,
  813. hour_play_cnt,
  814. hour_video_comment_cnt,
  815. hour_video_like_cnt,
  816. hour_video_share_cnt,
  817. int(hour_video_duration) / 1000,
  818. hour_video_resolution,
  819. time.strftime("%Y/%m/%d %H:%M:%S",
  820. time.localtime(int(hour_video_send_time) / 1000)),
  821. hour_user_name,
  822. p_id,
  823. p_mid,
  824. hour_head_url,
  825. hour_cover_url,
  826. hour_video_url]]
  827. time.sleep(1)
  828. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  829. except Exception as e:
  830. Common.logger(log_type).error("下载视频异常:{}", e)
  831. # 上升榜判断逻辑,任意时间段上升量>=5000,连续两个时间段上升量>=2000
  832. elif int(ten_cnt) >= 5000 or int(fifteen_cnt) >= 5000 or int(twenty_cnt) >= 5000:
  833. Common.logger(log_type).info("10:00 or 15:00 or 20:00 数据上升量:{} or {} or {} >= 5000",
  834. ten_cnt, fifteen_cnt, twenty_cnt)
  835. Common.logger(log_type).info("满足下载规则,开始下载视频")
  836. try:
  837. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  838. headers = {
  839. "x-b3-traceid": cls.hour_x_b3_traceid,
  840. "X-Token-Id": cls.hour_x_token_id,
  841. "uid": cls.hour_uid,
  842. "content-type": "application/json",
  843. "Accept-Encoding": "gzip,compress,br,deflate",
  844. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  845. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  846. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  847. "Referer": cls.hour_referer
  848. }
  849. data = {
  850. "play_src": "1",
  851. "profile_id": int(p_id),
  852. "profile_mid": int(p_mid),
  853. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  854. "!400x400r/crop/400x400/interlace/1/format/jpg",
  855. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  856. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  857. "share_width": 625,
  858. "share_height": 500,
  859. "no_comments": True,
  860. "no_follow": True,
  861. "vid": v_id,
  862. "hot_l1_comment": True,
  863. "token": cls.hour_token,
  864. "uid": cls.hour_uid,
  865. "proj": "ma",
  866. "wx_ver": "8.0.20",
  867. "code_ver": "3.62.0",
  868. "log_common_params": {
  869. "e": [{
  870. "data": {
  871. "page": "dynamicSharePage"
  872. }
  873. }],
  874. "ext": {
  875. "brand": "iPhone",
  876. "device": "iPhone 11",
  877. "os": "iOS 14.7.1",
  878. "weixinver": "8.0.20",
  879. "srcver": "2.24.3",
  880. "net": "wifi",
  881. "scene": "1089"
  882. },
  883. "pj": "1",
  884. "pf": "2",
  885. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  886. }
  887. }
  888. urllib3.disable_warnings()
  889. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  890. hour_play_cnt = r.json()["data"]["play_pv"]
  891. hour_cover_url = r.json()["data"]["url"]
  892. hour_video_url = r.json()["data"]["v_url"]
  893. hour_video_duration = r.json()["data"]["du"]
  894. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  895. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  896. hour_video_share_cnt = r.json()["data"]["share"]
  897. hour_video_width = r.json()["data"]["w"]
  898. hour_video_height = r.json()["data"]["h"]
  899. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  900. hour_video_send_time = r.json()["data"]["t"]
  901. hour_user_name = r.json()["data"]["user"]["nick"]
  902. hour_head_url = r.json()["data"]["user"]["hurl"]
  903. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  904. # 下载封面
  905. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  906. # 下载视频
  907. Common.download_method(log_type, "video", v_title, hour_video_url)
  908. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  909. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  910. f_a.write(str(v_id) + "\n" +
  911. str(v_title) + "\n" +
  912. str(int(int(hour_video_duration) / 1000)) + "\n" +
  913. str(hour_play_cnt) + "\n" +
  914. str(hour_video_comment_cnt) + "\n" +
  915. str(hour_video_like_cnt) + "\n" +
  916. str(hour_video_share_cnt) + "\n" +
  917. str(hour_video_resolution) + "\n" +
  918. str(hour_video_send_time) + "\n" +
  919. str(hour_user_name) + "\n" +
  920. str(hour_head_url) + "\n" +
  921. str(hour_video_url) + "\n" +
  922. str(hour_cover_url) + "\n" +
  923. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  924. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  925. # 上传视频
  926. Common.logger(log_type).info("开始上传视频")
  927. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  928. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  929. our_video_id) + "/info"
  930. Common.logger(log_type).info("视频上传完成:{}", v_title)
  931. # 上传完成时间
  932. upload_time = int(time.time())
  933. # 保存视频信息到云文档
  934. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  935. # 插入空行
  936. time.sleep(1)
  937. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  938. # 视频信息写入云文档
  939. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  940. "小时级上升榜",
  941. v_id,
  942. v_title,
  943. our_video_link,
  944. hour_play_cnt,
  945. hour_video_comment_cnt,
  946. hour_video_like_cnt,
  947. hour_video_share_cnt,
  948. int(hour_video_duration) / 1000,
  949. hour_video_resolution,
  950. time.strftime("%Y/%m/%d %H:%M:%S",
  951. time.localtime(int(hour_video_send_time) / 1000)),
  952. hour_user_name,
  953. p_id,
  954. p_mid,
  955. hour_head_url,
  956. hour_cover_url,
  957. hour_video_url]]
  958. time.sleep(1)
  959. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  960. except Exception as e:
  961. Common.logger(log_type).error("下载视频异常:{}", e)
  962. elif int(ten_cnt) >= 2000 and int(fifteen_cnt) >= 2000:
  963. Common.logger(log_type).info(
  964. "10:00 and 15:00 数据上升量:{} and {} >= 2000", ten_cnt, fifteen_cnt)
  965. Common.logger(log_type).info("满足下载规则,开始下载视频")
  966. try:
  967. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  968. headers = {
  969. "x-b3-traceid": cls.hour_x_b3_traceid,
  970. "X-Token-Id": cls.hour_x_token_id,
  971. "uid": cls.hour_uid,
  972. "content-type": "application/json",
  973. "Accept-Encoding": "gzip,compress,br,deflate",
  974. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  975. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  976. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  977. "Referer": cls.hour_referer
  978. }
  979. data = {
  980. "play_src": "1",
  981. "profile_id": int(p_id),
  982. "profile_mid": int(p_mid),
  983. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  984. "!400x400r/crop/400x400/interlace/1/format/jpg",
  985. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  986. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  987. "share_width": 625,
  988. "share_height": 500,
  989. "no_comments": True,
  990. "no_follow": True,
  991. "vid": v_id,
  992. "hot_l1_comment": True,
  993. "token": cls.hour_token,
  994. "uid": cls.hour_uid,
  995. "proj": "ma",
  996. "wx_ver": "8.0.20",
  997. "code_ver": "3.62.0",
  998. "log_common_params": {
  999. "e": [{
  1000. "data": {
  1001. "page": "dynamicSharePage"
  1002. }
  1003. }],
  1004. "ext": {
  1005. "brand": "iPhone",
  1006. "device": "iPhone 11",
  1007. "os": "iOS 14.7.1",
  1008. "weixinver": "8.0.20",
  1009. "srcver": "2.24.3",
  1010. "net": "wifi",
  1011. "scene": "1089"
  1012. },
  1013. "pj": "1",
  1014. "pf": "2",
  1015. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1016. }
  1017. }
  1018. urllib3.disable_warnings()
  1019. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1020. hour_play_cnt = r.json()["data"]["play_pv"]
  1021. hour_cover_url = r.json()["data"]["url"]
  1022. hour_video_url = r.json()["data"]["v_url"]
  1023. hour_video_duration = r.json()["data"]["du"]
  1024. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1025. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1026. hour_video_share_cnt = r.json()["data"]["share"]
  1027. hour_video_width = r.json()["data"]["w"]
  1028. hour_video_height = r.json()["data"]["h"]
  1029. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1030. hour_video_send_time = r.json()["data"]["t"]
  1031. hour_user_name = r.json()["data"]["user"]["nick"]
  1032. hour_head_url = r.json()["data"]["user"]["hurl"]
  1033. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  1034. # 下载封面
  1035. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  1036. # 下载视频
  1037. Common.download_method(log_type, "video", v_title, hour_video_url)
  1038. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1039. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1040. f_a.write(str(v_id) + "\n" +
  1041. str(v_title) + "\n" +
  1042. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1043. str(hour_play_cnt) + "\n" +
  1044. str(hour_video_comment_cnt) + "\n" +
  1045. str(hour_video_like_cnt) + "\n" +
  1046. str(hour_video_share_cnt) + "\n" +
  1047. str(hour_video_resolution) + "\n" +
  1048. str(hour_video_send_time) + "\n" +
  1049. str(hour_user_name) + "\n" +
  1050. str(hour_head_url) + "\n" +
  1051. str(hour_video_url) + "\n" +
  1052. str(hour_cover_url) + "\n" +
  1053. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1054. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  1055. # 上传
  1056. Common.logger(log_type).info("开始上传视频")
  1057. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  1058. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  1059. our_video_id) + "/info"
  1060. Common.logger(log_type).info("视频上传完成:{}", v_title)
  1061. # 上传完成时间
  1062. upload_time = int(time.time())
  1063. # 保存视频信息到云文档
  1064. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  1065. # 插入空行
  1066. time.sleep(1)
  1067. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1068. # 视频信息写入云文档
  1069. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  1070. "小时级上升榜",
  1071. v_id,
  1072. v_title,
  1073. our_video_link,
  1074. hour_play_cnt,
  1075. hour_video_comment_cnt,
  1076. hour_video_like_cnt,
  1077. hour_video_share_cnt,
  1078. int(hour_video_duration) / 1000,
  1079. hour_video_resolution,
  1080. time.strftime("%Y/%m/%d %H:%M:%S",
  1081. time.localtime(int(hour_video_send_time) / 1000)),
  1082. hour_user_name,
  1083. p_id,
  1084. p_mid,
  1085. hour_head_url,
  1086. hour_cover_url,
  1087. hour_video_url]]
  1088. time.sleep(1)
  1089. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  1090. except Exception as e:
  1091. Common.logger(log_type).error("下载视频异常:{}", e)
  1092. elif int(fifteen_cnt) >= 2000 and int(twenty_cnt) >= 2000:
  1093. Common.logger(log_type).info(
  1094. "15:00 and 20:00 数据上升量:{} and {} >= 2000", fifteen_cnt, twenty_cnt)
  1095. Common.logger(log_type).info("满足下载规则,开始下载视频")
  1096. try:
  1097. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  1098. headers = {
  1099. "x-b3-traceid": cls.hour_x_b3_traceid,
  1100. "X-Token-Id": cls.hour_x_token_id,
  1101. "uid": cls.hour_uid,
  1102. "content-type": "application/json",
  1103. "Accept-Encoding": "gzip,compress,br,deflate",
  1104. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  1105. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  1106. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  1107. "Referer": cls.hour_referer
  1108. }
  1109. data = {
  1110. "play_src": "1",
  1111. "profile_id": int(p_id),
  1112. "profile_mid": int(p_mid),
  1113. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  1114. "!400x400r/crop/400x400/interlace/1/format/jpg",
  1115. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  1116. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  1117. "share_width": 625,
  1118. "share_height": 500,
  1119. "no_comments": True,
  1120. "no_follow": True,
  1121. "vid": v_id,
  1122. "hot_l1_comment": True,
  1123. "token": cls.hour_token,
  1124. "uid": cls.hour_uid,
  1125. "proj": "ma",
  1126. "wx_ver": "8.0.20",
  1127. "code_ver": "3.62.0",
  1128. "log_common_params": {
  1129. "e": [{
  1130. "data": {
  1131. "page": "dynamicSharePage"
  1132. }
  1133. }],
  1134. "ext": {
  1135. "brand": "iPhone",
  1136. "device": "iPhone 11",
  1137. "os": "iOS 14.7.1",
  1138. "weixinver": "8.0.20",
  1139. "srcver": "2.24.3",
  1140. "net": "wifi",
  1141. "scene": "1089"
  1142. },
  1143. "pj": "1",
  1144. "pf": "2",
  1145. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1146. }
  1147. }
  1148. urllib3.disable_warnings()
  1149. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1150. hour_play_cnt = r.json()["data"]["play_pv"]
  1151. hour_cover_url = r.json()["data"]["url"]
  1152. hour_video_url = r.json()["data"]["v_url"]
  1153. hour_video_duration = r.json()["data"]["du"]
  1154. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1155. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1156. hour_video_share_cnt = r.json()["data"]["share"]
  1157. hour_video_width = r.json()["data"]["w"]
  1158. hour_video_height = r.json()["data"]["h"]
  1159. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1160. hour_video_send_time = r.json()["data"]["t"]
  1161. hour_user_name = r.json()["data"]["user"]["nick"]
  1162. hour_head_url = r.json()["data"]["user"]["hurl"]
  1163. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  1164. # 下载封面
  1165. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  1166. # 下载视频
  1167. Common.download_method(log_type, "video", v_title, hour_video_url)
  1168. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1169. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1170. f_a.write(str(v_id) + "\n" +
  1171. str(v_title) + "\n" +
  1172. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1173. str(hour_play_cnt) + "\n" +
  1174. str(hour_video_comment_cnt) + "\n" +
  1175. str(hour_video_like_cnt) + "\n" +
  1176. str(hour_video_share_cnt) + "\n" +
  1177. str(hour_video_resolution) + "\n" +
  1178. str(hour_video_send_time) + "\n" +
  1179. str(hour_user_name) + "\n" +
  1180. str(hour_head_url) + "\n" +
  1181. str(hour_video_url) + "\n" +
  1182. str(hour_cover_url) + "\n" +
  1183. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1184. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  1185. # 上传
  1186. Common.logger(log_type).info("开始上传视频")
  1187. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  1188. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  1189. our_video_id) + "/info"
  1190. Common.logger(log_type).info("视频上传完成:{}", v_title)
  1191. # 上传完成时间
  1192. upload_time = int(time.time())
  1193. # 保存视频信息到云文档
  1194. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  1195. # 插入空行
  1196. time.sleep(1)
  1197. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1198. # 视频信息写入云文档
  1199. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  1200. "小时级上升榜",
  1201. v_id,
  1202. v_title,
  1203. our_video_link,
  1204. hour_play_cnt,
  1205. hour_video_comment_cnt,
  1206. hour_video_like_cnt,
  1207. hour_video_share_cnt,
  1208. int(hour_video_duration) / 1000,
  1209. hour_video_resolution,
  1210. time.strftime("%Y/%m/%d %H:%M:%S",
  1211. time.localtime(int(hour_video_send_time) / 1000)),
  1212. hour_user_name,
  1213. p_id,
  1214. p_mid,
  1215. hour_head_url,
  1216. hour_cover_url,
  1217. hour_video_url]]
  1218. time.sleep(1)
  1219. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  1220. except Exception as e:
  1221. Common.logger(log_type).error("下载视频异常:{}", e)
  1222. elif int(yesterday_twenty_cnt) >= 2000 and int(ten_cnt) >= 2000:
  1223. Common.logger(log_type).info("昨日20:00 and 今日10:00 数据上升量:{} and {} >= 2000",
  1224. yesterday_twenty_cnt, ten_cnt)
  1225. Common.logger(log_type).info("满足下载规则,开始下载视频")
  1226. try:
  1227. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  1228. headers = {
  1229. "x-b3-traceid": cls.hour_x_b3_traceid,
  1230. "X-Token-Id": cls.hour_x_token_id,
  1231. "uid": cls.hour_uid,
  1232. "content-type": "application/json",
  1233. "Accept-Encoding": "gzip,compress,br,deflate",
  1234. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  1235. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  1236. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  1237. "Referer": cls.hour_referer
  1238. }
  1239. data = {
  1240. "play_src": "1",
  1241. "profile_id": int(p_id),
  1242. "profile_mid": int(p_mid),
  1243. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  1244. "!400x400r/crop/400x400/interlace/1/format/jpg",
  1245. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  1246. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  1247. "share_width": 625,
  1248. "share_height": 500,
  1249. "no_comments": True,
  1250. "no_follow": True,
  1251. "vid": v_id,
  1252. "hot_l1_comment": True,
  1253. "token": cls.hour_token,
  1254. "uid": cls.hour_uid,
  1255. "proj": "ma",
  1256. "wx_ver": "8.0.20",
  1257. "code_ver": "3.62.0",
  1258. "log_common_params": {
  1259. "e": [{
  1260. "data": {
  1261. "page": "dynamicSharePage"
  1262. }
  1263. }],
  1264. "ext": {
  1265. "brand": "iPhone",
  1266. "device": "iPhone 11",
  1267. "os": "iOS 14.7.1",
  1268. "weixinver": "8.0.20",
  1269. "srcver": "2.24.3",
  1270. "net": "wifi",
  1271. "scene": "1089"
  1272. },
  1273. "pj": "1",
  1274. "pf": "2",
  1275. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1276. }
  1277. }
  1278. urllib3.disable_warnings()
  1279. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1280. hour_play_cnt = r.json()["data"]["play_pv"]
  1281. hour_cover_url = r.json()["data"]["url"]
  1282. hour_video_url = r.json()["data"]["v_url"]
  1283. hour_video_duration = r.json()["data"]["du"]
  1284. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1285. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1286. hour_video_share_cnt = r.json()["data"]["share"]
  1287. hour_video_width = r.json()["data"]["w"]
  1288. hour_video_height = r.json()["data"]["h"]
  1289. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1290. hour_video_send_time = r.json()["data"]["t"]
  1291. hour_user_name = r.json()["data"]["user"]["nick"]
  1292. hour_head_url = r.json()["data"]["user"]["hurl"]
  1293. Common.logger(log_type).info("视频详情,当前播放量:{}", hour_play_cnt)
  1294. # 下载封面
  1295. Common.download_method(log_type, "cover", v_title, hour_cover_url)
  1296. # 下载视频
  1297. Common.download_method(log_type, "video", v_title, hour_video_url)
  1298. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1299. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1300. f_a.write(str(v_id) + "\n" +
  1301. str(v_title) + "\n" +
  1302. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1303. str(hour_play_cnt) + "\n" +
  1304. str(hour_video_comment_cnt) + "\n" +
  1305. str(hour_video_like_cnt) + "\n" +
  1306. str(hour_video_share_cnt) + "\n" +
  1307. str(hour_video_resolution) + "\n" +
  1308. str(hour_video_send_time) + "\n" +
  1309. str(hour_user_name) + "\n" +
  1310. str(hour_head_url) + "\n" +
  1311. str(hour_video_url) + "\n" +
  1312. str(hour_cover_url) + "\n" +
  1313. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1314. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  1315. # 上传
  1316. Common.logger(log_type).info("开始上传视频")
  1317. our_video_id = Publish.upload_and_publish(log_type, "prod", "up")
  1318. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  1319. our_video_id) + "/info"
  1320. Common.logger(log_type).info("视频上传完成:{}", v_title)
  1321. # 上传完成时间
  1322. upload_time = int(time.time())
  1323. # 保存视频信息到云文档
  1324. Common.logger(log_type).info("添加视频到云文档:{}", v_title)
  1325. # 插入空行
  1326. time.sleep(1)
  1327. Feishu.insert_columns(log_type, "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1328. # 视频信息写入云文档
  1329. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(upload_time))),
  1330. "小时级上升榜",
  1331. v_id,
  1332. v_title,
  1333. our_video_link,
  1334. hour_play_cnt,
  1335. hour_video_comment_cnt,
  1336. hour_video_like_cnt,
  1337. hour_video_share_cnt,
  1338. int(hour_video_duration) / 1000,
  1339. hour_video_resolution,
  1340. time.strftime("%Y/%m/%d %H:%M:%S",
  1341. time.localtime(int(hour_video_send_time) / 1000)),
  1342. hour_user_name,
  1343. p_id,
  1344. p_mid,
  1345. hour_head_url,
  1346. hour_cover_url,
  1347. hour_video_url]]
  1348. time.sleep(1)
  1349. Feishu.update_values(log_type, "xiaoniangao", "yatRv2", "F2:W2", values)
  1350. except Exception as e:
  1351. Common.logger(log_type).error("下载视频异常:{}", e)
  1352. else:
  1353. Common.logger(log_type).info("上升量不满足下载规则")
  1354. except Exception as e:
  1355. Common.logger(log_type).error("下载视频时,获取飞书信息异常:{}\n", e)
  1356. if __name__ == "__main__":
  1357. HourList.get_hour_list_feeds("hour")
  1358. pass