hour_list.py 79 KB

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