diff --git a/gemini/client.py b/gemini/client.py index 43efa15..f169201 100644 --- a/gemini/client.py +++ b/gemini/client.py @@ -223,7 +223,7 @@ def _construct_payload( image and [ prompt, - int(os.getenv("GEMINI_ULTRA" or 0)), + int(os.getenv("GEMINI_ULTRA", "0")), None, [[[upload_image(image), 1]]], ] diff --git a/gemini/src/model/image.py b/gemini/src/model/image.py index e0162fd..bc55d77 100644 --- a/gemini/src/model/image.py +++ b/gemini/src/model/image.py @@ -50,7 +50,7 @@ def validate_images(cls, images): "Input is empty. Please provide images infomation to proceed." ) - # Async downloader + # Async @classmethod async def save( cls, @@ -73,9 +73,33 @@ async def save( image_data = await cls.fetch_images_dict(images, cookies) await cls.save_images(image_data, save_path) + # Sync + @staticmethod + def save_sync( + images: List["GeminiImage"], + save_path: str = "cached", + cookies: Optional[dict] = None, + ) -> Optional[Path]: + """Synchronously saves the image to the specified path. + + Args: + path (str): The directory where the image will be saved. + filename (str, optional): The filename for the saved image. If not provided, + a filename is generated based on the image title. + cookies (dict, optional): Cookies to be used for downloading the image. + + Returns: + Optional[Path]: The path where the image is saved, or None if saving fails. + """ + image_data = GeminiImage.fetch_images_dict_sync(images, cookies) + GeminiImage.validate_images(image_data) + GeminiImage.save_images_sync(image_data, save_path) + + ##### Main functions are above. The code below handles special cases for transmitting byte images. + @staticmethod async def fetch_bytes( - url: HttpUrl, cookies: Optional[dict] = None + url: HttpUrl, cookies: Optional[dict] = None, proxies: Optional[dict] = None ) -> Optional[bytes]: """ Fetches bytes of an image asynchronously. @@ -88,8 +112,8 @@ async def fetch_bytes( Optional[bytes]: The bytes of the image, or None if fetching fails. """ try: - async with httpx.AsyncClient(follow_redirects=True) as client: - response = await client.get(str(url), cookies=cookies) + async with httpx.AsyncClient(follow_redirects=True, cookies=cookies, proxies=proxies) as client: + response = await client.get(str(url)) response.raise_for_status() return response.content except Exception as e: @@ -136,28 +160,6 @@ async def save_images(image_data: Dict[str, bytes], save_path: str = "cached"): except Exception as e: print(f"Error saving {title}: {str(e)}") - # Sync downloader - @staticmethod - def save_sync( - images: List["GeminiImage"], - cookies: Optional[dict] = None, - save_path: str = "cached", - ) -> Optional[Path]: - """Synchronously saves the image to the specified path. - - Args: - path (str): The directory where the image will be saved. - filename (str, optional): The filename for the saved image. If not provided, - a filename is generated based on the image title. - cookies (dict, optional): Cookies to be used for downloading the image. - - Returns: - Optional[Path]: The path where the image is saved, or None if saving fails. - """ - image_data = GeminiImage.fetch_images_dict_sync(images, cookies) - GeminiImage.validate_images(image_data) - GeminiImage.save_images_sync(image_data, save_path) - @staticmethod def fetch_bytes_sync( url: HttpUrl, cookies: Optional[dict] = None diff --git a/gemini/src/model/parser/response_parser.py b/gemini/src/model/parser/response_parser.py index 0a8335d..8010887 100644 --- a/gemini/src/model/parser/response_parser.py +++ b/gemini/src/model/parser/response_parser.py @@ -91,7 +91,7 @@ def _extract_body(self, response_text: str) -> Dict: continue raise ValueError( - "All parsing strategies failed. Try to use `Gemini.send_request(prompt)` to get original payload." + "Google PeerSide authentication may have expired. Refresh the cookie manually and retry the test.\nDetails: All parsing strategies failed. Try to use `Gemini.send_request(prompt)` to get original payload" ) def __extract_strategy_1(self, response_text: str) -> Dict: