common_utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. # @Author : lihuiwen
  3. # @Email : huiwennear@163.com
  4. # @Time : 2024/5/23 16:59
  5. import json
  6. import logging
  7. import os
  8. import re
  9. import time
  10. import urllib
  11. import random
  12. import requests
  13. from py_mini_racer import MiniRacer
  14. class CommonUtils(object):
  15. def __init__(self):
  16. self.user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
  17. js_path = os.path.dirname(os.path.abspath(__file__))
  18. self.x_bogus_js_path = os.path.join(js_path, 'x_bogus.js')
  19. self.a_bogus_js_path = os.path.join(js_path, 'a_bogus.js')
  20. with open(self.x_bogus_js_path, 'r', encoding='utf-8') as f:
  21. x_bogus_js_code = f.read()
  22. self.x_bogus_ctx = MiniRacer()
  23. self.x_bogus_ctx.eval(x_bogus_js_code)
  24. with open(self.a_bogus_js_path, 'r', encoding='utf-8') as f:
  25. a_bogus_js_code = f.read()
  26. self.a_bogus_ctx = MiniRacer()
  27. self.a_bogus_ctx.eval(a_bogus_js_code)
  28. self.ac_sign_js_path = os.path.join(js_path, 'ac_sign.js')
  29. def get_xbogus(self, req_url, user_agent):
  30. """
  31. xbogus加密
  32. :param req_url:
  33. :param user_agent:
  34. :return:
  35. """
  36. query = urllib.parse.urlparse(req_url).query
  37. xbogus = self.x_bogus_ctx.call('sign', query, user_agent)
  38. return xbogus
  39. def get_abogus(self, req_url, user_agent):
  40. """
  41. xbogus加密
  42. :param req_url:
  43. :param user_agent:
  44. :return:
  45. """
  46. query = urllib.parse.urlparse(req_url).query
  47. abogus = self.a_bogus_ctx.call('generate_a_bogus', query, user_agent)
  48. return abogus
  49. def get_ms_token(self, randomlength=107):
  50. """
  51. 根据传入长度产生随机字符串
  52. """
  53. random_str = ''
  54. base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789='
  55. length = len(base_str) - 1
  56. for _ in range(randomlength):
  57. random_str += base_str[random.randint(0, length)]
  58. return random_str
  59. def get_ttwid_webid(self, req_url):
  60. while True:
  61. try:
  62. headers = {
  63. "User-Agent": self.user_agent,
  64. "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",
  65. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"
  66. }
  67. response = requests.request("GET", req_url, headers=headers, verify=False, timeout=3)
  68. cookies_dict = response.cookies.get_dict()
  69. ttwid_str = cookies_dict.get('ttwid')
  70. render_data_text = \
  71. re.compile('\<script id=\"RENDER_DATA\" type\=\"application\/json\">(.*?)\<\/script\>').findall(
  72. response.text)
  73. if(render_data_text):
  74. render_data_text=render_data_text[0]
  75. render_data_text = requests.utils.unquote(render_data_text)
  76. render_data_json = json.loads(render_data_text, strict=False)
  77. webid = render_data_json.get('app').get('odin').get('user_unique_id')
  78. return ttwid_str, webid
  79. except Exception as e:
  80. logging.error(e)
  81. time.sleep(1)