get_feeds.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/4/18
  4. """
  5. 获取看一看+小程序,首页推荐视频列表
  6. """
  7. import json
  8. import os
  9. import random
  10. import sys
  11. import time
  12. import requests
  13. import urllib3
  14. from main.feishu_lib import Feishu
  15. sys.path.append(os.getcwd())
  16. from main.common import Common
  17. proxies = {"http": None, "https": None}
  18. # 敏感词库
  19. def kanyikan_sensitive_words():
  20. # 敏感词库列表
  21. word_list = []
  22. # 从云文档读取所有敏感词,添加到词库列表
  23. lists = Feishu.get_values_batch("rofdM5")
  24. for i in lists:
  25. for j in i:
  26. # 过滤空的单元格内容
  27. if j is None:
  28. pass
  29. else:
  30. word_list.append(j)
  31. return word_list
  32. def get_feeds():
  33. """
  34. 1.从看一看+小程序首页推荐,获取视频列表
  35. 2.先在 https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=20ce0c 中去重
  36. 3.再从 https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM 中去重
  37. 4.添加视频信息至 https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
  38. """
  39. host = "https://search.weixin.qq.com"
  40. url = '/cgi-bin/recwxa/recwxavideolist?'
  41. video_list_session = Common.get_session()
  42. Common.logger().info("获取视频list时,session:{}", video_list_session)
  43. header = {
  44. "Connection": "keep-alive",
  45. "content-type": "application/json",
  46. "Accept-Encoding": "gzip,compress,br,deflate",
  47. "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) "
  48. "AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x18001236) "
  49. "NetType/WIFI Language/zh_CN",
  50. "Referer": "https://servicewechat.com/wxbb9a805eb4f9533c/234/page-frame.html",
  51. }
  52. params = {
  53. 'session': video_list_session,
  54. "offset": 0,
  55. "wxaVersion": "3.9.2",
  56. "count": "10",
  57. "channelid": "208",
  58. "scene": '310',
  59. "subscene": '1089',
  60. "clientVersion": '8.0.18',
  61. "sharesearchid": '0',
  62. "nettype": 'wifi',
  63. "switchprofile": "0",
  64. "switchnewuser": "0",
  65. }
  66. try:
  67. urllib3.disable_warnings()
  68. r = requests.get(host + url, headers=header, params=params, proxies=proxies, verify=False)
  69. response = json.loads(r.content.decode("utf8"))
  70. if "data" not in response:
  71. Common.logger().info("获取视频list时,session过期,随机睡眠 31-50 秒")
  72. # 如果返回空信息,则随机睡眠 31-40 秒
  73. time.sleep(random.randint(31, 40))
  74. get_feeds()
  75. elif "items" not in response["data"]:
  76. Common.logger().info("获取视频list时,返回空信息,随机睡眠 1-3 分钟")
  77. # 如果返回空信息,则随机睡眠 1-3 分钟
  78. time.sleep(random.randint(60, 180))
  79. get_feeds()
  80. else:
  81. items = response["data"]["items"]
  82. for i in range(len(items)):
  83. # 如果该视频没有视频信息,则忽略
  84. if "videoInfo" not in items[i]:
  85. Common.logger().info("无视频信息")
  86. else:
  87. # 获取视频标题
  88. video_title = items[i]["title"].strip().replace("\n", "")\
  89. .replace("/", "").replace("\\", "").replace("\r", "")\
  90. .replace(":", "").replace("*", "").replace("?", "")\
  91. .replace("?", "").replace('"', "").replace("<", "")\
  92. .replace(">", "").replace("|", "").replace(" ", "")\
  93. .replace("&NBSP", "").replace(".", "。").replace(" ", "")
  94. Common.logger().info('视频标题:{}', video_title)
  95. # 获取视频ID
  96. video_id = items[i]["videoId"]
  97. Common.logger().info('视频ID:{}', video_id)
  98. # 获取视频播放次数
  99. video_play_cnt = items[i]["playCount"]
  100. Common.logger().info('视频播放次数:{}', video_play_cnt)
  101. # 获取视频点赞数
  102. video_liked_cnt = items[i]["liked_cnt"]
  103. Common.logger().info('视频点赞数:{}', video_liked_cnt)
  104. # 获取视频评论数
  105. video_comment_cnt = items[i]["comment_cnt"]
  106. Common.logger().info('视频评论数:{}', video_comment_cnt)
  107. # 获取视频分享数
  108. video_shared_cnt = items[i]["shared_cnt"]
  109. Common.logger().info('视频分享数:{}', video_shared_cnt)
  110. # 获取视频时长
  111. video_duration = items[i]["mediaDuration"]
  112. Common.logger().info('视频时长:{}秒', video_duration)
  113. # 获取视频宽高
  114. if "short_video_info" not in items[i]:
  115. video_width = "0"
  116. video_height = "0"
  117. video_resolution = str(video_width) + "*" + str(video_height)
  118. Common.logger().info("无分辨率:{}", video_resolution)
  119. elif len(items[i]["short_video_info"]) == 0:
  120. video_width = "0"
  121. video_height = "0"
  122. video_resolution = str(video_width) + "*" + str(video_height)
  123. Common.logger().info("无分辨率:{}", video_resolution)
  124. else:
  125. # 视频宽
  126. video_width = items[i]["short_video_info"]["width"]
  127. # 视频高
  128. video_height = items[i]["short_video_info"]["height"]
  129. video_resolution = str(video_width) + "*" + str(video_height)
  130. Common.logger().info('视频宽高:{}', video_resolution)
  131. # 获取视频发布时间
  132. video_send_date = items[i]["date"]
  133. Common.logger().info("视频发布时间:{}",
  134. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(video_send_date)))
  135. # 获取视频用户名
  136. video_user = items[i]["source"].strip().replace("\n", "")
  137. Common.logger().info('视频用户名:{}', video_user)
  138. # 获取视频用户头像
  139. video_user_cover = items[i]["bizIcon"]
  140. Common.logger().info('视频用户头像:{}', video_user_cover)
  141. # 获取视频封面
  142. if "smartCoverUrl" in items[i]:
  143. video_cover = items[i]["smartCoverUrl"]
  144. Common.logger().info('视频封面:{}', video_cover)
  145. else:
  146. video_cover = items[i]["thumbUrl"]
  147. Common.logger().info('视频封面:{}', video_cover)
  148. # 获取播放地址
  149. if "mpInfo" in items[i]["videoInfo"]["videoCdnInfo"].keys():
  150. if len(items[i]["videoInfo"]["videoCdnInfo"]["mpInfo"]["urlInfo"]) > 2:
  151. url = items[i]["videoInfo"]["videoCdnInfo"]["mpInfo"]["urlInfo"][2]["url"]
  152. Common.logger().info('视频播放地址:{}', url)
  153. else:
  154. url = items[i]["videoInfo"]["videoCdnInfo"]["mpInfo"]["urlInfo"][0]["url"]
  155. Common.logger().info('视频播放地址:{}', url)
  156. elif "ctnInfo" in items[i]["videoInfo"]["videoCdnInfo"]:
  157. url = items[i]["videoInfo"]["videoCdnInfo"]["ctnInfo"]["urlInfo"][0]["url"]
  158. Common.logger().info('视频播放地址:{}', url)
  159. else:
  160. url = items[i]["videoInfo"]["videoCdnInfo"]["urlInfo"][0]["url"]
  161. Common.logger().info('视频播放地址:{}', url)
  162. # 过滤无效视频
  163. if video_id == "" \
  164. or video_send_date == "" \
  165. or video_title.strip() == "" \
  166. or video_play_cnt == "" \
  167. or video_liked_cnt == "" \
  168. or video_duration == "" \
  169. or video_comment_cnt == "" \
  170. or video_shared_cnt == "" \
  171. or video_user == "" \
  172. or video_user_cover == "" \
  173. or video_cover == "" \
  174. or url == "":
  175. Common.logger().info("无效视频")
  176. # 过滤敏感词
  177. elif any(word if word in video_title else False for word in kanyikan_sensitive_words()) is True:
  178. Common.logger().info("视频已中敏感词:{}".format(video_title))
  179. # 从 云文档 去重:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=20ce0c
  180. elif video_id in [j for i in Feishu.get_values_batch("20ce0c") for j in i]:
  181. Common.logger().info("该视频已下载:{}", video_title)
  182. # 从 云文档 去重:https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?sheet=SdCHOM
  183. elif video_id in [j for i in Feishu.get_values_batch("SdCHOM") for j in i]:
  184. Common.logger().info("该视频已在kanyikan_feeds中:{}", video_title)
  185. else:
  186. Common.logger().info("该视频未下载,添加至kanyikan_feeds:{}", video_title)
  187. # 看一看+工作表,插入首行
  188. Feishu.insert_columns("SdCHOM")
  189. # 获取当前时间
  190. get_feeds_time = int(time.time())
  191. # 看一看云文档,工作表 kanyikan_feeds 中写入数据
  192. Feishu.update_values("SdCHOM",
  193. a1=str(get_feeds_time),
  194. b1=str(video_id),
  195. c1=str(video_play_cnt),
  196. d1=str(video_title),
  197. e1=str(video_duration),
  198. f1=str(video_comment_cnt),
  199. g1=str(video_liked_cnt),
  200. h1=str(video_shared_cnt),
  201. i1=str(video_resolution),
  202. j1=str(video_send_date),
  203. k1=str(video_user),
  204. l1=str(video_user_cover),
  205. m1=str(video_cover),
  206. n1=str(url),
  207. o1=str(video_list_session))
  208. except Exception as e:
  209. Common.logger().error("获取视频 list 时异常:{}", e)
  210. if __name__ == "__main__":
  211. get_feeds()