get_token.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import browser_cookie3 as bc
  2. import json
  3. import sys
  4. import os
  5. import glob
  6. from requests.utils import dict_from_cookiejar
  7. from base64 import b64encode
  8. from typing import Dict, List, AnyStr
  9. def tostr(obj: Dict) -> str:
  10. return b64encode(json.dumps(obj).encode()).decode()
  11. def generate_path() -> List[AnyStr]:
  12. if sys.platform == 'darwin':
  13. return glob.glob(os.path.expanduser('~/Library/Application Support/Google/Chrome/Profile 1/Cookies'))
  14. elif sys.platform.startswith('linux'):
  15. return glob.glob(os.path.expanduser('~/.config/google-chrome/Profile 1/Cookies')) \
  16. or glob.glob(os.path.expanduser('~/.config/chromium/Profile 1/Cookies')) \
  17. or glob.glob(os.path.expanduser('~/.config/google-chrome-beta/Profile 1/Cookies'))
  18. elif sys.platform == 'win32':
  19. win_group_policy_path = glob.glob(os.path.join(os.path.split(os.path.split(bc.windows_group_policy_path())[0])[0], 'Profile 1', 'Cookies'))
  20. return win_group_policy_path \
  21. or glob.glob(os.path.join(os.getenv('APPDATA', ''), '..\Local\\Google\\Chrome\\User Data\\Profile 1\\Cookies')) \
  22. or glob.glob(os.path.join(os.getenv('LOCALAPPDATA', ''), 'Google\\Chrome\\User Data\\Profile 1\\Cookies')) \
  23. or glob.glob(os.path.join(os.getenv('APPDATA', ''), 'Google\\Chrome\\User Data\\Profile 1\\Cookies'))
  24. else:
  25. raise NotImplementedError
  26. def get_cookies() -> Dict:
  27. path = generate_path()
  28. _163_cookies = dict_from_cookiejar(bc.chrome(cookie_file=path, domain_name='.163.com'))
  29. _bnet_cookies = dict_from_cookiejar(bc.chrome(cookie_file=path, domain_name='.battlenet.com.cn'))
  30. keys = {
  31. '_ntes_nuid',
  32. 'MTK_BBID',
  33. 'opt',
  34. 'web.id',
  35. 'BA-tassadar-login.key',
  36. 'login.key',
  37. 'BA-tassadar',
  38. 'bnet.extra',
  39. }
  40. return {k: v for k, v in {**_163_cookies, **_bnet_cookies}.items() if k in keys}
  41. if __name__ == '__main__':
  42. cookies = get_cookies()
  43. if not cookies:
  44. print('Cookie not found! Please Sign in to Battle.net with Chrome')
  45. else:
  46. print('Congrats! Here\'s your token: \n')
  47. print(tostr(cookies))