hour_list.py 79 KB

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