get_token.py 2.4 KB

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