cookies.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from __future__ import annotations
  2. import os
  3. import time
  4. import json
  5. try:
  6. from platformdirs import user_config_dir
  7. has_platformdirs = True
  8. except ImportError:
  9. has_platformdirs = False
  10. try:
  11. from browser_cookie3 import (
  12. chrome, chromium, opera, opera_gx,
  13. brave, edge, vivaldi, firefox,
  14. _LinuxPasswordManager, BrowserCookieError
  15. )
  16. def _g4f(domain_name: str) -> list:
  17. """
  18. Load cookies from the 'g4f' browser (if exists).
  19. Args:
  20. domain_name (str): The domain for which to load cookies.
  21. Returns:
  22. list: List of cookies.
  23. """
  24. if not has_platformdirs:
  25. return []
  26. user_data_dir = user_config_dir("g4f")
  27. cookie_file = os.path.join(user_data_dir, "Default", "Cookies")
  28. return [] if not os.path.exists(cookie_file) else chrome(cookie_file, domain_name)
  29. browsers = [
  30. _g4f,
  31. chrome, chromium, opera, opera_gx,
  32. brave, edge, vivaldi, firefox,
  33. ]
  34. has_browser_cookie3 = True
  35. except ImportError:
  36. has_browser_cookie3 = False
  37. browsers = []
  38. from .typing import Dict, Cookies
  39. from .errors import MissingRequirementsError
  40. from . import debug
  41. class CookiesConfig():
  42. cookies: Dict[str, Cookies] = {}
  43. cookies_dir: str = "./har_and_cookies"
  44. DOMAINS = [
  45. ".bing.com",
  46. ".meta.ai",
  47. ".google.com",
  48. "www.whiterabbitneo.com",
  49. "huggingface.co",
  50. "chat.reka.ai",
  51. "chatgpt.com"
  52. ]
  53. if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":
  54. _LinuxPasswordManager.get_password = lambda a, b: b"secret"
  55. def get_cookies(domain_name: str = '', raise_requirements_error: bool = True, single_browser: bool = False) -> Dict[str, str]:
  56. """
  57. Load cookies for a given domain from all supported browsers and cache the results.
  58. Args:
  59. domain_name (str): The domain for which to load cookies.
  60. Returns:
  61. Dict[str, str]: A dictionary of cookie names and values.
  62. """
  63. if domain_name in CookiesConfig.cookies:
  64. return CookiesConfig.cookies[domain_name]
  65. cookies = load_cookies_from_browsers(domain_name, raise_requirements_error, single_browser)
  66. CookiesConfig.cookies[domain_name] = cookies
  67. return cookies
  68. def set_cookies(domain_name: str, cookies: Cookies = None) -> None:
  69. if cookies:
  70. CookiesConfig.cookies[domain_name] = cookies
  71. elif domain_name in CookiesConfig.cookies:
  72. CookiesConfig.cookies.pop(domain_name)
  73. def load_cookies_from_browsers(domain_name: str, raise_requirements_error: bool = True, single_browser: bool = False) -> Cookies:
  74. """
  75. Helper function to load cookies from various browsers.
  76. Args:
  77. domain_name (str): The domain for which to load cookies.
  78. Returns:
  79. Dict[str, str]: A dictionary of cookie names and values.
  80. """
  81. if not has_browser_cookie3:
  82. if raise_requirements_error:
  83. raise MissingRequirementsError('Install "browser_cookie3" package')
  84. return {}
  85. cookies = {}
  86. for cookie_fn in [_g4f, chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]:
  87. try:
  88. cookie_jar = cookie_fn(domain_name=domain_name)
  89. if len(cookie_jar) and debug.logging:
  90. print(f"Read cookies from {cookie_fn.__name__} for {domain_name}")
  91. for cookie in cookie_jar:
  92. if cookie.name not in cookies:
  93. if not cookie.expires or cookie.expires > time.time():
  94. cookies[cookie.name] = cookie.value
  95. if single_browser and len(cookie_jar):
  96. break
  97. except BrowserCookieError:
  98. pass
  99. except Exception as e:
  100. if debug.logging:
  101. print(f"Error reading cookies from {cookie_fn.__name__} for {domain_name}: {e}")
  102. return cookies
  103. def set_cookies_dir(dir: str) -> None:
  104. CookiesConfig.cookies_dir = dir
  105. def get_cookies_dir() -> str:
  106. return CookiesConfig.cookies_dir
  107. def read_cookie_files(dirPath: str = None):
  108. def get_domain(v: dict) -> str:
  109. host = [h["value"] for h in v['request']['headers'] if h["name"].lower() in ("host", ":authority")]
  110. if not host:
  111. return
  112. host = host.pop()
  113. for d in DOMAINS:
  114. if d in host:
  115. return d
  116. harFiles = []
  117. cookieFiles = []
  118. for root, _, files in os.walk(CookiesConfig.cookies_dir if dirPath is None else dirPath):
  119. for file in files:
  120. if file.endswith(".har"):
  121. harFiles.append(os.path.join(root, file))
  122. elif file.endswith(".json"):
  123. cookieFiles.append(os.path.join(root, file))
  124. CookiesConfig.cookies = {}
  125. for path in harFiles:
  126. with open(path, 'rb') as file:
  127. try:
  128. harFile = json.load(file)
  129. except json.JSONDecodeError:
  130. # Error: not a HAR file!
  131. continue
  132. if debug.logging:
  133. print("Read .har file:", path)
  134. new_cookies = {}
  135. for v in harFile['log']['entries']:
  136. domain = get_domain(v)
  137. if domain is None:
  138. continue
  139. v_cookies = {}
  140. for c in v['request']['cookies']:
  141. v_cookies[c['name']] = c['value']
  142. if len(v_cookies) > 0:
  143. CookiesConfig.cookies[domain] = v_cookies
  144. new_cookies[domain] = len(v_cookies)
  145. if debug.logging:
  146. for domain, new_values in new_cookies.items():
  147. print(f"Cookies added: {new_values} from {domain}")
  148. for path in cookieFiles:
  149. with open(path, 'rb') as file:
  150. try:
  151. cookieFile = json.load(file)
  152. except json.JSONDecodeError:
  153. # Error: not a json file!
  154. continue
  155. if not isinstance(cookieFile, list):
  156. continue
  157. if debug.logging:
  158. print("Read cookie file:", path)
  159. new_cookies = {}
  160. for c in cookieFile:
  161. if isinstance(c, dict) and "domain" in c:
  162. if c["domain"] not in new_cookies:
  163. new_cookies[c["domain"]] = {}
  164. new_cookies[c["domain"]][c["name"]] = c["value"]
  165. for domain, new_values in new_cookies.items():
  166. if debug.logging:
  167. print(f"Cookies added: {len(new_values)} from {domain}")
  168. CookiesConfig.cookies[domain] = new_values
  169. def _g4f(domain_name: str) -> list:
  170. """
  171. Load cookies from the 'g4f' browser (if exists).
  172. Args:
  173. domain_name (str): The domain for which to load cookies.
  174. Returns:
  175. list: List of cookies.
  176. """
  177. if not has_platformdirs:
  178. return []
  179. user_data_dir = user_config_dir("g4f")
  180. cookie_file = os.path.join(user_data_dir, "Default", "Cookies")
  181. return [] if not os.path.exists(cookie_file) else chrome(cookie_file, domain_name)