hour_list.py 79 KB

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