demo.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/5/16
  4. import datetime
  5. import random
  6. import time
  7. import requests
  8. import urllib3
  9. from main.common import Common
  10. from main.feishu_lib import Feishu
  11. proxies = {"http": None, "https": None}
  12. class Demo:
  13. # 查询视频详情的各项数据
  14. @classmethod
  15. def video_detail_info(cls, user_id, user_mid, video_id):
  16. url = "https://kapi.xiaoniangao.cn/profile/get_profile_by_id"
  17. headers = {
  18. "x-b3-traceid": "bd267349bf41b",
  19. "X-Token-Id": "86f6d7cc2b2b6870004df5d16c82aaf3-1185665701",
  20. "uid": "8fde3c6c-c070-4379-bfc4-15c7e85139c9",
  21. "content-type": "application/json",
  22. "Accept-Encoding": "gzip,compress,br,deflate",
  23. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  24. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  25. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  26. "Referer": "https://servicewechat.com/wxd7911e4c177690e4/617/page-frame.html"
  27. }
  28. data = {
  29. "play_src": "1",
  30. "profile_id": int(user_id),
  31. "profile_mid": int(user_mid),
  32. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/"
  33. "!400x400r/crop/400x400/interlace/1/format/jpg",
  34. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail"
  35. "/!80x80r/crop/80x80/interlace/1/format/jpg",
  36. "share_width": 625,
  37. "share_height": 500,
  38. "no_comments": True,
  39. "no_follow": True,
  40. "vid": video_id,
  41. "hot_l1_comment": True,
  42. "token": "90747742180aeb22c0fe3a3c6a38f3d9",
  43. "uid": "8fde3c6c-c070-4379-bfc4-15c7e85139c9",
  44. "proj": "ma",
  45. "wx_ver": "8.0.20",
  46. "code_ver": "3.62.0",
  47. "log_common_params": {
  48. "e": [{
  49. "data": {
  50. "page": "dynamicSharePage"
  51. }
  52. }],
  53. "ext": {
  54. "brand": "iPhone",
  55. "device": "iPhone 11",
  56. "os": "iOS 14.7.1",
  57. "weixinver": "8.0.20",
  58. "srcver": "2.24.3",
  59. "net": "wifi",
  60. "scene": "1089"
  61. },
  62. "pj": "1",
  63. "pf": "2",
  64. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  65. }
  66. }
  67. try:
  68. urllib3.disable_warnings()
  69. r = requests.post(headers=headers, url=url, json=data, proxies=proxies, verify=False)
  70. print(r.text)
  71. video_title = r.json()["data"]["title"]
  72. video_id = r.json()["data"]["vid"]
  73. video_play_cnt = r.json()["data"]["play_pv"]
  74. video_duration = int(r.json()["data"]["du"]) / 1000
  75. video_url = r.json()["data"]["v_url"]
  76. video_send_time = r.json()["data"]["t"]
  77. print(f"video_title:{video_title}")
  78. print(f"video_id:{video_id}")
  79. print(f"video_play_cnt:{video_play_cnt}")
  80. print(f"video_duration:{video_duration}")
  81. print(f'video_send_time:{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000))}')
  82. print(f"video_url:{video_url}")
  83. except Exception as e:
  84. print(e)
  85. # 微信号
  86. @classmethod
  87. def get_sheet(cls):
  88. hour_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "ba0da4")
  89. # print(hour_sheet)
  90. print(hour_sheet)
  91. @classmethod
  92. def check_date(cls):
  93. date = Feishu.get_values_batch("hour", "xiaoniangao", "FEcuil")
  94. print(f'文档中L1:N1的日期:{date[0][11]}')
  95. today = datetime.datetime.now().strftime("%Y-%m-%d")
  96. print(f"today:{today}")
  97. Common.logger("hour").info("检查今日上升榜日期是否存在")
  98. if date[0][11] != today:
  99. # 插入3列 L1:N1,并写入日期和时间数据
  100. values = [[today], ["10:00", "15:00", "20:00"]]
  101. time.sleep(1)
  102. Feishu.insert_columns("hour", "xiaoniangao", "FEcuil", "COLUMNS", 11, 14)
  103. time.sleep(1)
  104. Feishu.update_values("hour", "xiaoniangao", "FEcuil", "L1:N2", values)
  105. time.sleep(1)
  106. Feishu.merge_cells("hour", "xiaoniangao", "FEcuil", "L1:N1")
  107. Common.logger("hour").info("插入今天日期成功")
  108. else:
  109. Common.logger("hour").info("今日上升榜日期已存在")
  110. # 获取列表
  111. @classmethod
  112. def get_recommend(cls, uid, token):
  113. """
  114. 1.从列表获取视频,7 天内,播放量>=5000
  115. 2.时长 1-10min
  116. 3.每天10:00、15:00、20:00 把符合规则的视频,写入云文档
  117. https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?sheet=ba0da4
  118. """
  119. url = "https://kapi.xiaoniangao.cn/trends/get_recommend_trends"
  120. headers = {
  121. "x-b3-traceid": "1c403a4aa72e3c",
  122. "X-Token-Id": "ab619e96d801f1567388629260aa68ec-1202200806",
  123. "uid": uid,
  124. "content-type": "application/json",
  125. "Accept-Encoding": "gzip,compress,br,deflate",
  126. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  127. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 '
  128. 'MicroMessenger/8.0.20(0x18001432) NetType/WIFI Language/zh_CN',
  129. "Referer": "https://servicewechat.com/wxd7911e4c177690e4/624/page-frame.html"
  130. }
  131. data = {
  132. "log_params": {
  133. "page": "discover_rec",
  134. "common": {
  135. "brand": "iPhone",
  136. "device": "iPhone 11",
  137. "os": "iOS 14.7.1",
  138. "weixinver": "8.0.20",
  139. "srcver": "2.24.2",
  140. "net": "wifi",
  141. "scene": 1089
  142. }
  143. },
  144. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!750x500r/crop/750x500/interlace/1/format/jpg",
  145. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!80x80r/crop/80x80/interlace/1/format/jpg",
  146. "share_width": 625,
  147. "share_height": 500,
  148. "ext": {
  149. "fmid": 0,
  150. "items": {}
  151. },
  152. "app": "xng",
  153. "rec_scene": "discover_rec",
  154. "log_common_params": {
  155. "e": [{
  156. "data": {
  157. "page": "discoverIndexPage",
  158. "topic": "recommend"
  159. },
  160. "ab": {}
  161. }],
  162. "ext": {
  163. "brand": "iPhone",
  164. "device": "iPhone 11",
  165. "os": "iOS 14.7.1",
  166. "weixinver": "8.0.20",
  167. "srcver": "2.24.3",
  168. "net": "wifi",
  169. "scene": "1089"
  170. },
  171. "pj": "1",
  172. "pf": "2",
  173. "session_id": "7bcce313-b57d-4305-8d14-6ebd9a1bad29"
  174. },
  175. "refresh": False,
  176. "token": token,
  177. "uid": uid,
  178. "proj": "ma",
  179. "wx_ver": "8.0.20",
  180. "code_ver": "3.62.0"
  181. }
  182. try:
  183. urllib3.disable_warnings()
  184. r = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
  185. if "data" not in r.json():
  186. Common.logger("demo").warning("获取视频feeds错误:{}", r.text)
  187. elif "list" not in r.json()["data"]:
  188. Common.logger("demo").warning("获取视频feeds无数据,休眠10s:{}", r.text)
  189. else:
  190. # 视频列表数据
  191. feeds = r.json()["data"]["list"]
  192. for i in range(len(feeds)):
  193. # 标题
  194. if "title" in feeds[i]:
  195. video_title = feeds[i]["title"].strip().replace("\n", "") \
  196. .replace("/", "").replace("\r", "").replace("#", "") \
  197. .replace(".", "。").replace("\\", "").replace("&NBSP", "") \
  198. .replace(":", "").replace("*", "").replace("?", "") \
  199. .replace("?", "").replace('"', "").replace("<", "") \
  200. .replace(">", "").replace("|", "").replace(" ", "")
  201. else:
  202. video_title = 0
  203. # 视频 ID
  204. if "vid" in feeds[i]:
  205. video_id = feeds[i]["vid"]
  206. else:
  207. video_id = 0
  208. # 播放量
  209. if "play_pv" in feeds[i]:
  210. video_play_cnt = feeds[i]["play_pv"]
  211. else:
  212. video_play_cnt = 0
  213. # 点赞量
  214. if "favor" in feeds[i]:
  215. video_like_cnt = feeds[i]["favor"]["total"]
  216. else:
  217. video_like_cnt = 0
  218. # 分享量
  219. if "share" in feeds[i]:
  220. video_share_cnt = feeds[i]["share"]
  221. else:
  222. video_share_cnt = 0
  223. # # 评论量
  224. # if "comment_count" in feeds[i]:
  225. # video_comment_cnt = feeds[i]["comment_count"]
  226. # else:
  227. # video_comment_cnt = 0
  228. # 时长
  229. if "du" in feeds[i]:
  230. video_duration = int(feeds[i]["du"] / 1000)
  231. else:
  232. video_duration = 0
  233. # 宽和高
  234. if "w" or "h" in feeds[i]:
  235. video_width = feeds[i]["w"]
  236. video_height = feeds[i]["h"]
  237. else:
  238. video_width = 0
  239. video_height = 0
  240. # 发布时间
  241. if "t" in feeds[i]:
  242. video_send_time = feeds[i]["t"]
  243. else:
  244. video_send_time = 0
  245. # 用户名 / 头像
  246. if "user" in feeds[i]:
  247. user_name = feeds[i]["user"]["nick"].strip().replace("\n", "") \
  248. .replace("/", "").replace("快手", "").replace(" ", "") \
  249. .replace(" ", "").replace("&NBSP", "").replace("\r", "")
  250. head_url = feeds[i]["user"]["hurl"]
  251. else:
  252. user_name = 0
  253. head_url = 0
  254. # 用户 ID
  255. profile_id = feeds[i]["id"]
  256. # 用户 mid
  257. profile_mid = feeds[i]["user"]["mid"]
  258. # 视频封面
  259. if "url" in feeds[i]:
  260. cover_url = feeds[i]["url"]
  261. else:
  262. cover_url = 0
  263. # 视频播放地址
  264. if "v_url" in feeds[i]:
  265. video_url = feeds[i]["v_url"]
  266. else:
  267. video_url = 0
  268. Common.logger("demo").info("标题:{}", video_title)
  269. Common.logger("demo").info("视频ID:{}", video_id)
  270. Common.logger("demo").info("播放量:{}", video_play_cnt)
  271. # Common.logger("demo").info("点赞量:{}", video_like_cnt)
  272. # Common.logger("demo").info("分享量:{}", video_share_cnt)
  273. # Common.logger("demo").info("评论数:{}", video_comment_cnt)
  274. Common.logger("demo").info("时长:{}秒", video_duration)
  275. # Common.logger("demo").info("宽高:{}*{}", video_width, video_height)
  276. Common.logger("demo").info(
  277. "视频发布时间:{}", time.strftime(
  278. "%Y-%m-%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
  279. Common.logger("demo").info("用户名:{}", user_name)
  280. # Common.logger("demo").info("用户头像:{}", head_url)
  281. # Common.logger("demo").info("封面:{}", cover_url)
  282. Common.logger("demo").info("播放地址:{}", video_url)
  283. except Exception as e:
  284. Common.logger("demo").error("get_recommend异常:{}", e)
  285. # 生成 uid、token
  286. @classmethod
  287. def get_uid_token(cls):
  288. uid_token = "abcdefghijklmnopqrstuvwxyz0123456789"
  289. uid = ("".join(random.sample(uid_token, 8))) + "-" + ("".join(random.sample(uid_token, 4))) + "-" + (
  290. "".join(random.sample(uid_token, 4))) + "-" + ("".join(random.sample(uid_token, 4))) + "-" + (
  291. "".join(random.sample(uid_token, 12)))
  292. token = ("".join(random.sample(uid_token, 32)))
  293. Feishu.update_values("hour", "xiaoniangao", "dzcWHw", "B6:B6", [[uid]])
  294. time.sleep(1)
  295. Feishu.update_values("hour", "xiaoniangao", "dzcWHw", "B7:B7", [[token]])
  296. if __name__ == "__main__":
  297. # 已下载表中: P_ID
  298. user_id = '45034645'
  299. # 已下载表中: P_mid
  300. user_mid = '1160417253'
  301. # 已下载表中: 站外视频 ID
  302. video_id = '4532791008'
  303. Demo.video_detail_info(user_id, user_mid, video_id)
  304. pass