YunSuoAutoJump.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2023-09-12
  4. ---------
  5. @summary: cookies -> security_session_mid_verify
  6. ---------
  7. @author: Lzz
  8. """
  9. import time
  10. import requests
  11. import execjs
  12. def yun_suo_auto_jump_by_js(url):
  13. ctx = execjs.compile('''
  14. function YunSuoAutoJump(url) {
  15. function stringToHex(str) {
  16. var val = "";
  17. for (var i = 0; i < str.length; i++) {
  18. if (val == "") val = str.charCodeAt(i).toString(16); else val += str.charCodeAt(i).toString(16);
  19. }
  20. return val;
  21. }
  22. var width = 1536;
  23. var height = 864;
  24. var screendate = width + "," + height;
  25. location = url + "?security_verify_data=" + stringToHex(screendate);
  26. return location
  27. }
  28. ''')
  29. return ctx.call("YunSuoAutoJump", url)
  30. def string_to_hex(s):
  31. # 将字符串中的每个字符转换为十六进制,不添加0x前缀
  32. return ''.join([format(ord(char), 'x') for char in s])
  33. def yun_suo_auto_jump_by_py(url):
  34. width = 1536
  35. height = 864
  36. screen_data = f"{width},{height}"
  37. # 生成包含安全验证数据的URL
  38. return f"{url}?security_verify_data={string_to_hex(screen_data)}"
  39. def yun_suo_auto_jump(url, platform='py'):
  40. if platform == 'js':
  41. return yun_suo_auto_jump_by_js(url)
  42. else:
  43. return yun_suo_auto_jump_by_py(url)
  44. def get_mid_code(url, proxies=None, platform=None):
  45. headers = {
  46. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
  47. "Accept-Language": "zh-CN,zh;q=0.9",
  48. "Cache-Control": "no-cache",
  49. "Connection": "keep-alive",
  50. "Pragma": "no-cache",
  51. "Upgrade-Insecure-Requests": "1",
  52. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
  53. }
  54. with requests.session() as session:
  55. session.proxies = proxies
  56. request_params = dict(headers=headers, timeout=60, verify=False)
  57. session.get(url, **request_params)
  58. yz_url = yun_suo_auto_jump(url, platform=platform)
  59. count, cookies = 0, {}
  60. while count < 10:
  61. session.get(yz_url, headers=headers, timeout=60, verify=False)
  62. cookies = session.cookies.get_dict()
  63. if cookies.get('security_session_mid_verify'):
  64. break
  65. count += 1
  66. time.sleep(2)
  67. return cookies