hour_list.py 78 KB

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