hour_list.py 79 KB

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