hour_list.py 79 KB

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