hour_list.py 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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. # 发布时间
  720. time.sleep(1)
  721. v_upload_time = Feishu.get_range_value(
  722. "logs", "xiaoniangao", "ba0da4", "J" + str(i) + ":" + "J" + str(i))[0]
  723. v_send_time = int(time.mktime(time.strptime(v_upload_time, "%Y-%m-%d %H:%M:%S")))
  724. # 判断视频 ID 长度,不大于 13 位
  725. if len(str(v_id)) > 13:
  726. Common.logger().info("视频ID长度大于13位:{}", v_id)
  727. elif int(time.time()) - int(v_send_time) >= 172800:
  728. Common.logger().info("抓取时间超过 2 天")
  729. return
  730. # 判断视频时长:1-10min
  731. elif int(v_duration) > 600 or int(v_duration) < 60:
  732. Common.logger().info("视频时长不在 1-10分钟 之间")
  733. # 过滤带字母的视频ID
  734. elif any(word if word in v_id else False for word in cls.sensitive_videoid_words()) is True:
  735. Common.logger().info("视频ID带字母:{}".format(v_id))
  736. # 从云文档中去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
  737. elif v_id in [j for i in Feishu.get_values_batch("logs", "xiaoniangao", "yatRv2") for j in i]:
  738. Common.logger().info("该视频已下载:{}", v_title)
  739. time.sleep(1)
  740. # 上升榜判断逻辑,任意时间段上升量>=5000,连续两个时间段上升量>=2000
  741. elif int(ten_cnt) >= 5000 or int(fifteen_cnt) >= 5000 or int(twenty_cnt) >= 5000:
  742. Common.logger().info("10:00 or 15:00 or 20:00 数据上升量:{} or {} or {} >= 5000",
  743. ten_cnt, fifteen_cnt, twenty_cnt)
  744. Common.logger().info("满足下载规则,开始下载视频")
  745. try:
  746. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  747. headers = {
  748. "x-b3-traceid": cls.hour_x_b3_traceid,
  749. "X-Token-Id": cls.hour_x_token_id,
  750. "uid": cls.hour_uid,
  751. "content-type": "application/json",
  752. "Accept-Encoding": "gzip,compress,br,deflate",
  753. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  754. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  755. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  756. "Referer": cls.hour_referer
  757. }
  758. data = {
  759. "play_src": "1",
  760. "profile_id": int(p_id),
  761. "profile_mid": int(p_mid),
  762. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  763. "!400x400r/crop/400x400/interlace/1/format/jpg",
  764. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  765. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  766. "share_width": 625,
  767. "share_height": 500,
  768. "no_comments": True,
  769. "no_follow": True,
  770. "vid": v_id,
  771. "hot_l1_comment": True,
  772. "token": cls.hour_token,
  773. "uid": cls.hour_uid,
  774. "proj": "ma",
  775. "wx_ver": "8.0.20",
  776. "code_ver": "3.62.0",
  777. "log_common_params": {
  778. "e": [{
  779. "data": {
  780. "page": "dynamicSharePage"
  781. }
  782. }],
  783. "ext": {
  784. "brand": "iPhone",
  785. "device": "iPhone 11",
  786. "os": "iOS 14.7.1",
  787. "weixinver": "8.0.20",
  788. "srcver": "2.24.3",
  789. "net": "wifi",
  790. "scene": "1089"
  791. },
  792. "pj": "1",
  793. "pf": "2",
  794. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  795. }
  796. }
  797. urllib3.disable_warnings()
  798. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  799. hour_play_cnt = r.json()["data"]["play_pv"]
  800. hour_cover_url = r.json()["data"]["url"]
  801. hour_video_url = r.json()["data"]["v_url"]
  802. hour_video_duration = r.json()["data"]["du"]
  803. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  804. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  805. hour_video_share_cnt = r.json()["data"]["share"]
  806. hour_video_width = r.json()["data"]["w"]
  807. hour_video_height = r.json()["data"]["h"]
  808. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  809. hour_video_send_time = r.json()["data"]["t"]
  810. hour_user_name = r.json()["data"]["user"]["nick"]
  811. hour_head_url = r.json()["data"]["user"]["hurl"]
  812. Common.logger().info("视频详情,当前播放量:{}", hour_play_cnt)
  813. # 下载封面
  814. Common.download_method("logs", "cover", v_title, hour_cover_url)
  815. # 下载视频
  816. Common.download_method("logs", "video", v_title, hour_video_url)
  817. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  818. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  819. f_a.write(str(v_id) + "\n" +
  820. str(v_title) + "\n" +
  821. str(int(int(hour_video_duration) / 1000)) + "\n" +
  822. str(hour_play_cnt) + "\n" +
  823. str(hour_video_comment_cnt) + "\n" +
  824. str(hour_video_like_cnt) + "\n" +
  825. str(hour_video_share_cnt) + "\n" +
  826. str(hour_video_resolution) + "\n" +
  827. str(hour_video_send_time) + "\n" +
  828. str(hour_user_name) + "\n" +
  829. str(hour_head_url) + "\n" +
  830. str(hour_video_url) + "\n" +
  831. str(hour_cover_url) + "\n" +
  832. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  833. Common.logger().info("==========视频信息已保存至info.txt==========")
  834. Common.logger().info("开始上传视频")
  835. Publish.upload_and_publish("prod", "up")
  836. Common.logger().info("视频上传完成:{}", v_title)
  837. # 上传完成时间
  838. upload_time = int(time.time())
  839. # 保存视频信息到云文档
  840. Common.logger().info("添加视频到云文档:{}", v_title)
  841. # 插入空行
  842. time.sleep(1)
  843. Feishu.insert_columns("logs", "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  844. # 视频信息写入云文档
  845. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  846. "小时级上升榜",
  847. v_id,
  848. v_title,
  849. hour_play_cnt,
  850. hour_video_comment_cnt,
  851. hour_video_like_cnt,
  852. hour_video_share_cnt,
  853. int(hour_video_duration)/1000,
  854. hour_video_resolution,
  855. time.strftime("%Y-%m-%d %H:%M:%S",
  856. time.localtime(int(hour_video_send_time) / 1000)),
  857. hour_user_name,
  858. p_id,
  859. p_mid,
  860. hour_head_url,
  861. hour_cover_url,
  862. hour_video_url]]
  863. time.sleep(1)
  864. Feishu.update_values("logs", "xiaoniangao", "yatRv2", "A2:Q2", values)
  865. except Exception as e:
  866. Common.logger().error("下载视频异常:{}", e)
  867. elif int(ten_cnt) >= 2000 and int(fifteen_cnt) >= 2000:
  868. Common.logger().info("10:00 and 15:00 数据上升量:{} and {} >= 2000", ten_cnt, fifteen_cnt)
  869. Common.logger().info("满足下载规则,开始下载视频")
  870. try:
  871. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  872. headers = {
  873. "x-b3-traceid": cls.hour_x_b3_traceid,
  874. "X-Token-Id": cls.hour_x_token_id,
  875. "uid": cls.hour_uid,
  876. "content-type": "application/json",
  877. "Accept-Encoding": "gzip,compress,br,deflate",
  878. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  879. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  880. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  881. "Referer": cls.hour_referer
  882. }
  883. data = {
  884. "play_src": "1",
  885. "profile_id": int(p_id),
  886. "profile_mid": int(p_mid),
  887. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  888. "!400x400r/crop/400x400/interlace/1/format/jpg",
  889. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  890. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  891. "share_width": 625,
  892. "share_height": 500,
  893. "no_comments": True,
  894. "no_follow": True,
  895. "vid": v_id,
  896. "hot_l1_comment": True,
  897. "token": cls.hour_token,
  898. "uid": cls.hour_uid,
  899. "proj": "ma",
  900. "wx_ver": "8.0.20",
  901. "code_ver": "3.62.0",
  902. "log_common_params": {
  903. "e": [{
  904. "data": {
  905. "page": "dynamicSharePage"
  906. }
  907. }],
  908. "ext": {
  909. "brand": "iPhone",
  910. "device": "iPhone 11",
  911. "os": "iOS 14.7.1",
  912. "weixinver": "8.0.20",
  913. "srcver": "2.24.3",
  914. "net": "wifi",
  915. "scene": "1089"
  916. },
  917. "pj": "1",
  918. "pf": "2",
  919. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  920. }
  921. }
  922. urllib3.disable_warnings()
  923. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  924. hour_play_cnt = r.json()["data"]["play_pv"]
  925. hour_cover_url = r.json()["data"]["url"]
  926. hour_video_url = r.json()["data"]["v_url"]
  927. hour_video_duration = r.json()["data"]["du"]
  928. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  929. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  930. hour_video_share_cnt = r.json()["data"]["share"]
  931. hour_video_width = r.json()["data"]["w"]
  932. hour_video_height = r.json()["data"]["h"]
  933. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  934. hour_video_send_time = r.json()["data"]["t"]
  935. hour_user_name = r.json()["data"]["user"]["nick"]
  936. hour_head_url = r.json()["data"]["user"]["hurl"]
  937. Common.logger().info("视频详情,当前播放量:{}", hour_play_cnt)
  938. # 下载封面
  939. Common.download_method("logs", "cover", v_title, hour_cover_url)
  940. # 下载视频
  941. Common.download_method("logs", "video", v_title, hour_video_url)
  942. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  943. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  944. f_a.write(str(v_id) + "\n" +
  945. str(v_title) + "\n" +
  946. str(int(int(hour_video_duration) / 1000)) + "\n" +
  947. str(hour_play_cnt) + "\n" +
  948. str(hour_video_comment_cnt) + "\n" +
  949. str(hour_video_like_cnt) + "\n" +
  950. str(hour_video_share_cnt) + "\n" +
  951. str(hour_video_resolution) + "\n" +
  952. str(hour_video_send_time) + "\n" +
  953. str(hour_user_name) + "\n" +
  954. str(hour_head_url) + "\n" +
  955. str(hour_video_url) + "\n" +
  956. str(hour_cover_url) + "\n" +
  957. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  958. Common.logger().info("==========视频信息已保存至info.txt==========")
  959. Common.logger().info("开始上传视频")
  960. Publish.upload_and_publish("prod", "up")
  961. Common.logger().info("视频上传完成:{}", v_title)
  962. # 上传完成时间
  963. upload_time = int(time.time())
  964. # 保存视频信息到云文档
  965. Common.logger().info("添加视频到云文档:{}", v_title)
  966. # 插入空行
  967. time.sleep(1)
  968. Feishu.insert_columns("logs", "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  969. # 视频信息写入云文档
  970. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  971. "小时级上升榜",
  972. v_id,
  973. v_title,
  974. hour_play_cnt,
  975. hour_video_comment_cnt,
  976. hour_video_like_cnt,
  977. hour_video_share_cnt,
  978. int(hour_video_duration)/1000,
  979. hour_video_resolution,
  980. time.strftime("%Y-%m-%d %H:%M:%S",
  981. time.localtime(int(hour_video_send_time) / 1000)),
  982. hour_user_name,
  983. p_id,
  984. p_mid,
  985. hour_head_url,
  986. hour_cover_url,
  987. hour_video_url]]
  988. time.sleep(1)
  989. Feishu.update_values("logs", "xiaoniangao", "yatRv2", "A2:Q2", values)
  990. except Exception as e:
  991. Common.logger().error("下载视频异常:{}", e)
  992. elif int(fifteen_cnt) >= 2000 and int(twenty_cnt) >= 2000:
  993. Common.logger().info("15:00 and 20:00 数据上升量:{} and {} >= 2000", fifteen_cnt, twenty_cnt)
  994. Common.logger().info("满足下载规则,开始下载视频")
  995. try:
  996. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  997. headers = {
  998. "x-b3-traceid": cls.hour_x_b3_traceid,
  999. "X-Token-Id": cls.hour_x_token_id,
  1000. "uid": cls.hour_uid,
  1001. "content-type": "application/json",
  1002. "Accept-Encoding": "gzip,compress,br,deflate",
  1003. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  1004. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  1005. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  1006. "Referer": cls.hour_referer
  1007. }
  1008. data = {
  1009. "play_src": "1",
  1010. "profile_id": int(p_id),
  1011. "profile_mid": int(p_mid),
  1012. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  1013. "!400x400r/crop/400x400/interlace/1/format/jpg",
  1014. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  1015. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  1016. "share_width": 625,
  1017. "share_height": 500,
  1018. "no_comments": True,
  1019. "no_follow": True,
  1020. "vid": v_id,
  1021. "hot_l1_comment": True,
  1022. "token": cls.hour_token,
  1023. "uid": cls.hour_uid,
  1024. "proj": "ma",
  1025. "wx_ver": "8.0.20",
  1026. "code_ver": "3.62.0",
  1027. "log_common_params": {
  1028. "e": [{
  1029. "data": {
  1030. "page": "dynamicSharePage"
  1031. }
  1032. }],
  1033. "ext": {
  1034. "brand": "iPhone",
  1035. "device": "iPhone 11",
  1036. "os": "iOS 14.7.1",
  1037. "weixinver": "8.0.20",
  1038. "srcver": "2.24.3",
  1039. "net": "wifi",
  1040. "scene": "1089"
  1041. },
  1042. "pj": "1",
  1043. "pf": "2",
  1044. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1045. }
  1046. }
  1047. urllib3.disable_warnings()
  1048. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1049. hour_play_cnt = r.json()["data"]["play_pv"]
  1050. hour_cover_url = r.json()["data"]["url"]
  1051. hour_video_url = r.json()["data"]["v_url"]
  1052. hour_video_duration = r.json()["data"]["du"]
  1053. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1054. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1055. hour_video_share_cnt = r.json()["data"]["share"]
  1056. hour_video_width = r.json()["data"]["w"]
  1057. hour_video_height = r.json()["data"]["h"]
  1058. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1059. hour_video_send_time = r.json()["data"]["t"]
  1060. hour_user_name = r.json()["data"]["user"]["nick"]
  1061. hour_head_url = r.json()["data"]["user"]["hurl"]
  1062. Common.logger().info("视频详情,当前播放量:{}", hour_play_cnt)
  1063. # 下载封面
  1064. Common.download_method("logs", "cover", v_title, hour_cover_url)
  1065. # 下载视频
  1066. Common.download_method("logs", "video", v_title, hour_video_url)
  1067. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1068. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1069. f_a.write(str(v_id) + "\n" +
  1070. str(v_title) + "\n" +
  1071. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1072. str(hour_play_cnt) + "\n" +
  1073. str(hour_video_comment_cnt) + "\n" +
  1074. str(hour_video_like_cnt) + "\n" +
  1075. str(hour_video_share_cnt) + "\n" +
  1076. str(hour_video_resolution) + "\n" +
  1077. str(hour_video_send_time) + "\n" +
  1078. str(hour_user_name) + "\n" +
  1079. str(hour_head_url) + "\n" +
  1080. str(hour_video_url) + "\n" +
  1081. str(hour_cover_url) + "\n" +
  1082. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1083. Common.logger().info("==========视频信息已保存至info.txt==========")
  1084. Common.logger().info("开始上传视频")
  1085. Publish.upload_and_publish("prod", "up")
  1086. Common.logger().info("视频上传完成:{}", v_title)
  1087. # 上传完成时间
  1088. upload_time = int(time.time())
  1089. # 保存视频信息到云文档
  1090. Common.logger().info("添加视频到云文档:{}", v_title)
  1091. # 插入空行
  1092. time.sleep(1)
  1093. Feishu.insert_columns("logs", "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1094. # 视频信息写入云文档
  1095. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  1096. "小时级上升榜",
  1097. v_id,
  1098. v_title,
  1099. hour_play_cnt,
  1100. hour_video_comment_cnt,
  1101. hour_video_like_cnt,
  1102. hour_video_share_cnt,
  1103. int(hour_video_duration)/1000,
  1104. hour_video_resolution,
  1105. time.strftime("%Y-%m-%d %H:%M:%S",
  1106. time.localtime(int(hour_video_send_time) / 1000)),
  1107. hour_user_name,
  1108. p_id,
  1109. p_mid,
  1110. hour_head_url,
  1111. hour_cover_url,
  1112. hour_video_url]]
  1113. time.sleep(1)
  1114. Feishu.update_values("logs", "xiaoniangao", "yatRv2", "A2:Q2", values)
  1115. except Exception as e:
  1116. Common.logger().error("下载视频异常:{}", e)
  1117. elif int(yesterday_twenty_cnt) >= 2000 and int(ten_cnt) >= 2000:
  1118. Common.logger().info("昨日20:00 and 今日10:00 数据上升量:{} and {} >= 2000",
  1119. yesterday_twenty_cnt, ten_cnt)
  1120. Common.logger().info("满足下载规则,开始下载视频")
  1121. try:
  1122. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  1123. headers = {
  1124. "x-b3-traceid": cls.hour_x_b3_traceid,
  1125. "X-Token-Id": cls.hour_x_token_id,
  1126. "uid": cls.hour_uid,
  1127. "content-type": "application/json",
  1128. "Accept-Encoding": "gzip,compress,br,deflate",
  1129. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  1130. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  1131. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  1132. "Referer": cls.hour_referer
  1133. }
  1134. data = {
  1135. "play_src": "1",
  1136. "profile_id": int(p_id),
  1137. "profile_mid": int(p_mid),
  1138. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  1139. "!400x400r/crop/400x400/interlace/1/format/jpg",
  1140. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  1141. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  1142. "share_width": 625,
  1143. "share_height": 500,
  1144. "no_comments": True,
  1145. "no_follow": True,
  1146. "vid": v_id,
  1147. "hot_l1_comment": True,
  1148. "token": cls.hour_token,
  1149. "uid": cls.hour_uid,
  1150. "proj": "ma",
  1151. "wx_ver": "8.0.20",
  1152. "code_ver": "3.62.0",
  1153. "log_common_params": {
  1154. "e": [{
  1155. "data": {
  1156. "page": "dynamicSharePage"
  1157. }
  1158. }],
  1159. "ext": {
  1160. "brand": "iPhone",
  1161. "device": "iPhone 11",
  1162. "os": "iOS 14.7.1",
  1163. "weixinver": "8.0.20",
  1164. "srcver": "2.24.3",
  1165. "net": "wifi",
  1166. "scene": "1089"
  1167. },
  1168. "pj": "1",
  1169. "pf": "2",
  1170. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  1171. }
  1172. }
  1173. urllib3.disable_warnings()
  1174. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  1175. hour_play_cnt = r.json()["data"]["play_pv"]
  1176. hour_cover_url = r.json()["data"]["url"]
  1177. hour_video_url = r.json()["data"]["v_url"]
  1178. hour_video_duration = r.json()["data"]["du"]
  1179. hour_video_comment_cnt = r.json()["data"]["comment_count"]
  1180. hour_video_like_cnt = r.json()["data"]["favor"]["total"]
  1181. hour_video_share_cnt = r.json()["data"]["share"]
  1182. hour_video_width = r.json()["data"]["w"]
  1183. hour_video_height = r.json()["data"]["h"]
  1184. hour_video_resolution = str(hour_video_width) + "*" + str(hour_video_height)
  1185. hour_video_send_time = r.json()["data"]["t"]
  1186. hour_user_name = r.json()["data"]["user"]["nick"]
  1187. hour_head_url = r.json()["data"]["user"]["hurl"]
  1188. Common.logger().info("视频详情,当前播放量:{}", hour_play_cnt)
  1189. # 下载封面
  1190. Common.download_method("logs", "cover", v_title, hour_cover_url)
  1191. # 下载视频
  1192. Common.download_method("logs", "video", v_title, hour_video_url)
  1193. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  1194. with open("./videos/" + v_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  1195. f_a.write(str(v_id) + "\n" +
  1196. str(v_title) + "\n" +
  1197. str(int(int(hour_video_duration) / 1000)) + "\n" +
  1198. str(hour_play_cnt) + "\n" +
  1199. str(hour_video_comment_cnt) + "\n" +
  1200. str(hour_video_like_cnt) + "\n" +
  1201. str(hour_video_share_cnt) + "\n" +
  1202. str(hour_video_resolution) + "\n" +
  1203. str(hour_video_send_time) + "\n" +
  1204. str(hour_user_name) + "\n" +
  1205. str(hour_head_url) + "\n" +
  1206. str(hour_video_url) + "\n" +
  1207. str(hour_cover_url) + "\n" +
  1208. str("90747742180aeb22c0fe3a3c6a38f3d9"))
  1209. Common.logger().info("==========视频信息已保存至info.txt==========")
  1210. Common.logger().info("开始上传视频")
  1211. Publish.upload_and_publish("prod", "up")
  1212. Common.logger().info("视频上传完成:{}", v_title)
  1213. # 上传完成时间
  1214. upload_time = int(time.time())
  1215. # 保存视频信息到云文档
  1216. Common.logger().info("添加视频到云文档:{}", v_title)
  1217. # 插入空行
  1218. time.sleep(1)
  1219. Feishu.insert_columns("logs", "xiaoniangao", "yatRv2", "ROWS", 1, 2)
  1220. # 视频信息写入云文档
  1221. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(upload_time))),
  1222. "小时级上升榜",
  1223. v_id,
  1224. v_title,
  1225. hour_play_cnt,
  1226. hour_video_comment_cnt,
  1227. hour_video_like_cnt,
  1228. hour_video_share_cnt,
  1229. int(hour_video_duration)/1000,
  1230. hour_video_resolution,
  1231. time.strftime("%Y-%m-%d %H:%M:%S",
  1232. time.localtime(int(hour_video_send_time) / 1000)),
  1233. hour_user_name,
  1234. p_id,
  1235. p_mid,
  1236. hour_head_url,
  1237. hour_cover_url,
  1238. hour_video_url]]
  1239. time.sleep(1)
  1240. Feishu.update_values("logs", "xiaoniangao", "yatRv2", "A2:Q2", values)
  1241. except Exception as e:
  1242. Common.logger().error("下载视频异常:{}", e)
  1243. else:
  1244. Common.logger().info("上升量不满足下载规则")
  1245. except Exception as e:
  1246. Common.logger().error("下载视频时,获取飞书信息异常:{}", e)
  1247. if __name__ == "__main__":
  1248. hour_list = HourList()
  1249. # hour_list.get_hour_list_feeds()
  1250. # hour_list.del_null_rows("xiaoniangao", "ba0da4", 3)
  1251. # hour_list.update_hour_list_data(
  1252. # today=datetime.datetime.now().strftime("%Y-%m-%d"),
  1253. # yesterday=(datetime.date.today() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d"),
  1254. # before_yesterday=(datetime.date.today() + datetime.timedelta(days=-2)).strftime("%Y-%m-%d"))
  1255. hour_list.download_and_publish()