hour_list.py 79 KB

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