hour_list.py 79 KB

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