client.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import logging
  4. import os
  5. import re
  6. import time
  7. import urllib
  8. import sys
  9. import random
  10. import requests
  11. from py_mini_racer import MiniRacer
  12. def get_bogus(url, user_agent):
  13. query = urllib.parse.urlparse(url).query
  14. js_path = os.path.dirname(os.path.abspath(__file__))
  15. '''
  16. x_bogus_js_path = os.path.join(js_path, 'x_bogus.js')
  17. with open(x_bogus_js_path, 'r', encoding='utf-8') as f:
  18. x_bogus_js_code = f.read()
  19. x_bogus_ctx = MiniRacer()
  20. x_bogus_ctx.eval(x_bogus_js_code)
  21. xbogus = x_bogus_ctx.call('sign', query, user_agent)
  22. url = url + "&X-Bogus=" + xbogus
  23. '''
  24. a_bogus_js_path = os.path.join(js_path, 'a_bogus.js')
  25. with open(a_bogus_js_path, 'r', encoding='utf-8') as f:
  26. a_bogus_js_code = f.read()
  27. a_bogus_ctx = MiniRacer()
  28. a_bogus_ctx.eval(a_bogus_js_code)
  29. abogus = a_bogus_ctx.call('generate_a_bogus', query, user_agent)
  30. url = url + "&a_bogus=" + abogus
  31. return url
  32. def get_ttwid_webid(url, user_agent):
  33. while True:
  34. try:
  35. headers = {
  36. "User-Agent": user_agent,
  37. "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",
  38. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"
  39. }
  40. response = requests.request("GET", url, headers=headers, verify=False, timeout=3)
  41. cookies_dict = response.cookies.get_dict()
  42. ttwid_str = cookies_dict.get('ttwid')
  43. render_data_text = \
  44. re.compile('\<script id=\"RENDER_DATA\" type\=\"application\/json\">(.*?)\<\/script\>').findall(
  45. response.text)
  46. if(render_data_text):
  47. render_data_text=render_data_text[0]
  48. render_data_text = requests.utils.unquote(render_data_text)
  49. render_data_json = json.loads(render_data_text, strict=False)
  50. webid = render_data_json.get('app').get('odin').get('user_unique_id')
  51. return ttwid_str, webid
  52. except Exception as e:
  53. logging.error(e)
  54. time.sleep(1)
  55. if __name__ == '__main__':
  56. try:
  57. payload = sys.argv[1]
  58. params = json.loads(payload)
  59. if params['type'] == 'bogus':
  60. result = get_bogus(params['url'], params['user_agent'])
  61. elif params['type'] == 'webid':
  62. result = get_ttwid_webid(params['url'], params['user_agent'])
  63. print(result)
  64. except Exception as e:
  65. print(e)