get_feeds.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/5/11
  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. # 敏感词库
  14. def sensitive_words():
  15. # 敏感词库列表
  16. word_list = []
  17. # 从云文档读取所有敏感词,添加到词库列表
  18. lists = Feishu.get_values_batch("QQrfQ7")
  19. for i in lists:
  20. for j in i:
  21. # 过滤空的单元格内容
  22. if j is None:
  23. pass
  24. else:
  25. word_list.append(j)
  26. return word_list
  27. # 获取列表数据
  28. def get_feeds():
  29. url = "https://kapi.xiaoniangao.cn/trends/get_recommend_trends"
  30. headers = {
  31. "x-b3-traceid": "17e832d1a42807",
  32. "X-Token-Id": "4bff41a8c35f054fa915dc71b937ac70-1145266232",
  33. "uid": "uid 250e6514-fd83-446c-a880-e274c7f17bce",
  34. "content-type": "application/json",
  35. "Accept-Encoding": "gzip,compress,br,deflate",
  36. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  37. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  38. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  39. "Referer": "https://servicewechat.com/wxd7911e4c177690e4/616/page-frame.html"
  40. }
  41. data = {
  42. "log_params": {
  43. "page": "discover_rec",
  44. "common": {
  45. "brand": "iPhone",
  46. "device": "iPhone 11",
  47. "os": "iOS 14.7.1",
  48. "weixinver": "8.0.20",
  49. "srcver": "2.24.2",
  50. "net": "none"
  51. }
  52. },
  53. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!750x500r/crop/750x500/interlace/1/format/jpg",
  54. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!80x80r/crop/80x80/interlace/1/format/jpg",
  55. "share_width": 625,
  56. "share_height": 500,
  57. "ext": {
  58. "fmid": 0,
  59. "items": {
  60. "15408539": {
  61. "type": "rec",
  62. "pd": 328.31,
  63. "ct": 1649248176494,
  64. "ut": 1649248509951
  65. },
  66. "36776414": {
  67. "type": "nice",
  68. "pd": 33.501,
  69. "ct": 1648893152909,
  70. "ut": 1648893189506
  71. },
  72. "39467179": {
  73. "type": "rec",
  74. "pd": 49.343,
  75. "ct": 1649247992882,
  76. "ut": 1649248043566
  77. },
  78. "40347940": {
  79. "type": "rec",
  80. "pd": 3.681,
  81. "ct": 1649248080042,
  82. "ut": 1649248084907
  83. },
  84. "42285576": {
  85. "type": "rec",
  86. "pd": 0.66,
  87. "ct": 1649248087683,
  88. "ut": 1649248089445
  89. },
  90. "42300668": {
  91. "type": "rec",
  92. "pd": 13.5,
  93. "ct": 1649248059189,
  94. "ut": 1649248074218
  95. },
  96. "42306954": {
  97. "type": "rec",
  98. "pd": 4.918,
  99. "ct": 1649248091376,
  100. "ut": 1649248097663
  101. },
  102. "42328061": {
  103. "type": "reflux",
  104. "pd": 4.001,
  105. "ct": 1649248510102,
  106. "ut": 1649248516806
  107. },
  108. "90004473051301": {
  109. "type": "my",
  110. "pd": 0,
  111. "ct": 1649247069688,
  112. "ut": 1649247069688
  113. },
  114. "80004478730156": {
  115. "type": "other",
  116. "pd": 153,
  117. "ct": 1649247850322,
  118. "ut": 1649247919263
  119. }
  120. }
  121. },
  122. "app": "xng",
  123. "rec_scene": "discover_rec",
  124. "log_common_params": {
  125. "e": [{
  126. "data": {
  127. "page": "discoverIndexPage",
  128. "topic": "recommend"
  129. },
  130. "ab": {}
  131. }],
  132. "ext": {
  133. "brand": "iPhone",
  134. "device": "iPhone 11",
  135. "os": "iOS 14.7.1",
  136. "weixinver": "8.0.20",
  137. "srcver": "2.24.2",
  138. "net": "wifi",
  139. "scene": "1089"
  140. },
  141. "pj": "1",
  142. "pf": "2",
  143. "session_id": "cd944ac9-f35f-47ac-9aa4-2f354afec0e2"
  144. },
  145. "refresh": False,
  146. "token": "132645b6e2b996aaad1713a557456816",
  147. "uid": "250e6514-fd83-446c-a880-e274c7f17bce",
  148. "proj": "ma",
  149. "wx_ver": "8.0.20",
  150. "code_ver": "3.61.0"
  151. }
  152. try:
  153. urllib3.disable_warnings()
  154. r = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
  155. if "data" not in r.json():
  156. Common.logger().warning("获取视频feeds错误:{}", r.text)
  157. elif "list" not in r.json()["data"]:
  158. Common.logger().warning("获取视频feeds无数据,休眠10s:{}", r.json()["data"])
  159. else:
  160. # 视频列表数据
  161. feeds = r.json()["data"]["list"]
  162. for i in range(len(feeds)):
  163. # 标题
  164. if "title" in feeds[i]:
  165. video_title = feeds[i]["title"].strip().replace("\n", "")\
  166. .replace("/", "").replace("\r", "").replace("#", "")\
  167. .replace(".", "。").replace("\\", "").replace("&NBSP", "")\
  168. .replace(":", "").replace("*", "").replace("?", "") \
  169. .replace("?", "").replace('"', "").replace("<", "") \
  170. .replace(">", "").replace("|", "").replace(" ", "")
  171. Common.logger().info("标题:{}", video_title)
  172. else:
  173. video_title = ""
  174. Common.logger().info("当前视频无标题:{}", video_title)
  175. # 视频 ID
  176. if "vid" in feeds[i]:
  177. video_id = feeds[i]["vid"]
  178. Common.logger().info("视频ID:{}", video_id)
  179. else:
  180. video_id = ""
  181. Common.logger().info("当前视频无ID:{}", video_id)
  182. # 播放量
  183. if "play_pv" in feeds[i]:
  184. video_play_cnt = feeds[i]["play_pv"]
  185. Common.logger().info("视频播放量:{}", video_play_cnt)
  186. else:
  187. video_play_cnt = ""
  188. Common.logger().info("当前视频无播放量:{}", video_play_cnt)
  189. # 点赞量
  190. if "favor" in feeds[i]:
  191. video_like_cnt = feeds[i]["favor"]["total"]
  192. Common.logger().info("视频点赞量:{}", video_like_cnt)
  193. else:
  194. video_like_cnt = ""
  195. Common.logger().info("当前视频无点赞量:{}", video_like_cnt)
  196. # 分享量
  197. if "share" in feeds[i]:
  198. video_share_cnt = feeds[i]["share"]
  199. Common.logger().info("视频分享量:{}", video_share_cnt)
  200. else:
  201. video_share_cnt = ""
  202. Common.logger().info("当前视频无分享量:{}", video_share_cnt)
  203. # 评论量
  204. if "comment_count" in feeds[i]:
  205. video_comment_cnt = feeds[i]["comment_count"]
  206. Common.logger().info("视频评论数:{}", video_comment_cnt)
  207. else:
  208. video_comment_cnt = ""
  209. Common.logger().info("当前视频无评论:{}", video_comment_cnt)
  210. # 时长
  211. if "du" in feeds[i]:
  212. video_duration = int(feeds[i]["du"] / 1000)
  213. Common.logger().info("视频时长:{}秒", video_duration)
  214. else:
  215. video_duration = ""
  216. Common.logger().info("当前视频无时长:{}", video_duration)
  217. # 宽和高
  218. if "w" or "h" in feeds[i]:
  219. video_width = feeds[i]["w"]
  220. video_height = feeds[i]["h"]
  221. Common.logger().info("视频宽高:{}*{}", video_width, video_height)
  222. else:
  223. video_width = ""
  224. video_height = ""
  225. Common.logger().info("当前视频无宽高:{}{}", video_width, video_height)
  226. # 发布时间
  227. if "t" in feeds[i]:
  228. video_send_time = feeds[i]["t"]
  229. Common.logger().info(
  230. "视频发布时间:{}", time.strftime(
  231. "%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
  232. else:
  233. video_send_time = ""
  234. Common.logger().info("当前视频无发布时间:{}", video_send_time)
  235. # 用户名 / 头像
  236. if "user" in feeds[i]:
  237. user_name = feeds[i]["user"]["nick"].strip().replace("\n", "")\
  238. .replace("/", "").replace("快手", "").replace(" ", "") \
  239. .replace(" ", "").replace("&NBSP", "").replace("\r", "")
  240. head_url = feeds[i]["user"]["hurl"]
  241. Common.logger().info("用户名:{}", user_name)
  242. Common.logger().info("用户头像:{}", head_url)
  243. else:
  244. user_name = ""
  245. head_url = ""
  246. Common.logger().info("当前视频无用户名:{}", user_name)
  247. Common.logger().info("当前视频无用户头像:{}", head_url)
  248. # 视频封面
  249. if "url" in feeds[i]:
  250. cover_url = feeds[i]["url"]
  251. Common.logger().info("视频封面:{}", cover_url)
  252. else:
  253. cover_url = ""
  254. Common.logger().info("当前视频无视频封面:{}", cover_url)
  255. # 视频播放地址
  256. if "v_url" in feeds[i]:
  257. video_url = feeds[i]["v_url"]
  258. Common.logger().info("播放地址:{}", video_url)
  259. else:
  260. video_url = ""
  261. Common.logger().info("当前视频无播放地址:{}", video_url)
  262. # 视频水印:0 无 1 有
  263. if "v_ort" in feeds[i]:
  264. video_logo = feeds[i]["v_ort"]
  265. else:
  266. video_logo = "1"
  267. # 过滤无效视频
  268. if video_title == "" or video_id == "" or video_duration == ""\
  269. or video_send_time == "" or user_name == "" or head_url == ""\
  270. or cover_url == "" or video_url == "":
  271. Common.logger().warning("无效视频")
  272. # 过滤敏感词
  273. elif any(word if word in video_title else False for word in sensitive_words()) is True:
  274. Common.logger().info("视频已中敏感词:{}".format(video_title))
  275. # 过滤水印视频
  276. elif str(video_logo) == "1" and feeds[i]["tpl_id"] != 0 and len(video_id) > 15:
  277. Common.logger().info("视频有水印:{}", video_title)
  278. # 从云文档去重:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=onyBDH
  279. elif video_id in [j for i in Feishu.get_values_batch("onyBDH") for j in i]:
  280. Common.logger().info("该视频已下载:{}", video_title)
  281. # 从 云文档 去重:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=S714lO
  282. elif video_id in [j for i in Feishu.get_values_batch("S714lO") for j in i]:
  283. Common.logger().info("该视频已在feeds中:{}", video_title)
  284. else:
  285. Common.logger().info("该视频未下载,添加至feeds中:{}".format(video_title))
  286. # feeds工作表,插入首行
  287. Feishu.insert_columns("S714lO", "ROWS", 1, 2)
  288. # 获取当前时间
  289. get_feeds_time = int(time.time())
  290. # 看一看云文档,工作表 kanyikan_feeds 中写入数据
  291. time.sleep(1)
  292. Feishu.update_values("S714lO",
  293. a1=str(get_feeds_time),
  294. b1=str(video_id),
  295. c1=str(video_play_cnt),
  296. d1=str(video_title),
  297. e1=str(video_duration),
  298. f1=str(video_comment_cnt),
  299. g1=str(video_like_cnt),
  300. h1=str(video_share_cnt),
  301. i1=str(video_width)+"*"+str(video_height),
  302. j1=str(video_send_time),
  303. k1=str(user_name),
  304. l1=str(head_url),
  305. m1=str(cover_url),
  306. n1=str(video_url),
  307. o1=str("132645b6e2b996aaad1713a557456816"))
  308. except Exception as e:
  309. Common.logger().error("获取视频列表异常:{}", e)
  310. if __name__ == "__main__":
  311. get_feeds()