demo.py 14 KB

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