hour_list.py 79 KB

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