Skip to content

Commit cb6b728

Browse files
committed
refactor: improve load_yupp_accounts and get_cookies functions for better handling of tokens and cookies
1 parent 4aa01b5 commit cb6b728

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

g4f/Provider/Yupp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def create_scraper():
8383
return scraper
8484

8585

86-
def load_yupp_accounts(tokens_str: str):
86+
def load_yupp_accounts(tokens: str):
8787
global YUPP_ACCOUNTS, _accounts_loaded
8888
if _accounts_loaded:
8989
return
90-
if not tokens_str:
90+
if not tokens:
9191
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()]
9393
YUPP_ACCOUNTS = [
9494
{"token": token, "is_valid": True, "error_count": 0, "last_used": 0.0}
9595
for token in tokens
@@ -541,7 +541,7 @@ async def create_async_generator(
541541
if not api_key:
542542
api_key = AuthManager.load_api_key(cls)
543543
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")]
545545
if api_key:
546546
load_yupp_accounts(api_key)
547547
log_debug(f"Yupp provider initialized with {len(YUPP_ACCOUNTS)} accounts")

g4f/cookies.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ def get_headers(domain_name: str) -> Dict[str, str]:
9494

9595

9696
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]:
9898
"""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:
100100
return CookiesConfig.cookies[domain_name]
101101

102102
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, {})
105107
return cookies
106108

107109

@@ -115,23 +117,26 @@ def set_cookies(domain_name: str, cookies: Cookies = None) -> None:
115117

116118
def load_cookies_from_browsers(domain_name: str,
117119
raise_requirements_error: bool = True,
118-
single_browser: bool = False) -> Cookies:
120+
single_browser: Optional[str] = None) -> Cookies:
119121
"""Helper to load cookies from all supported browsers."""
120122
if not has_browser_cookie3:
121123
if raise_requirements_error:
122124
raise MissingRequirementsError('Install "browser_cookie3" package')
123125
return {}
124126

125127
cookies = {}
128+
all_cookies = {}
126129
for cookie_fn in BROWSERS:
130+
all_cookies[cookie_fn.__name__] = {}
127131
try:
128132
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}")
131133
for cookie in cookie_jar:
132134
if cookie.name not in cookies and (not cookie.expires or cookie.expires > time.time()):
133135
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:
135140
break
136141
except BrowserCookieError:
137142
pass
@@ -140,6 +145,8 @@ def load_cookies_from_browsers(domain_name: str,
140145
break
141146
except Exception as e:
142147
debug.error(f"Error reading cookies from {cookie_fn.__name__} for {domain_name}: {e}")
148+
if single_browser == "all":
149+
return all_cookies
143150
return cookies
144151

145152

0 commit comments

Comments
 (0)