demo.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/8/3
  4. import datetime
  5. import json
  6. import os
  7. import time
  8. # import urllib.parse
  9. # from urllib import parse
  10. import requests
  11. import urllib3
  12. from main.feishu_lib import Feishu
  13. class Demo:
  14. # 获取 token,保存至飞书云文档
  15. @classmethod
  16. def get_token(cls):
  17. # charles 抓包文件保存目录
  18. charles_file_dir = "../chlsfiles/"
  19. if int(len(os.listdir(charles_file_dir))) == 0:
  20. print("未找到chlsfile文件,等待60s")
  21. time.sleep(60)
  22. else:
  23. # try:
  24. # 目标文件夹下所有文件
  25. all_file = sorted(os.listdir(charles_file_dir))
  26. # 获取到目标文件
  27. old_file = all_file[-1]
  28. # 分离文件名与扩展名
  29. new_file = os.path.splitext(old_file)
  30. # 重命名文件后缀
  31. os.rename(os.path.join(charles_file_dir, old_file),
  32. os.path.join(charles_file_dir, new_file[0] + ".txt"))
  33. with open(charles_file_dir + new_file[0] + ".txt", encoding='utf-8-sig', errors='ignore') as f:
  34. contents = json.load(f, strict=False)
  35. for content in contents:
  36. if "mp.weixin.qq.com" in content['host']:
  37. if content["path"] == r"/mp/getappmsgext":
  38. # query
  39. query = content["query"]
  40. Feishu.update_values("recommend", "gzh", "VzrN7E", "B9:B9", [[query]])
  41. # body
  42. headers = content["request"]["header"]["headers"]
  43. body = content["request"]["body"]["text"]
  44. # time.sleep(1)
  45. Feishu.update_values("recommend", "gzh", "VzrN7E", "B8:B8", [[body]])
  46. # title / vid
  47. title = content["request"]["body"]["text"].split("title=")[-1].split("&ct=")[0]
  48. vid = content["request"]["body"]["text"].split("vid=")[-1].split("&is_pay_subscribe")[0]
  49. # time.sleep(1)
  50. Feishu.update_values("recommend", "gzh", "VzrN7E", "B1:B1", [[title]])
  51. # time.sleep(1)
  52. Feishu.update_values("recommend", "gzh", "VzrN7E", "B2:B2", [[vid]])
  53. for h in headers:
  54. if h["name"] == "cookie" and "pass_ticket" in h["value"]:
  55. pass_ticket = h["value"].split("pass_ticket=")[-1]
  56. # print(f"pass_ticket:{pass_ticket}")
  57. Feishu.update_values("recommend", "gzh", "VzrN7E", "B5:B5", [[pass_ticket]])
  58. if h["name"] == "referer":
  59. referer = h["value"]
  60. # print(f"__biz:{referer}")
  61. Feishu.update_values("recommend", "gzh", "VzrN7E", "B7:B7", [[referer]])
  62. if h["name"] == "referer":
  63. __biz = h["value"].split("__biz=")[-1].split("&mid=")[0]
  64. # print(f"__biz:{__biz}")
  65. Feishu.update_values("recommend", "gzh", "VzrN7E", "B3:B3", [[__biz]])
  66. if h["name"] == "cookie" and "appmsg_token" in h["value"]:
  67. appmsg_token = h["value"].split("appmsg_token=")[-1]
  68. # print(f"appmsg_token:{appmsg_token}")
  69. Feishu.update_values("recommend", "gzh", "VzrN7E", "B4:B4", [[appmsg_token]])
  70. if h["name"] == "cookie" and "wap_sid2" in h["value"]:
  71. wap_sid2 = h["value"].split("wap_sid2=")[-1]
  72. # print(f"wap_sid2:{wap_sid2}")
  73. Feishu.update_values("recommend", "gzh", "VzrN7E", "B6:B6", [[wap_sid2]])
  74. # 获取视频下载链接
  75. @classmethod
  76. def get_url(cls, url):
  77. try:
  78. payload = {}
  79. headers = {
  80. 'Cookie': 'rewardsn=; wxtokenkey=777'
  81. }
  82. urllib3.disable_warnings()
  83. response = requests.get(url=url, headers=headers, data=payload, verify=False)
  84. response_list = response.text.splitlines()
  85. video_url_list = []
  86. for m in response_list:
  87. if "mpvideo.qpic.cn" in m:
  88. video_url = m.split("url: '")[1].split("',")[0].replace(r"\x26amp;", "&")
  89. video_url_list.append(video_url)
  90. video_url = video_url_list[0]
  91. return video_url
  92. except Exception as e:
  93. print(f"get_url异常:{e}")
  94. @classmethod
  95. def recommend(cls):
  96. token_sheet = Feishu.get_values_batch("recommend", "gzh", "VzrN7E")
  97. title = token_sheet[0][1]
  98. vid = token_sheet[1][1]
  99. __biz = token_sheet[2][1]
  100. appmsg_token = token_sheet[3][1]
  101. pass_ticket = token_sheet[4][1]
  102. wap_sid2 = token_sheet[5][1]
  103. referer = token_sheet[6][1]
  104. body = token_sheet[7][1]
  105. query = token_sheet[8][1]
  106. # title = "%25E8%25BF%2599%25E4%25B8%25AA%25E5%25BC%259F%25E6%2591%258A%25E4%25B8%258A%25E8%25BF%2599%25E4%25B8%25AA%25E5%2593%25A5%25E4%25B8%258D%25E7%259F%25A5%25E9%2581%2593%25E6%2598%25AF%25E5%25B9%25B8%25E8%25BF%2590%25E8%25BF%2598%25E6%2598%25AF%25E5%2599%25A9%25E6%25A2%25A6%25E7%259A%2584%25E5%25BC%2580%25E5%25A7%258B"
  107. # vid = "wxv_2512850656278560768"
  108. # __biz = "MzkwMjM4OTYyMA%3D%3D"
  109. # appmsg_token = "1177_uEP3JZFhlaEpfuX2xZkF7a5mr7y0GIw2Xtm6WmawNEjFdmTFLWIMx6XbC5z-mFgwQ6mWBq8Wh9EvcaVI"
  110. # pass_ticket = "1VEraA4AwklrT95KI+7WK09zHoybkPgSDgYX/Fw3ArRKYh+QnkI1NXGFHvZ0naeD"
  111. # wap_sid2 = "CNSn5r4HEooBeV9ISXFxZ1FWX1JPazhCVXlfWW5UU0dSbXpza0lHZnlKSDVUVF9aUndjeDhvbDctc0tfUGlWd09uOTVwcTVxWGVkenB5Nld2YkRKNFBKVk9jZVpJZTZGU2hXUkpnZlB5OTMwSEJXQVpTNS13NnJXY0hiTjJibkJ6U3Z3WllaRkNmcEwzWVNBQUF+MOzurZcGOA1AAQ=="
  112. # print(f"title:{title}")
  113. # print(f"vid:{vid}")
  114. # print(f"__biz:{__biz}")
  115. # print(f"appmsg_token:{appmsg_token}")
  116. # print(f"pass_ticket:{pass_ticket}")
  117. # print(f"wap_sid2:{wap_sid2}\n")
  118. url = "https://mp.weixin.qq.com/mp/getappmsgext?"
  119. headers = {
  120. # "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  121. "content-type": 'text/plain',
  122. "accept": "*/*",
  123. "x-requested-with": "XMLHttpRequest",
  124. "accept-language": "zh-cn",
  125. "accept-encoding": "gzip, deflate, br",
  126. "origin": "https://mp.weixin.qq.com",
  127. "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 "
  128. "(KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.26(0x18001a29)"
  129. " NetType/WIFI Language/zh_CN",
  130. "referer": referer,
  131. }
  132. # query_string = {
  133. # "f": "json",
  134. # "mock": "",
  135. # "uin": "777",
  136. # "key": "777",
  137. # "pass_ticket": pass_ticket,
  138. # "wxtoken": "",
  139. # "devicetype": "iOS14.7.1",
  140. # "clientversion": "18001a29",
  141. # "__biz": __biz,
  142. # "appmsg_token": appmsg_token,
  143. # "x5": "0",
  144. # # "f": "json"
  145. # }
  146. cookies = {
  147. "appmsg_token": appmsg_token,
  148. "devicetype": "iOS14.7.1",
  149. "lang": "zh_CN",
  150. "pass_ticket": pass_ticket,
  151. "rewardsn": "",
  152. "version": "18001a29",
  153. "wap_sid2": wap_sid2,
  154. "wxtokenkey": "777",
  155. "wxuin": "2010747860"
  156. }
  157. # form = {
  158. # "r": "0.7399955678811934",
  159. # "__biz": __biz,
  160. # "appmsg_type": "9",
  161. # "mid": "2247483669",
  162. # "sn": "b290af5fe2d28a02bf93721a10ce5106",
  163. # "idx": "1",
  164. # "scene": "136",
  165. # "title": title,
  166. # "ct": "1659387788",
  167. # "abtest_cookie": "AAACAA==",
  168. # "devicetype": "iOS14.7.1",
  169. # "version": "18001a29",
  170. # "is_need_ticket": "0",
  171. # "is_need_ad": "1",
  172. # "comment_id": "0",
  173. # "is_need_reward": "0",
  174. # "both_ad": "0",
  175. # "reward_uin_count": "0",
  176. # "send_time": "",
  177. # "msg_daily_idx": "1",
  178. # "is_original": "0",
  179. # "is_only_read": "1",
  180. # "req_id": "0415gcfdToRrDu1wXSkK8VcK",
  181. # "pass_ticket": pass_ticket,
  182. # "is_temp_url": "0",
  183. # "item_show_type": "5",
  184. # "tmp_version": "1",
  185. # "more_read_type": "0",
  186. # "appmsg_like_type": "2",
  187. # "related_video_sn": "",
  188. # "related_video_num": "5",
  189. # "vid": vid,
  190. # "is_pay_subscribe": "0",
  191. # "pay_subscribe_uin_count": "0",
  192. # "has_red_packet_cover": "0",
  193. # "album_id": "1296223588617486300",
  194. # "album_video_num": "5",
  195. # "cur_album_id": "",
  196. # "is_public_related_video": "0",
  197. # "encode_info_by_base64": "0",
  198. # "exptype": ""
  199. # }
  200. response = requests.post(url=url, headers=headers, cookies=cookies, params=query, data=body)
  201. if "related_tag_video" not in response.json():
  202. print(f"response:{response.text}\n")
  203. elif len(response.json()["related_tag_video"]) == 0:
  204. print(f"response:{response.text}\n")
  205. else:
  206. feeds = response.json()["related_tag_video"]
  207. for m in range(len(feeds)):
  208. # video_title
  209. if "title" not in feeds[m]:
  210. video_title = 0
  211. else:
  212. video_title = feeds[m]["title"]
  213. # video_title = base64.b64decode(video_title).decode("utf-8")
  214. # video_id
  215. if "vid" not in feeds[m]:
  216. video_id = 0
  217. else:
  218. video_id = feeds[m]["vid"]
  219. # play_cnt
  220. if "read_num" not in feeds[m]:
  221. play_cnt = 0
  222. else:
  223. play_cnt = feeds[m]["read_num"]
  224. # like_cnt
  225. if "like_num" not in feeds[m]:
  226. like_cnt = 0
  227. else:
  228. like_cnt = feeds[m]["like_num"]
  229. # duration
  230. if "duration" not in feeds[m]:
  231. duration = 0
  232. else:
  233. duration = feeds[m]["duration"]
  234. # video_width / video_height
  235. if "videoWidth" not in feeds[m] or "videoHeight" not in feeds[m]:
  236. video_width = 0
  237. video_height = 0
  238. else:
  239. video_width = feeds[m]["videoWidth"]
  240. video_height = feeds[m]["videoHeight"]
  241. # send_time
  242. if "pubTime" not in feeds[m]:
  243. send_time = 0
  244. else:
  245. send_time = feeds[m]["pubTime"]
  246. # user_name
  247. if "srcDisplayName" not in feeds[m]:
  248. user_name = 0
  249. else:
  250. user_name = feeds[m]["srcDisplayName"]
  251. # user_name = base64.b64decode(user_name).decode("utf-8")
  252. # user_id
  253. if "srcUserName" not in feeds[m]:
  254. user_id = 0
  255. else:
  256. user_id = feeds[m]["srcUserName"]
  257. # head_url
  258. if "head_img_url" not in feeds[m]:
  259. head_url = 0
  260. else:
  261. head_url = feeds[m]["head_img_url"]
  262. # cover_url
  263. if "cover" not in feeds[m]:
  264. cover_url = 0
  265. else:
  266. cover_url = feeds[m]["cover"]
  267. # video_url
  268. if "url" not in feeds[m]:
  269. video_url = 0
  270. else:
  271. video_url = feeds[m]["url"]
  272. # 下载链接
  273. download_url = cls.get_url(video_url)
  274. print(f"video_title:{video_title}")
  275. print(f"download_url:{download_url}")
  276. # print(f"video_id:{video_id}")
  277. # print(f"play_cnt:{play_cnt}")
  278. # print(f"like_cnt:{like_cnt}")
  279. # print(f"duration:{duration}")
  280. # print(f"video_width:{video_width}")
  281. # print(f"video_height:{video_height}")
  282. # print(f"send_time:{send_time}")
  283. # print(f"user_name:{user_name}")
  284. # print(f"user_id:{user_id}")
  285. # print(f"head_url:{head_url}")
  286. # print(f"cover_url:{cover_url}")
  287. # print(f"video_url:{video_url}")
  288. print("\n")
  289. @classmethod
  290. def get_sheet(cls):
  291. sheet = Feishu.get_values_batch("recommend", "gzh", "VzrN7E")
  292. print(sheet)
  293. print(sheet[0][1])
  294. print(sheet[1][1])
  295. print(sheet[2][1])
  296. print(sheet[3][1])
  297. print(sheet[4][1])
  298. print(sheet[5][1])
  299. @classmethod
  300. def demo1(cls):
  301. t = 3600 * (24 - datetime.datetime.now().hour)
  302. print(24 - datetime.datetime.now().hour)
  303. print(t)
  304. print(3600*(24-datetime.datetime.now().hour))
  305. @classmethod
  306. def demo2(cls):
  307. for i in range(3):
  308. if '我们' in '我们今天吃什么?':
  309. print('no')
  310. print('休眠 2s\n')
  311. time.sleep(2)
  312. else:
  313. print('yes')
  314. print('休眠 2s\n')
  315. time.sleep(2)
  316. if __name__ == "__main__":
  317. # Demo.get_token()
  318. # Demo.recommend()
  319. # Demo.get_sheet()
  320. Demo.demo2()