XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Fix "domain" KeyError with Firefox .har

6a05a238
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +22 -9
Modified g4f/cookies.py +22 -9
@@ -26,6 +26,12 @@ from . import debug
26 26 # Global variable to store cookies
27 27 _cookies: Dict[str, Cookies] = {}
28 28
29 DOMAINS = [
30 ".bing.com",
31 ".meta.ai",
32 ".google.com"
33 ]
34
29 35 if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":
30 36 _LinuxPasswordManager.get_password = lambda a, b: b"secret"
31 37
@@ -88,6 +94,15 @@ def load_cookies_from_browsers(domain_name: str, raise_requirements_error: bool
88 94 return cookies
89 95
90 96 def read_cookie_files(dirPath: str = "./har_and_cookies"):
97 def get_domain(v: dict) -> str:
98 host = [h["value"] for h in v['request']['headers'] if h["name"].lower() in ("host", ":authority")]
99 if not host:
100 return
101 host = host.pop()
102 for d in DOMAINS:
103 if d in host:
104 return d
105
91 106 global _cookies
92 107 harFiles = []
93 108 cookieFiles = []
@@ -109,17 +124,15 @@ def read_cookie_files(dirPath: str = "./har_and_cookies"):
109 124 print("Read .har file:", path)
110 125 new_cookies = {}
111 126 for v in harFile['log']['entries']:
127 domain = get_domain(v)
128 if domain is None:
129 continue
112 130 v_cookies = {}
113 131 for c in v['request']['cookies']:
114 if "domain" not in c:
115 continue
116
117 if c['domain'] not in v_cookies:
118 v_cookies[c['domain']] = {}
119 v_cookies[c['domain']][c['name']] = c['value']
120 for domain, c in v_cookies.items():
121 _cookies[domain] = c
122 new_cookies[domain] = len(c)
132 v_cookies[c['name']] = c['value']
133 if len(v_cookies) > 0:
134 _cookies[domain] = v_cookies
135 new_cookies[domain] = len(v_cookies)
123 136 if debug.logging:
124 137 for domain, new_values in new_cookies.items():
125 138 print(f"Cookies added: {new_values} from {domain}")