diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-02-21 17:02:54 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-02-21 17:02:54 +0100 |
commit | 0a0698c7f3fa117e95eaf9b017e4122d15ef4566 (patch) | |
tree | 8d2997750aef7bf9ae0f9ec63410279119fffb69 /g4f/Provider | |
parent | Merge pull request #1603 from xtekky/index (diff) | |
download | gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.tar gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.tar.gz gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.tar.bz2 gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.tar.lz gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.tar.xz gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.tar.zst gpt4free-0a0698c7f3fa117e95eaf9b017e4122d15ef4566.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/needs_auth/Gemini.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/g4f/Provider/needs_auth/Gemini.py b/g4f/Provider/needs_auth/Gemini.py index 0650942e..bd30d8b2 100644 --- a/g4f/Provider/needs_auth/Gemini.py +++ b/g4f/Provider/needs_auth/Gemini.py @@ -50,7 +50,6 @@ class Gemini(AsyncGeneratorProvider): url = "https://gemini.google.com" needs_auth = True working = True - supports_stream = False @classmethod async def create_async_generator( @@ -64,10 +63,9 @@ class Gemini(AsyncGeneratorProvider): **kwargs ) -> AsyncResult: prompt = format_prompt(messages) - - if not cookies: - cookies = get_cookies(".google.com", False, True) - if "__Secure-1PSID" not in cookies or "__Secure-1PSIDCC" not in cookies: + cookies = cookies if cookies else get_cookies(".google.com", False, True) + snlm0e = await cls.fetch_snlm0e(cookies, proxy) if cookies else None + if not snlm0e: driver = None try: driver = get_browser(proxy=proxy) @@ -90,8 +88,12 @@ class Gemini(AsyncGeneratorProvider): if driver: driver.close() - if "__Secure-1PSID" not in cookies: - raise MissingAuthError('Missing "__Secure-1PSID" cookie') + if not snlm0e: + if "__Secure-1PSID" not in cookies: + raise MissingAuthError('Missing "__Secure-1PSID" cookie') + snlm0e = await cls.fetch_snlm0e(cookies, proxy) + if not snlm0e: + raise RuntimeError("Invalid auth. SNlM0e not found") image_url = await cls.upload_image(to_bytes(image), image_name, proxy) if image else None @@ -99,14 +101,6 @@ class Gemini(AsyncGeneratorProvider): cookies=cookies, headers=REQUEST_HEADERS ) as session: - async with session.get(cls.url, proxy=proxy) as response: - text = await response.text() - match = re.search(r'SNlM0e\":\"(.*?)\"', text) - if match: - snlm0e = match.group(1) - else: - raise RuntimeError("SNlM0e not found") - params = { 'bl': REQUEST_BL_PARAM, '_reqid': random.randint(1111, 9999), @@ -204,4 +198,16 @@ class Gemini(AsyncGeneratorProvider): upload_url, headers=headers, data=image, proxy=proxy ) as response: response.raise_for_status() - return await response.text()
\ No newline at end of file + return await response.text() + + @classmethod + async def fetch_snlm0e(cls, cookies: Cookies, proxy: str = None): + async with ClientSession( + cookies=cookies, + headers=REQUEST_HEADERS + ) as session: + async with session.get(cls.url, proxy=proxy) as response: + text = await response.text() + match = re.search(r'SNlM0e\":\"(.*?)\"', text) + if match: + return match.group(1)
\ No newline at end of file |