hour_list.py 79 KB

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