hour_list.py 78 KB

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