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

XFEstudio/gpt4free

refactor: improve load_yupp_accounts and get_cookies functions for better handling of tokens and cookies

cb6b728a
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +19 -12
Modified g4f/Provider/Yupp.py +4 -4
@@ -83,13 +83,13 @@ def create_scraper():
83 83 return scraper
84 84
85 85
86 def load_yupp_accounts(tokens_str: str):
86 def load_yupp_accounts(tokens: str):
87 87 global YUPP_ACCOUNTS, _accounts_loaded
88 88 if _accounts_loaded:
89 89 return
90 if not tokens_str:
90 if not tokens:
91 91 return
92 tokens = [token.strip() for token in tokens_str.split(",") if token.strip()]
92 tokens = [token.strip() for token in (tokens.split(",") if isinstance(tokens, str) else tokens) if token.strip()]
93 93 YUPP_ACCOUNTS = [
94 94 {"token": token, "is_valid": True, "error_count": 0, "last_used": 0.0}
95 95 for token in tokens
@@ -541,7 +541,7 @@ class Yupp(AsyncGeneratorProvider, ProviderModelMixin):
541 541 if not api_key:
542 542 api_key = AuthManager.load_api_key(cls)
543 543 if not api_key:
544 api_key = get_cookies("yupp.ai", False).get("__Secure-yupp.session-token")
544 api_key = [cookies.get("__Secure-yupp.session-token") for cookies in get_cookies("yupp.ai", False, "all").values() if cookies.get("__Secure-yupp.session-token")]
545 545 if api_key:
546 546 load_yupp_accounts(api_key)
547 547 log_debug(f"Yupp provider initialized with {len(YUPP_ACCOUNTS)} accounts")
Modified g4f/cookies.py +15 -8
@@ -94,14 +94,16 @@ def get_headers(domain_name: str) -> Dict[str, str]:
94 94
95 95
96 96 def get_cookies(domain_name: str, raise_requirements_error: bool = True,
97 single_browser: bool = False, cache_result: bool = True) -> Dict[str, str]:
97 single_browser: Optional[str] = None, cache_result: bool = True) -> Dict[str, str]:
98 98 """Load cookies for a given domain from all supported browsers."""
99 if domain_name in CookiesConfig.cookies:
99 if single_browser != "all" and domain_name in CookiesConfig.cookies:
100 100 return CookiesConfig.cookies[domain_name]
101 101
102 102 cookies = load_cookies_from_browsers(domain_name, raise_requirements_error, single_browser)
103 if cache_result:
104 CookiesConfig.cookies[domain_name] = cookies
103 if single_browser != "all" and cache_result:
104 if len(cookies) > 0:
105 CookiesConfig.cookies[domain_name] = cookies
106 return CookiesConfig.cookies.get(domain_name, {})
105 107 return cookies
106 108
107 109
@@ -115,7 +117,7 @@ def set_cookies(domain_name: str, cookies: Cookies = None) -> None:
115 117
116 118 def load_cookies_from_browsers(domain_name: str,
117 119 raise_requirements_error: bool = True,
118 single_browser: bool = False) -> Cookies:
120 single_browser: Optional[str] = None) -> Cookies:
119 121 """Helper to load cookies from all supported browsers."""
120 122 if not has_browser_cookie3:
121 123 if raise_requirements_error:
@@ -123,15 +125,18 @@ def load_cookies_from_browsers(domain_name: str,
123 125 return {}
124 126
125 127 cookies = {}
128 all_cookies = {}
126 129 for cookie_fn in BROWSERS:
130 all_cookies[cookie_fn.__name__] = {}
127 131 try:
128 132 cookie_jar = cookie_fn(domain_name=domain_name)
129 if cookie_jar:
130 debug.log(f"Read cookies from {cookie_fn.__name__} for {domain_name}")
131 133 for cookie in cookie_jar:
132 134 if cookie.name not in cookies and (not cookie.expires or cookie.expires > time.time()):
133 135 cookies[cookie.name] = cookie.value
134 if single_browser and cookie_jar:
136 all_cookies[cookie_fn.__name__][cookie.name] = cookie.value
137 if len(all_cookies[cookie_fn.__name__]) > 0:
138 debug.log(f"Total cookies loaded for {domain_name} from {cookie_fn.__name__}: {len(all_cookies[cookie_fn.__name__])}")
139 if single_browser is True and cookie_jar:
135 140 break
136 141 except BrowserCookieError:
137 142 pass
@@ -140,6 +145,8 @@ def load_cookies_from_browsers(domain_name: str,
140 145 break
141 146 except Exception as e:
142 147 debug.error(f"Error reading cookies from {cookie_fn.__name__} for {domain_name}: {e}")
148 if single_browser == "all":
149 return all_cookies
143 150 return cookies
144 151
145 152