play_list.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/7/1
  4. import os
  5. import sys
  6. import time
  7. import requests
  8. import urllib3
  9. sys.path.append(os.getcwd())
  10. from main.common import Common
  11. from main.feishu_lib import Feishu
  12. proxies = {"http": None, "https": None}
  13. class PlayList:
  14. # 配置微信
  15. wechat_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "dzcWHw")
  16. hour_x_b3_traceid = wechat_sheet[2][1]
  17. hour_x_token_id = wechat_sheet[3][1]
  18. hour_referer = wechat_sheet[4][1]
  19. hour_uid = wechat_sheet[5][1]
  20. hour_token = wechat_sheet[6][1]
  21. # 过滤敏感词
  22. @classmethod
  23. def sensitive_words(cls, log_type):
  24. # 敏感词库列表
  25. word_list = []
  26. # 从云文档读取所有敏感词,添加到词库列表
  27. time.sleep(1)
  28. lists = Feishu.get_values_batch(log_type, "xiaoniangao", "DRAnZh")
  29. for i in lists:
  30. for j in i:
  31. # 过滤空的单元格内容
  32. if j is None:
  33. pass
  34. else:
  35. word_list.append(j)
  36. return word_list
  37. # 视频ID过滤字母
  38. @classmethod
  39. def sensitive_videoid_words(cls):
  40. # 字母列表
  41. words_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  42. "t", "u", "v", "w", "x", "y", "z",
  43. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
  44. "T", "U", "V", "W", "X", "Y", "Z"]
  45. return words_list
  46. # 基础门槛规则
  47. @staticmethod
  48. def download_rule(d_duration, d_width, d_height, d_play_cnt, d_like_cnt, d_share_cnt, d_send_time):
  49. """
  50. 下载视频的基本规则
  51. :param d_duration: 时长
  52. :param d_width: 宽
  53. :param d_height: 高
  54. :param d_play_cnt: 播放量
  55. :param d_like_cnt: 点赞量
  56. :param d_share_cnt: 分享量
  57. :param d_send_time: 发布时间
  58. :return: 满足规则,返回 True;反之,返回 False
  59. """
  60. # 视频时长
  61. if int(float(d_duration)) >= 40:
  62. # 宽或高
  63. if int(d_width) >= 0 or int(d_height) >= 0:
  64. # 播放量
  65. if int(d_play_cnt) >= 100000:
  66. # 点赞量
  67. if int(d_like_cnt) >= 0:
  68. # 分享量
  69. if int(d_share_cnt) >= 0:
  70. # 发布时间 <= 7 天
  71. if int(time.time()) - int(d_send_time) / 1000 <= 604800:
  72. return True
  73. else:
  74. return False
  75. else:
  76. return False
  77. else:
  78. return False
  79. else:
  80. return False
  81. return False
  82. return False
  83. # 获取列表
  84. @classmethod
  85. def get_hour_list_feeds(cls, log_type):
  86. """
  87. 1.从列表获取视频,7 天内,播放量>=5000
  88. 2.时长 1-10min
  89. 3.每天10:00、15:00、20:00 把符合规则的视频,写入云文档
  90. https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
  91. """
  92. url = "https://kapi.xiaoniangao.cn/trends/get_recommend_trends"
  93. headers = {
  94. "x-b3-traceid": cls.hour_x_b3_traceid,
  95. "X-Token-Id": cls.hour_x_token_id,
  96. "uid": cls.hour_uid,
  97. "content-type": "application/json",
  98. "Accept-Encoding": "gzip,compress,br,deflate",
  99. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  100. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  101. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  102. "Referer": cls.hour_referer
  103. }
  104. data = {
  105. "log_params": {
  106. "page": "discover_rec",
  107. "common": {
  108. "brand": "iPhone",
  109. "device": "iPhone 11",
  110. "os": "iOS 14.7.1",
  111. "weixinver": "8.0.20",
  112. "srcver": "2.24.2",
  113. "net": "wifi",
  114. "scene": 1089
  115. }
  116. },
  117. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!750x500r/crop/750x500/interlace/1/format/jpg",
  118. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!80x80r/crop/80x80/interlace/1/format/jpg",
  119. "share_width": 625,
  120. "share_height": 500,
  121. "ext": {
  122. "fmid": 0,
  123. "items": {}
  124. },
  125. "app": "xng",
  126. "rec_scene": "discover_rec",
  127. "log_common_params": {
  128. "e": [{
  129. "data": {
  130. "page": "discoverIndexPage",
  131. "topic": "recommend"
  132. },
  133. "ab": {}
  134. }],
  135. "ext": {
  136. "brand": "iPhone",
  137. "device": "iPhone 11",
  138. "os": "iOS 14.7.1",
  139. "weixinver": "8.0.20",
  140. "srcver": "2.24.3",
  141. "net": "wifi",
  142. "scene": "1089"
  143. },
  144. "pj": "1",
  145. "pf": "2",
  146. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  147. },
  148. "refresh": False,
  149. "token": cls.hour_token,
  150. "uid": cls.hour_uid,
  151. "proj": "ma",
  152. "wx_ver": "8.0.20",
  153. "code_ver": "3.62.0"
  154. }
  155. try:
  156. urllib3.disable_warnings()
  157. r = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
  158. if "data" not in r.json():
  159. Common.logger(log_type).warning("获取视频feeds错误:{}", r.text)
  160. elif "list" not in r.json()["data"]:
  161. Common.logger(log_type).warning("获取视频feeds无数据,休眠10s:{}", r.json()["data"])
  162. else:
  163. # 视频列表数据
  164. feeds = r.json()["data"]["list"]
  165. for i in range(len(feeds)):
  166. # 标题
  167. if "title" in feeds[i]:
  168. video_title = feeds[i]["title"].strip().replace("\n", "") \
  169. .replace("/", "").replace("\r", "").replace("#", "") \
  170. .replace(".", "。").replace("\\", "").replace("&NBSP", "") \
  171. .replace(":", "").replace("*", "").replace("?", "") \
  172. .replace("?", "").replace('"', "").replace("<", "") \
  173. .replace(">", "").replace("|", "").replace(" ", "")
  174. else:
  175. video_title = 0
  176. # 视频 ID
  177. if "vid" in feeds[i]:
  178. video_id = feeds[i]["vid"]
  179. else:
  180. video_id = 0
  181. # 播放量
  182. if "play_pv" in feeds[i]:
  183. video_play_cnt = feeds[i]["play_pv"]
  184. else:
  185. video_play_cnt = 0
  186. # 点赞量
  187. if "favor" in feeds[i]:
  188. video_like_cnt = feeds[i]["favor"]["total"]
  189. else:
  190. video_like_cnt = 0
  191. # 分享量
  192. if "share" in feeds[i]:
  193. video_share_cnt = feeds[i]["share"]
  194. else:
  195. video_share_cnt = 0
  196. # # 评论量
  197. # if "comment_count" in feeds[i]:
  198. # video_comment_cnt = feeds[i]["comment_count"]
  199. # else:
  200. # video_comment_cnt = 0
  201. # 时长
  202. if "du" in feeds[i]:
  203. video_duration = int(feeds[i]["du"] / 1000)
  204. else:
  205. video_duration = 0
  206. # 宽和高
  207. if "w" or "h" in feeds[i]:
  208. video_width = feeds[i]["w"]
  209. video_height = feeds[i]["h"]
  210. else:
  211. video_width = 0
  212. video_height = 0
  213. # 发布时间
  214. if "t" in feeds[i]:
  215. video_send_time = feeds[i]["t"]
  216. else:
  217. video_send_time = 0
  218. # 用户名 / 头像
  219. if "user" in feeds[i]:
  220. user_name = feeds[i]["user"]["nick"].strip().replace("\n", "") \
  221. .replace("/", "").replace("快手", "").replace(" ", "") \
  222. .replace(" ", "").replace("&NBSP", "").replace("\r", "")
  223. head_url = feeds[i]["user"]["hurl"]
  224. else:
  225. user_name = 0
  226. head_url = 0
  227. # 用户 ID
  228. profile_id = feeds[i]["id"]
  229. # 用户 mid
  230. profile_mid = feeds[i]["user"]["mid"]
  231. # 视频封面
  232. if "url" in feeds[i]:
  233. cover_url = feeds[i]["url"]
  234. else:
  235. cover_url = 0
  236. # 视频播放地址
  237. if "v_url" in feeds[i]:
  238. video_url = feeds[i]["v_url"]
  239. else:
  240. video_url = 0
  241. Common.logger(log_type).info("标题:{}", video_title)
  242. Common.logger(log_type).info("视频ID:{}", video_id)
  243. Common.logger(log_type).info("播放量:{}", video_play_cnt)
  244. # Common.logger(log_type).info("点赞量:{}", video_like_cnt)
  245. # Common.logger(log_type).info("分享量:{}", video_share_cnt)
  246. # Common.logger(log_type).info("评论数:{}", video_comment_cnt)
  247. Common.logger(log_type).info("时长:{}秒", video_duration)
  248. # Common.logger(log_type).info("宽高:{}*{}", video_width, video_height)
  249. Common.logger(log_type).info(
  250. "视频发布时间:{}", time.strftime(
  251. "%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
  252. Common.logger(log_type).info("用户名:{}", user_name)
  253. # Common.logger(log_type).info("用户头像:{}", head_url)
  254. # Common.logger(log_type).info("封面:{}", cover_url)
  255. Common.logger(log_type).info("播放地址:{}", video_url)
  256. # 过滤无效视频
  257. if video_title == 0 or video_id == 0 or video_duration == 0 \
  258. or video_send_time == 0 or user_name == 0 or head_url == 0 \
  259. or cover_url == 0 or video_url == 0:
  260. Common.logger(log_type).warning("无效视频")
  261. elif cls.download_rule(video_duration, video_width, video_height, video_play_cnt,
  262. video_like_cnt, video_share_cnt, video_send_time) is False:
  263. Common.logger(log_type).info("不满足基础门槛规则")
  264. # 过滤敏感词
  265. elif any(word if word in video_title else False for word in
  266. cls.sensitive_words(log_type)) is True:
  267. Common.logger(log_type).info("视频已中敏感词:{}".format(video_title))
  268. time.sleep(1)
  269. # 从云文档中去重:https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=yatRv2
  270. elif video_id in [j for i in Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2") for j in
  271. i]:
  272. Common.logger(log_type).info("该视频已下载:{}", video_title)
  273. time.sleep(1)
  274. else:
  275. Common.logger(log_type).info("该视频未下载,添加至feeds中:{}".format(video_title))
  276. # feeds工作表,插入空行
  277. time.sleep(1)
  278. Feishu.insert_columns(log_type, "xiaoniangao", "ba0da4", "ROWS", 2, 3)
  279. # 获取当前时间
  280. get_feeds_time = int(time.time())
  281. # 看一看云文档,工作表中写入数据
  282. values = [[profile_id,
  283. profile_mid,
  284. video_id,
  285. video_title,
  286. user_name,
  287. video_duration,
  288. cover_url,
  289. video_url,
  290. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)),
  291. str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(get_feeds_time))),
  292. video_play_cnt]]
  293. # 等待 1s,防止操作云文档太频繁,导致报错
  294. time.sleep(1)
  295. Feishu.update_values(log_type, "xiaoniangao", "ba0da4", "A3:K3", values)
  296. except Exception as e:
  297. Common.logger(log_type).error("获取小时榜视频列表异常:{}", e)