webscout 8.2.9__py3-none-any.whl → 2026.1.19__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- webscout/AIauto.py +524 -251
- webscout/AIbase.py +247 -319
- webscout/AIutel.py +68 -703
- webscout/Bard.py +1072 -1026
- webscout/Extra/GitToolkit/__init__.py +10 -10
- webscout/Extra/GitToolkit/gitapi/__init__.py +20 -12
- webscout/Extra/GitToolkit/gitapi/gist.py +142 -0
- webscout/Extra/GitToolkit/gitapi/organization.py +91 -0
- webscout/Extra/GitToolkit/gitapi/repository.py +308 -195
- webscout/Extra/GitToolkit/gitapi/search.py +162 -0
- webscout/Extra/GitToolkit/gitapi/trending.py +236 -0
- webscout/Extra/GitToolkit/gitapi/user.py +128 -96
- webscout/Extra/GitToolkit/gitapi/utils.py +82 -62
- webscout/Extra/YTToolkit/README.md +443 -375
- webscout/Extra/YTToolkit/YTdownloader.py +953 -957
- webscout/Extra/YTToolkit/__init__.py +3 -3
- webscout/Extra/YTToolkit/transcriber.py +595 -476
- webscout/Extra/YTToolkit/ytapi/README.md +230 -44
- webscout/Extra/YTToolkit/ytapi/__init__.py +22 -6
- webscout/Extra/YTToolkit/ytapi/captions.py +190 -0
- webscout/Extra/YTToolkit/ytapi/channel.py +302 -307
- webscout/Extra/YTToolkit/ytapi/errors.py +13 -13
- webscout/Extra/YTToolkit/ytapi/extras.py +178 -118
- webscout/Extra/YTToolkit/ytapi/hashtag.py +120 -0
- webscout/Extra/YTToolkit/ytapi/https.py +89 -88
- webscout/Extra/YTToolkit/ytapi/patterns.py +61 -61
- webscout/Extra/YTToolkit/ytapi/playlist.py +59 -59
- webscout/Extra/YTToolkit/ytapi/pool.py +8 -8
- webscout/Extra/YTToolkit/ytapi/query.py +143 -40
- webscout/Extra/YTToolkit/ytapi/shorts.py +122 -0
- webscout/Extra/YTToolkit/ytapi/stream.py +68 -63
- webscout/Extra/YTToolkit/ytapi/suggestions.py +97 -0
- webscout/Extra/YTToolkit/ytapi/utils.py +66 -62
- webscout/Extra/YTToolkit/ytapi/video.py +403 -232
- webscout/Extra/__init__.py +2 -3
- webscout/Extra/gguf.py +1298 -684
- webscout/Extra/tempmail/README.md +487 -487
- webscout/Extra/tempmail/__init__.py +28 -28
- webscout/Extra/tempmail/async_utils.py +143 -141
- webscout/Extra/tempmail/base.py +172 -161
- webscout/Extra/tempmail/cli.py +191 -187
- webscout/Extra/tempmail/emailnator.py +88 -84
- webscout/Extra/tempmail/mail_tm.py +378 -361
- webscout/Extra/tempmail/temp_mail_io.py +304 -292
- webscout/Extra/weather.py +196 -194
- webscout/Extra/weather_ascii.py +17 -15
- webscout/Provider/AISEARCH/PERPLEXED_search.py +175 -0
- webscout/Provider/AISEARCH/Perplexity.py +292 -333
- webscout/Provider/AISEARCH/README.md +106 -279
- webscout/Provider/AISEARCH/__init__.py +16 -9
- webscout/Provider/AISEARCH/brave_search.py +298 -0
- webscout/Provider/AISEARCH/iask_search.py +357 -410
- webscout/Provider/AISEARCH/monica_search.py +200 -220
- webscout/Provider/AISEARCH/webpilotai_search.py +242 -255
- webscout/Provider/Algion.py +413 -0
- webscout/Provider/Andi.py +74 -69
- webscout/Provider/Apriel.py +313 -0
- webscout/Provider/Ayle.py +323 -0
- webscout/Provider/ChatSandbox.py +329 -342
- webscout/Provider/ClaudeOnline.py +365 -0
- webscout/Provider/Cohere.py +232 -208
- webscout/Provider/DeepAI.py +367 -0
- webscout/Provider/Deepinfra.py +467 -340
- webscout/Provider/EssentialAI.py +217 -0
- webscout/Provider/ExaAI.py +274 -261
- webscout/Provider/Gemini.py +175 -169
- webscout/Provider/GithubChat.py +385 -369
- webscout/Provider/Gradient.py +286 -0
- webscout/Provider/Groq.py +556 -801
- webscout/Provider/HadadXYZ.py +323 -0
- webscout/Provider/HeckAI.py +392 -375
- webscout/Provider/HuggingFace.py +387 -0
- webscout/Provider/IBM.py +340 -0
- webscout/Provider/Jadve.py +317 -291
- webscout/Provider/K2Think.py +306 -0
- webscout/Provider/Koboldai.py +221 -384
- webscout/Provider/Netwrck.py +273 -270
- webscout/Provider/Nvidia.py +310 -0
- webscout/Provider/OPENAI/DeepAI.py +489 -0
- webscout/Provider/OPENAI/K2Think.py +423 -0
- webscout/Provider/OPENAI/PI.py +463 -0
- webscout/Provider/OPENAI/README.md +890 -952
- webscout/Provider/OPENAI/TogetherAI.py +405 -0
- webscout/Provider/OPENAI/TwoAI.py +255 -357
- webscout/Provider/OPENAI/__init__.py +148 -40
- webscout/Provider/OPENAI/ai4chat.py +348 -293
- webscout/Provider/OPENAI/akashgpt.py +436 -0
- webscout/Provider/OPENAI/algion.py +303 -0
- webscout/Provider/OPENAI/{exachat.py → ayle.py} +365 -444
- webscout/Provider/OPENAI/base.py +253 -249
- webscout/Provider/OPENAI/cerebras.py +296 -0
- webscout/Provider/OPENAI/chatgpt.py +870 -556
- webscout/Provider/OPENAI/chatsandbox.py +233 -173
- webscout/Provider/OPENAI/deepinfra.py +403 -322
- webscout/Provider/OPENAI/e2b.py +2370 -1414
- webscout/Provider/OPENAI/elmo.py +278 -0
- webscout/Provider/OPENAI/exaai.py +452 -417
- webscout/Provider/OPENAI/freeassist.py +446 -0
- webscout/Provider/OPENAI/gradient.py +448 -0
- webscout/Provider/OPENAI/groq.py +380 -364
- webscout/Provider/OPENAI/hadadxyz.py +292 -0
- webscout/Provider/OPENAI/heckai.py +333 -308
- webscout/Provider/OPENAI/huggingface.py +321 -0
- webscout/Provider/OPENAI/ibm.py +425 -0
- webscout/Provider/OPENAI/llmchat.py +253 -0
- webscout/Provider/OPENAI/llmchatco.py +378 -335
- webscout/Provider/OPENAI/meta.py +541 -0
- webscout/Provider/OPENAI/netwrck.py +374 -357
- webscout/Provider/OPENAI/nvidia.py +317 -0
- webscout/Provider/OPENAI/oivscode.py +348 -287
- webscout/Provider/OPENAI/openrouter.py +328 -0
- webscout/Provider/OPENAI/pydantic_imports.py +1 -172
- webscout/Provider/OPENAI/sambanova.py +397 -0
- webscout/Provider/OPENAI/sonus.py +305 -304
- webscout/Provider/OPENAI/textpollinations.py +370 -339
- webscout/Provider/OPENAI/toolbaz.py +375 -413
- webscout/Provider/OPENAI/typefully.py +419 -355
- webscout/Provider/OPENAI/typliai.py +279 -0
- webscout/Provider/OPENAI/utils.py +314 -318
- webscout/Provider/OPENAI/wisecat.py +359 -387
- webscout/Provider/OPENAI/writecream.py +185 -163
- webscout/Provider/OPENAI/x0gpt.py +462 -365
- webscout/Provider/OPENAI/zenmux.py +380 -0
- webscout/Provider/OpenRouter.py +386 -0
- webscout/Provider/Openai.py +337 -496
- webscout/Provider/PI.py +443 -429
- webscout/Provider/QwenLM.py +346 -254
- webscout/Provider/STT/__init__.py +28 -0
- webscout/Provider/STT/base.py +303 -0
- webscout/Provider/STT/elevenlabs.py +264 -0
- webscout/Provider/Sambanova.py +317 -0
- webscout/Provider/TTI/README.md +69 -82
- webscout/Provider/TTI/__init__.py +37 -7
- webscout/Provider/TTI/base.py +147 -64
- webscout/Provider/TTI/claudeonline.py +393 -0
- webscout/Provider/TTI/magicstudio.py +292 -201
- webscout/Provider/TTI/miragic.py +180 -0
- webscout/Provider/TTI/pollinations.py +331 -221
- webscout/Provider/TTI/together.py +334 -0
- webscout/Provider/TTI/utils.py +14 -11
- webscout/Provider/TTS/README.md +186 -192
- webscout/Provider/TTS/__init__.py +43 -10
- webscout/Provider/TTS/base.py +523 -159
- webscout/Provider/TTS/deepgram.py +286 -156
- webscout/Provider/TTS/elevenlabs.py +189 -111
- webscout/Provider/TTS/freetts.py +218 -0
- webscout/Provider/TTS/murfai.py +288 -113
- webscout/Provider/TTS/openai_fm.py +364 -129
- webscout/Provider/TTS/parler.py +203 -111
- webscout/Provider/TTS/qwen.py +334 -0
- webscout/Provider/TTS/sherpa.py +286 -0
- webscout/Provider/TTS/speechma.py +693 -580
- webscout/Provider/TTS/streamElements.py +275 -333
- webscout/Provider/TTS/utils.py +280 -280
- webscout/Provider/TextPollinationsAI.py +331 -308
- webscout/Provider/TogetherAI.py +450 -0
- webscout/Provider/TwoAI.py +309 -475
- webscout/Provider/TypliAI.py +311 -305
- webscout/Provider/UNFINISHED/ChatHub.py +219 -209
- webscout/Provider/{OPENAI/glider.py → UNFINISHED/ChutesAI.py} +331 -326
- webscout/Provider/{GizAI.py → UNFINISHED/GizAI.py} +300 -295
- webscout/Provider/{Marcus.py → UNFINISHED/Marcus.py} +218 -198
- webscout/Provider/UNFINISHED/Qodo.py +481 -0
- webscout/Provider/{MCPCore.py → UNFINISHED/XenAI.py} +330 -315
- webscout/Provider/UNFINISHED/Youchat.py +347 -330
- webscout/Provider/UNFINISHED/aihumanizer.py +41 -0
- webscout/Provider/UNFINISHED/grammerchecker.py +37 -0
- webscout/Provider/UNFINISHED/liner.py +342 -0
- webscout/Provider/UNFINISHED/liner_api_request.py +246 -263
- webscout/Provider/{samurai.py → UNFINISHED/samurai.py} +231 -224
- webscout/Provider/WiseCat.py +256 -233
- webscout/Provider/WrDoChat.py +390 -370
- webscout/Provider/__init__.py +115 -174
- webscout/Provider/ai4chat.py +181 -174
- webscout/Provider/akashgpt.py +330 -335
- webscout/Provider/cerebras.py +397 -290
- webscout/Provider/cleeai.py +236 -213
- webscout/Provider/elmo.py +291 -283
- webscout/Provider/geminiapi.py +343 -208
- webscout/Provider/julius.py +245 -223
- webscout/Provider/learnfastai.py +333 -325
- webscout/Provider/llama3mitril.py +230 -215
- webscout/Provider/llmchat.py +308 -258
- webscout/Provider/llmchatco.py +321 -306
- webscout/Provider/meta.py +996 -801
- webscout/Provider/oivscode.py +332 -309
- webscout/Provider/searchchat.py +316 -292
- webscout/Provider/sonus.py +264 -258
- webscout/Provider/toolbaz.py +359 -353
- webscout/Provider/turboseek.py +332 -266
- webscout/Provider/typefully.py +262 -202
- webscout/Provider/x0gpt.py +332 -299
- webscout/__init__.py +31 -39
- webscout/__main__.py +5 -5
- webscout/cli.py +585 -524
- webscout/client.py +1497 -70
- webscout/conversation.py +140 -436
- webscout/exceptions.py +383 -362
- webscout/litagent/__init__.py +29 -29
- webscout/litagent/agent.py +492 -455
- webscout/litagent/constants.py +60 -60
- webscout/models.py +505 -181
- webscout/optimizers.py +74 -420
- webscout/prompt_manager.py +376 -288
- webscout/sanitize.py +1514 -0
- webscout/scout/README.md +452 -404
- webscout/scout/__init__.py +8 -8
- webscout/scout/core/__init__.py +7 -7
- webscout/scout/core/crawler.py +330 -210
- webscout/scout/core/scout.py +800 -607
- webscout/scout/core/search_result.py +51 -96
- webscout/scout/core/text_analyzer.py +64 -63
- webscout/scout/core/text_utils.py +412 -277
- webscout/scout/core/web_analyzer.py +54 -52
- webscout/scout/element.py +872 -478
- webscout/scout/parsers/__init__.py +70 -69
- webscout/scout/parsers/html5lib_parser.py +182 -172
- webscout/scout/parsers/html_parser.py +238 -236
- webscout/scout/parsers/lxml_parser.py +203 -178
- webscout/scout/utils.py +38 -37
- webscout/search/__init__.py +47 -0
- webscout/search/base.py +201 -0
- webscout/search/bing_main.py +45 -0
- webscout/search/brave_main.py +92 -0
- webscout/search/duckduckgo_main.py +57 -0
- webscout/search/engines/__init__.py +127 -0
- webscout/search/engines/bing/__init__.py +15 -0
- webscout/search/engines/bing/base.py +35 -0
- webscout/search/engines/bing/images.py +114 -0
- webscout/search/engines/bing/news.py +96 -0
- webscout/search/engines/bing/suggestions.py +36 -0
- webscout/search/engines/bing/text.py +109 -0
- webscout/search/engines/brave/__init__.py +19 -0
- webscout/search/engines/brave/base.py +47 -0
- webscout/search/engines/brave/images.py +213 -0
- webscout/search/engines/brave/news.py +353 -0
- webscout/search/engines/brave/suggestions.py +318 -0
- webscout/search/engines/brave/text.py +167 -0
- webscout/search/engines/brave/videos.py +364 -0
- webscout/search/engines/duckduckgo/__init__.py +25 -0
- webscout/search/engines/duckduckgo/answers.py +80 -0
- webscout/search/engines/duckduckgo/base.py +189 -0
- webscout/search/engines/duckduckgo/images.py +100 -0
- webscout/search/engines/duckduckgo/maps.py +183 -0
- webscout/search/engines/duckduckgo/news.py +70 -0
- webscout/search/engines/duckduckgo/suggestions.py +22 -0
- webscout/search/engines/duckduckgo/text.py +221 -0
- webscout/search/engines/duckduckgo/translate.py +48 -0
- webscout/search/engines/duckduckgo/videos.py +80 -0
- webscout/search/engines/duckduckgo/weather.py +84 -0
- webscout/search/engines/mojeek.py +61 -0
- webscout/search/engines/wikipedia.py +77 -0
- webscout/search/engines/yahoo/__init__.py +41 -0
- webscout/search/engines/yahoo/answers.py +19 -0
- webscout/search/engines/yahoo/base.py +34 -0
- webscout/search/engines/yahoo/images.py +323 -0
- webscout/search/engines/yahoo/maps.py +19 -0
- webscout/search/engines/yahoo/news.py +258 -0
- webscout/search/engines/yahoo/suggestions.py +140 -0
- webscout/search/engines/yahoo/text.py +273 -0
- webscout/search/engines/yahoo/translate.py +19 -0
- webscout/search/engines/yahoo/videos.py +302 -0
- webscout/search/engines/yahoo/weather.py +220 -0
- webscout/search/engines/yandex.py +67 -0
- webscout/search/engines/yep/__init__.py +13 -0
- webscout/search/engines/yep/base.py +34 -0
- webscout/search/engines/yep/images.py +101 -0
- webscout/search/engines/yep/suggestions.py +38 -0
- webscout/search/engines/yep/text.py +99 -0
- webscout/search/http_client.py +172 -0
- webscout/search/results.py +141 -0
- webscout/search/yahoo_main.py +57 -0
- webscout/search/yep_main.py +48 -0
- webscout/server/__init__.py +48 -0
- webscout/server/config.py +78 -0
- webscout/server/exceptions.py +69 -0
- webscout/server/providers.py +286 -0
- webscout/server/request_models.py +131 -0
- webscout/server/request_processing.py +404 -0
- webscout/server/routes.py +642 -0
- webscout/server/server.py +351 -0
- webscout/server/ui_templates.py +1171 -0
- webscout/swiftcli/__init__.py +79 -95
- webscout/swiftcli/core/__init__.py +7 -7
- webscout/swiftcli/core/cli.py +574 -297
- webscout/swiftcli/core/context.py +98 -104
- webscout/swiftcli/core/group.py +268 -241
- webscout/swiftcli/decorators/__init__.py +28 -28
- webscout/swiftcli/decorators/command.py +243 -221
- webscout/swiftcli/decorators/options.py +247 -220
- webscout/swiftcli/decorators/output.py +392 -252
- webscout/swiftcli/exceptions.py +21 -21
- webscout/swiftcli/plugins/__init__.py +9 -9
- webscout/swiftcli/plugins/base.py +134 -135
- webscout/swiftcli/plugins/manager.py +269 -269
- webscout/swiftcli/utils/__init__.py +58 -59
- webscout/swiftcli/utils/formatting.py +251 -252
- webscout/swiftcli/utils/parsing.py +368 -267
- webscout/update_checker.py +280 -136
- webscout/utils.py +28 -14
- webscout/version.py +2 -1
- webscout/version.py.bak +3 -0
- webscout/zeroart/__init__.py +218 -135
- webscout/zeroart/base.py +70 -66
- webscout/zeroart/effects.py +155 -101
- webscout/zeroart/fonts.py +1799 -1239
- webscout-2026.1.19.dist-info/METADATA +638 -0
- webscout-2026.1.19.dist-info/RECORD +312 -0
- {webscout-8.2.9.dist-info → webscout-2026.1.19.dist-info}/WHEEL +1 -1
- {webscout-8.2.9.dist-info → webscout-2026.1.19.dist-info}/entry_points.txt +1 -1
- webscout/DWEBS.py +0 -520
- webscout/Extra/Act.md +0 -309
- webscout/Extra/GitToolkit/gitapi/README.md +0 -110
- webscout/Extra/autocoder/__init__.py +0 -9
- webscout/Extra/autocoder/autocoder.py +0 -1105
- webscout/Extra/autocoder/autocoder_utiles.py +0 -332
- webscout/Extra/gguf.md +0 -430
- webscout/Extra/weather.md +0 -281
- webscout/Litlogger/README.md +0 -10
- webscout/Litlogger/__init__.py +0 -15
- webscout/Litlogger/formats.py +0 -4
- webscout/Litlogger/handlers.py +0 -103
- webscout/Litlogger/levels.py +0 -13
- webscout/Litlogger/logger.py +0 -92
- webscout/Provider/AI21.py +0 -177
- webscout/Provider/AISEARCH/DeepFind.py +0 -254
- webscout/Provider/AISEARCH/felo_search.py +0 -202
- webscout/Provider/AISEARCH/genspark_search.py +0 -324
- webscout/Provider/AISEARCH/hika_search.py +0 -186
- webscout/Provider/AISEARCH/scira_search.py +0 -298
- webscout/Provider/Aitopia.py +0 -316
- webscout/Provider/AllenAI.py +0 -440
- webscout/Provider/Blackboxai.py +0 -791
- webscout/Provider/ChatGPTClone.py +0 -237
- webscout/Provider/ChatGPTGratis.py +0 -194
- webscout/Provider/Cloudflare.py +0 -324
- webscout/Provider/ExaChat.py +0 -358
- webscout/Provider/Flowith.py +0 -217
- webscout/Provider/FreeGemini.py +0 -250
- webscout/Provider/Glider.py +0 -225
- webscout/Provider/HF_space/__init__.py +0 -0
- webscout/Provider/HF_space/qwen_qwen2.py +0 -206
- webscout/Provider/HuggingFaceChat.py +0 -469
- webscout/Provider/Hunyuan.py +0 -283
- webscout/Provider/LambdaChat.py +0 -411
- webscout/Provider/Llama3.py +0 -259
- webscout/Provider/Nemotron.py +0 -218
- webscout/Provider/OLLAMA.py +0 -396
- webscout/Provider/OPENAI/BLACKBOXAI.py +0 -766
- webscout/Provider/OPENAI/Cloudflare.py +0 -378
- webscout/Provider/OPENAI/FreeGemini.py +0 -283
- webscout/Provider/OPENAI/NEMOTRON.py +0 -232
- webscout/Provider/OPENAI/Qwen3.py +0 -283
- webscout/Provider/OPENAI/api.py +0 -969
- webscout/Provider/OPENAI/c4ai.py +0 -373
- webscout/Provider/OPENAI/chatgptclone.py +0 -494
- webscout/Provider/OPENAI/copilot.py +0 -242
- webscout/Provider/OPENAI/flowith.py +0 -162
- webscout/Provider/OPENAI/freeaichat.py +0 -359
- webscout/Provider/OPENAI/mcpcore.py +0 -389
- webscout/Provider/OPENAI/multichat.py +0 -376
- webscout/Provider/OPENAI/opkfc.py +0 -496
- webscout/Provider/OPENAI/scirachat.py +0 -477
- webscout/Provider/OPENAI/standardinput.py +0 -433
- webscout/Provider/OPENAI/typegpt.py +0 -364
- webscout/Provider/OPENAI/uncovrAI.py +0 -463
- webscout/Provider/OPENAI/venice.py +0 -431
- webscout/Provider/OPENAI/yep.py +0 -382
- webscout/Provider/OpenGPT.py +0 -209
- webscout/Provider/Perplexitylabs.py +0 -415
- webscout/Provider/Reka.py +0 -214
- webscout/Provider/StandardInput.py +0 -290
- webscout/Provider/TTI/aiarta.py +0 -365
- webscout/Provider/TTI/artbit.py +0 -0
- webscout/Provider/TTI/fastflux.py +0 -200
- webscout/Provider/TTI/piclumen.py +0 -203
- webscout/Provider/TTI/pixelmuse.py +0 -225
- webscout/Provider/TTS/gesserit.py +0 -128
- webscout/Provider/TTS/sthir.py +0 -94
- webscout/Provider/TeachAnything.py +0 -229
- webscout/Provider/UNFINISHED/puterjs.py +0 -635
- webscout/Provider/UNFINISHED/test_lmarena.py +0 -119
- webscout/Provider/Venice.py +0 -258
- webscout/Provider/VercelAI.py +0 -253
- webscout/Provider/Writecream.py +0 -246
- webscout/Provider/WritingMate.py +0 -269
- webscout/Provider/asksteve.py +0 -220
- webscout/Provider/chatglm.py +0 -215
- webscout/Provider/copilot.py +0 -425
- webscout/Provider/freeaichat.py +0 -285
- webscout/Provider/granite.py +0 -235
- webscout/Provider/hermes.py +0 -266
- webscout/Provider/koala.py +0 -170
- webscout/Provider/lmarena.py +0 -198
- webscout/Provider/multichat.py +0 -364
- webscout/Provider/scira_chat.py +0 -299
- webscout/Provider/scnet.py +0 -243
- webscout/Provider/talkai.py +0 -194
- webscout/Provider/typegpt.py +0 -289
- webscout/Provider/uncovr.py +0 -368
- webscout/Provider/yep.py +0 -389
- webscout/litagent/Readme.md +0 -276
- webscout/litprinter/__init__.py +0 -59
- webscout/swiftcli/Readme.md +0 -323
- webscout/tempid.py +0 -128
- webscout/webscout_search.py +0 -1184
- webscout/webscout_search_async.py +0 -654
- webscout/yep_search.py +0 -347
- webscout/zeroart/README.md +0 -89
- webscout-8.2.9.dist-info/METADATA +0 -1033
- webscout-8.2.9.dist-info/RECORD +0 -289
- {webscout-8.2.9.dist-info → webscout-2026.1.19.dist-info}/licenses/LICENSE.md +0 -0
- {webscout-8.2.9.dist-info → webscout-2026.1.19.dist-info}/top_level.txt +0 -0
webscout/Bard.py
CHANGED
|
@@ -1,1026 +1,1072 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
from curl_cffi import
|
|
18
|
-
from
|
|
19
|
-
|
|
20
|
-
from
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
INIT = "https://gemini.google.com/app"
|
|
48
|
-
GENERATE = "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate"
|
|
49
|
-
ROTATE_COOKIES = "https://accounts.google.com/RotateCookies"
|
|
50
|
-
UPLOAD = "https://content-push.googleapis.com/upload"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
"X-Same-Domain": "1",
|
|
69
|
-
}
|
|
70
|
-
ROTATE_COOKIES = {
|
|
71
|
-
"Content-Type": "application/json",
|
|
72
|
-
}
|
|
73
|
-
UPLOAD = {"Push-ID": "feeds/mcudyrk2a4khkz"}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
False,
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
"gemini-
|
|
103
|
-
{
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
"""
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
self.
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
"
|
|
326
|
-
"
|
|
327
|
-
"
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
"
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
self,
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
self.
|
|
354
|
-
self.
|
|
355
|
-
self.
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
self.
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
self.
|
|
367
|
-
self.
|
|
368
|
-
self.
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
"
|
|
415
|
-
"
|
|
416
|
-
"
|
|
417
|
-
"
|
|
418
|
-
"
|
|
419
|
-
"
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
error_message
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
[
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
lines
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
chat_data_line = None
|
|
619
|
-
for line in lines:
|
|
620
|
-
if line.startswith(")]}'"):
|
|
621
|
-
chat_data_line = line[4:].strip()
|
|
622
|
-
break
|
|
623
|
-
elif line.startswith("["):
|
|
624
|
-
chat_data_line = line
|
|
625
|
-
break
|
|
626
|
-
|
|
627
|
-
if not chat_data_line:
|
|
628
|
-
chat_data_line = lines[3] if len(lines) > 3 else lines[-1]
|
|
629
|
-
if chat_data_line.startswith(")]}'"):
|
|
630
|
-
chat_data_line = chat_data_line[4:].strip()
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
img_url
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
generated_images.append(
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
for part_index, part in enumerate(response_json):
|
|
712
|
-
if part_index <= body_index:
|
|
713
|
-
continue
|
|
714
|
-
try:
|
|
715
|
-
img_part = json.loads(part[2])
|
|
716
|
-
if img_part[4][0][12][7][0]:
|
|
717
|
-
for img_index, img_data in enumerate(
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
return
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
"""
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
""
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
"""
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import random
|
|
5
|
+
import re
|
|
6
|
+
import string
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from enum import Enum
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, List, Optional, Tuple, TypedDict, Union, cast
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
import trio # type: ignore # noqa: F401
|
|
14
|
+
except ImportError:
|
|
15
|
+
pass
|
|
16
|
+
from curl_cffi import CurlError
|
|
17
|
+
from curl_cffi.requests import AsyncSession
|
|
18
|
+
from pydantic import BaseModel, field_validator
|
|
19
|
+
from requests.exceptions import HTTPError, RequestException, Timeout
|
|
20
|
+
from rich.console import Console
|
|
21
|
+
|
|
22
|
+
console = Console()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AskResponse(TypedDict):
|
|
26
|
+
content: str
|
|
27
|
+
conversation_id: str
|
|
28
|
+
response_id: str
|
|
29
|
+
factualityQueries: Optional[List[Any]]
|
|
30
|
+
textQuery: str
|
|
31
|
+
choices: List[Dict[str, Union[str, List[str]]]]
|
|
32
|
+
images: List[Dict[str, str]]
|
|
33
|
+
error: bool
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Endpoint(Enum):
|
|
37
|
+
"""
|
|
38
|
+
Enum for Google Gemini API endpoints.
|
|
39
|
+
|
|
40
|
+
Attributes:
|
|
41
|
+
INIT (str): URL for initializing the Gemini session.
|
|
42
|
+
GENERATE (str): URL for generating chat responses.
|
|
43
|
+
ROTATE_COOKIES (str): URL for rotating authentication cookies.
|
|
44
|
+
UPLOAD (str): URL for uploading files/images.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
INIT = "https://gemini.google.com/app"
|
|
48
|
+
GENERATE = "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate"
|
|
49
|
+
ROTATE_COOKIES = "https://accounts.google.com/RotateCookies"
|
|
50
|
+
UPLOAD = "https://content-push.googleapis.com/upload"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Headers(Enum):
|
|
54
|
+
"""
|
|
55
|
+
Enum for HTTP headers used in Gemini API requests.
|
|
56
|
+
|
|
57
|
+
Attributes:
|
|
58
|
+
GEMINI (dict): Headers for Gemini chat requests.
|
|
59
|
+
ROTATE_COOKIES (dict): Headers for rotating cookies.
|
|
60
|
+
UPLOAD (dict): Headers for file/image upload.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
GEMINI = {
|
|
64
|
+
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
|
|
65
|
+
"Host": "gemini.google.com",
|
|
66
|
+
"Origin": "https://gemini.google.com",
|
|
67
|
+
"Referer": "https://gemini.google.com/",
|
|
68
|
+
"X-Same-Domain": "1",
|
|
69
|
+
}
|
|
70
|
+
ROTATE_COOKIES = {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
}
|
|
73
|
+
UPLOAD = {"Push-ID": "feeds/mcudyrk2a4khkz"}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Model(Enum):
|
|
77
|
+
"""
|
|
78
|
+
Enum for available Gemini model configurations.
|
|
79
|
+
|
|
80
|
+
Attributes:
|
|
81
|
+
model_name (str): Name of the model.
|
|
82
|
+
model_header (dict): Additional headers required for the model.
|
|
83
|
+
advanced_only (bool): Whether the model is available only for advanced users.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
UNSPECIFIED = ("unspecified", {}, False)
|
|
87
|
+
GEMINI_3_0_PRO = (
|
|
88
|
+
"gemini-3.0-pro",
|
|
89
|
+
{
|
|
90
|
+
"x-goog-ext-525001261-jspb": '[1,null,null,null,"e6fa609c3fa255c0",null,null,0,[4],null,null,2]'
|
|
91
|
+
},
|
|
92
|
+
False,
|
|
93
|
+
)
|
|
94
|
+
GEMINI_3_0_FLASH = (
|
|
95
|
+
"gemini-3.0-flash",
|
|
96
|
+
{
|
|
97
|
+
"x-goog-ext-525001261-jspb": '[1,null,null,null,"56fdd199312815e2",null,null,0,[4],null,null,2]'
|
|
98
|
+
},
|
|
99
|
+
False,
|
|
100
|
+
)
|
|
101
|
+
GEMINI_3_0_FLASH_THINKING = (
|
|
102
|
+
"gemini-3.0-flash-thinking",
|
|
103
|
+
{
|
|
104
|
+
"x-goog-ext-525001261-jspb": '[1,null,null,null,"e051ce1aa80aa576",null,null,0,[4],null,null,2]'
|
|
105
|
+
},
|
|
106
|
+
False,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
def __init__(self, name: str, header: Dict[str, str], advanced_only: bool):
|
|
110
|
+
"""
|
|
111
|
+
Initialize a Model enum member.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
name (str): Model name.
|
|
115
|
+
header (dict): Model-specific headers.
|
|
116
|
+
advanced_only (bool): If True, model is for advanced users only.
|
|
117
|
+
"""
|
|
118
|
+
self.model_name = name
|
|
119
|
+
self.model_header = header
|
|
120
|
+
self.advanced_only = advanced_only
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
def from_name(cls, name: str):
|
|
124
|
+
"""
|
|
125
|
+
Get a Model enum member by its model name.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
name (str): Name of the model.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
Model: Corresponding Model enum member.
|
|
132
|
+
|
|
133
|
+
Raises:
|
|
134
|
+
ValueError: If the model name is not found.
|
|
135
|
+
"""
|
|
136
|
+
for model in cls:
|
|
137
|
+
if model.model_name == name:
|
|
138
|
+
return model
|
|
139
|
+
raise ValueError(
|
|
140
|
+
f"Unknown model name: {name}. Available models: {', '.join([model.model_name for model in cls])}"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async def upload_file(
|
|
145
|
+
file: Union[bytes, str, Path],
|
|
146
|
+
proxy: Optional[Union[str, Dict[str, str]]] = None,
|
|
147
|
+
impersonate: str = "chrome110",
|
|
148
|
+
) -> str:
|
|
149
|
+
"""
|
|
150
|
+
Uploads a file to Google's Gemini server using curl_cffi and returns its identifier.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
file (bytes | str | Path): File data in bytes or path to the file to be uploaded.
|
|
154
|
+
proxy (str | dict, optional): Proxy URL or dictionary for the request.
|
|
155
|
+
impersonate (str, optional): Browser profile for curl_cffi to impersonate. Defaults to "chrome110".
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
str: Identifier of the uploaded file.
|
|
159
|
+
|
|
160
|
+
Raises:
|
|
161
|
+
HTTPError: If the upload request fails.
|
|
162
|
+
RequestException: For other network-related errors.
|
|
163
|
+
FileNotFoundError: If the file path does not exist.
|
|
164
|
+
"""
|
|
165
|
+
if not isinstance(file, bytes):
|
|
166
|
+
file_path = Path(file)
|
|
167
|
+
if not file_path.is_file():
|
|
168
|
+
raise FileNotFoundError(f"File not found at path: {file}")
|
|
169
|
+
with open(file_path, "rb") as f:
|
|
170
|
+
file_content = f.read()
|
|
171
|
+
else:
|
|
172
|
+
file_content = file
|
|
173
|
+
|
|
174
|
+
proxies_dict = None
|
|
175
|
+
if isinstance(proxy, str):
|
|
176
|
+
proxies_dict = {"http": proxy, "https": proxy}
|
|
177
|
+
elif isinstance(proxy, dict):
|
|
178
|
+
proxies_dict = proxy
|
|
179
|
+
|
|
180
|
+
try:
|
|
181
|
+
async with AsyncSession(
|
|
182
|
+
proxies=cast(Any, proxies_dict),
|
|
183
|
+
impersonate=cast(Any, impersonate),
|
|
184
|
+
headers=Headers.UPLOAD.value,
|
|
185
|
+
) as client:
|
|
186
|
+
response = await client.post(
|
|
187
|
+
url=Endpoint.UPLOAD.value,
|
|
188
|
+
files={"file": file_content},
|
|
189
|
+
)
|
|
190
|
+
response.raise_for_status()
|
|
191
|
+
return response.text
|
|
192
|
+
except HTTPError as e:
|
|
193
|
+
console.log(f"[red]HTTP error during file upload: {e.response.status_code} {e}[/red]")
|
|
194
|
+
raise
|
|
195
|
+
except (RequestException, CurlError) as e:
|
|
196
|
+
console.log(f"[red]Network error during file upload: {e}[/red]")
|
|
197
|
+
raise
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def load_cookies(cookie_path: str) -> Tuple[str, str]:
|
|
201
|
+
"""
|
|
202
|
+
Loads authentication cookies from a JSON file.
|
|
203
|
+
|
|
204
|
+
Args:
|
|
205
|
+
cookie_path (str): Path to the JSON file containing cookies.
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
tuple[str, str]: Tuple containing __Secure-1PSID and __Secure-1PSIDTS cookie values.
|
|
209
|
+
|
|
210
|
+
Raises:
|
|
211
|
+
Exception: If the file is not found, invalid, or required cookies are missing.
|
|
212
|
+
"""
|
|
213
|
+
try:
|
|
214
|
+
with open(cookie_path, "r", encoding="utf-8") as file:
|
|
215
|
+
cookies = json.load(file)
|
|
216
|
+
session_auth1 = next(
|
|
217
|
+
(item["value"] for item in cookies if item["name"].upper() == "__SECURE-1PSID"), None
|
|
218
|
+
)
|
|
219
|
+
session_auth2 = next(
|
|
220
|
+
(item["value"] for item in cookies if item["name"].upper() == "__SECURE-1PSIDTS"), None
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
if not session_auth1 or not session_auth2:
|
|
224
|
+
raise ValueError("Required cookies (__Secure-1PSID or __Secure-1PSIDTS) not found.")
|
|
225
|
+
|
|
226
|
+
return session_auth1, session_auth2
|
|
227
|
+
except FileNotFoundError:
|
|
228
|
+
raise Exception(f"Cookie file not found at path: {cookie_path}")
|
|
229
|
+
except json.JSONDecodeError:
|
|
230
|
+
raise Exception("Invalid JSON format in the cookie file.")
|
|
231
|
+
except StopIteration as e:
|
|
232
|
+
raise Exception(f"{e} Check the cookie file format and content.")
|
|
233
|
+
except Exception as e:
|
|
234
|
+
raise Exception(f"An unexpected error occurred while loading cookies: {e}")
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
class Chatbot:
|
|
238
|
+
"""
|
|
239
|
+
Synchronous wrapper for the AsyncChatbot class.
|
|
240
|
+
|
|
241
|
+
This class provides a synchronous interface to interact with Google Gemini,
|
|
242
|
+
handling authentication, conversation management, and message sending.
|
|
243
|
+
|
|
244
|
+
Attributes:
|
|
245
|
+
loop (asyncio.AbstractEventLoop): Event loop for running async tasks.
|
|
246
|
+
secure_1psid (str): Authentication cookie.
|
|
247
|
+
secure_1psidts (str): Authentication cookie.
|
|
248
|
+
async_chatbot (AsyncChatbot): Underlying asynchronous chatbot instance.
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
def __init__(
|
|
252
|
+
self,
|
|
253
|
+
cookie_path: str,
|
|
254
|
+
proxy: Optional[Union[str, Dict[str, str]]] = None,
|
|
255
|
+
timeout: int = 20,
|
|
256
|
+
model: Model = Model.UNSPECIFIED,
|
|
257
|
+
impersonate: str = "chrome110",
|
|
258
|
+
):
|
|
259
|
+
try:
|
|
260
|
+
self.loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
|
|
261
|
+
except RuntimeError:
|
|
262
|
+
self.loop = asyncio.new_event_loop()
|
|
263
|
+
asyncio.set_event_loop(self.loop)
|
|
264
|
+
|
|
265
|
+
self.secure_1psid, self.secure_1psidts = load_cookies(cookie_path)
|
|
266
|
+
self.async_chatbot = self.loop.run_until_complete(
|
|
267
|
+
AsyncChatbot.create(
|
|
268
|
+
self.secure_1psid, self.secure_1psidts, proxy, timeout, model, impersonate
|
|
269
|
+
)
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def save_conversation(self, file_path: str, conversation_name: str):
|
|
273
|
+
return self.loop.run_until_complete(
|
|
274
|
+
self.async_chatbot.save_conversation(file_path, conversation_name)
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
def load_conversations(self, file_path: str) -> List[Dict]:
|
|
278
|
+
return self.loop.run_until_complete(self.async_chatbot.load_conversations(file_path))
|
|
279
|
+
|
|
280
|
+
def load_conversation(self, file_path: str, conversation_name: str) -> bool:
|
|
281
|
+
return self.loop.run_until_complete(
|
|
282
|
+
self.async_chatbot.load_conversation(file_path, conversation_name)
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
def ask(self, message: str, image: Optional[Union[bytes, str, Path]] = None) -> AskResponse:
|
|
286
|
+
return self.loop.run_until_complete(self.async_chatbot.ask(message, image=image))
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class AsyncChatbot:
|
|
290
|
+
"""
|
|
291
|
+
Asynchronous chatbot client for interacting with Google Gemini using curl_cffi.
|
|
292
|
+
|
|
293
|
+
This class manages authentication, session state, conversation history,
|
|
294
|
+
and sending/receiving messages (including images) asynchronously.
|
|
295
|
+
|
|
296
|
+
Attributes:
|
|
297
|
+
headers (dict): HTTP headers for requests.
|
|
298
|
+
_reqid (int): Request identifier for Gemini API.
|
|
299
|
+
SNlM0e (str): Session token required for API requests.
|
|
300
|
+
conversation_id (str): Current conversation ID.
|
|
301
|
+
response_id (str): Current response ID.
|
|
302
|
+
choice_id (str): Current choice ID.
|
|
303
|
+
proxy (str | dict | None): Proxy configuration.
|
|
304
|
+
proxies_dict (dict | None): Proxy dictionary for curl_cffi.
|
|
305
|
+
secure_1psid (str): Authentication cookie.
|
|
306
|
+
secure_1psidts (str): Authentication cookie.
|
|
307
|
+
session (AsyncSession): curl_cffi session for HTTP requests.
|
|
308
|
+
timeout (int): Request timeout in seconds.
|
|
309
|
+
model (Model): Selected Gemini model.
|
|
310
|
+
impersonate (str): Browser profile for curl_cffi to impersonate.
|
|
311
|
+
"""
|
|
312
|
+
|
|
313
|
+
__slots__ = [
|
|
314
|
+
"headers",
|
|
315
|
+
"_reqid",
|
|
316
|
+
"SNlM0e",
|
|
317
|
+
"conversation_id",
|
|
318
|
+
"response_id",
|
|
319
|
+
"choice_id",
|
|
320
|
+
"proxy",
|
|
321
|
+
"proxies_dict",
|
|
322
|
+
"secure_1psidts",
|
|
323
|
+
"secure_1psid",
|
|
324
|
+
"session",
|
|
325
|
+
"timeout",
|
|
326
|
+
"model",
|
|
327
|
+
"impersonate",
|
|
328
|
+
]
|
|
329
|
+
|
|
330
|
+
def __init__(
|
|
331
|
+
self,
|
|
332
|
+
secure_1psid: str,
|
|
333
|
+
secure_1psidts: str,
|
|
334
|
+
proxy: Optional[Union[str, Dict[str, str]]] = None,
|
|
335
|
+
timeout: int = 20,
|
|
336
|
+
model: Model = Model.UNSPECIFIED,
|
|
337
|
+
impersonate: str = "chrome110",
|
|
338
|
+
):
|
|
339
|
+
headers = Headers.GEMINI.value.copy()
|
|
340
|
+
if model != Model.UNSPECIFIED:
|
|
341
|
+
headers.update(model.model_header)
|
|
342
|
+
self._reqid = int("".join(random.choices(string.digits, k=7)))
|
|
343
|
+
self.proxy = proxy
|
|
344
|
+
self.impersonate = impersonate
|
|
345
|
+
|
|
346
|
+
self.proxies_dict = None
|
|
347
|
+
if isinstance(proxy, str):
|
|
348
|
+
self.proxies_dict = {"http": proxy, "https": proxy}
|
|
349
|
+
elif isinstance(proxy, dict):
|
|
350
|
+
self.proxies_dict = proxy
|
|
351
|
+
|
|
352
|
+
self.conversation_id = ""
|
|
353
|
+
self.response_id = ""
|
|
354
|
+
self.choice_id = ""
|
|
355
|
+
self.secure_1psid = secure_1psid
|
|
356
|
+
self.secure_1psidts = secure_1psidts
|
|
357
|
+
|
|
358
|
+
self.session: AsyncSession = AsyncSession(
|
|
359
|
+
headers=headers,
|
|
360
|
+
cookies={"__Secure-1PSID": secure_1psid, "__Secure-1PSIDTS": secure_1psidts},
|
|
361
|
+
proxies=cast(Any, self.proxies_dict if self.proxies_dict else None),
|
|
362
|
+
timeout=timeout,
|
|
363
|
+
impersonate=cast(Any, self.impersonate if self.impersonate else None),
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
self.timeout = timeout
|
|
367
|
+
self.model = model
|
|
368
|
+
self.SNlM0e = None
|
|
369
|
+
|
|
370
|
+
@classmethod
|
|
371
|
+
async def create(
|
|
372
|
+
cls,
|
|
373
|
+
secure_1psid: str,
|
|
374
|
+
secure_1psidts: str,
|
|
375
|
+
proxy: Optional[Union[str, Dict[str, str]]] = None,
|
|
376
|
+
timeout: int = 20,
|
|
377
|
+
model: Model = Model.UNSPECIFIED,
|
|
378
|
+
impersonate: str = "chrome110",
|
|
379
|
+
) -> "AsyncChatbot":
|
|
380
|
+
"""
|
|
381
|
+
Factory method to create and initialize an AsyncChatbot instance.
|
|
382
|
+
Fetches the necessary SNlM0e value asynchronously.
|
|
383
|
+
"""
|
|
384
|
+
instance = cls(secure_1psid, secure_1psidts, proxy, timeout, model, impersonate)
|
|
385
|
+
try:
|
|
386
|
+
instance.SNlM0e = await instance.__get_snlm0e()
|
|
387
|
+
except Exception as e:
|
|
388
|
+
console.log(
|
|
389
|
+
f"[red]Error during AsyncChatbot initialization (__get_snlm0e): {e}[/red]",
|
|
390
|
+
style="bold red",
|
|
391
|
+
)
|
|
392
|
+
await instance.session.close()
|
|
393
|
+
raise
|
|
394
|
+
return instance
|
|
395
|
+
|
|
396
|
+
def _error_response(self, message: str) -> AskResponse:
|
|
397
|
+
"""Helper to create a consistent error response."""
|
|
398
|
+
return {
|
|
399
|
+
"content": message,
|
|
400
|
+
"conversation_id": getattr(self, "conversation_id", ""),
|
|
401
|
+
"response_id": getattr(self, "response_id", ""),
|
|
402
|
+
"factualityQueries": [],
|
|
403
|
+
"textQuery": "",
|
|
404
|
+
"choices": [],
|
|
405
|
+
"images": [],
|
|
406
|
+
"error": True,
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async def save_conversation(self, file_path: str, conversation_name: str) -> None:
|
|
410
|
+
conversations = await self.load_conversations(file_path)
|
|
411
|
+
conversation_data = {
|
|
412
|
+
"conversation_name": conversation_name,
|
|
413
|
+
"_reqid": self._reqid,
|
|
414
|
+
"conversation_id": self.conversation_id,
|
|
415
|
+
"response_id": self.response_id,
|
|
416
|
+
"choice_id": self.choice_id,
|
|
417
|
+
"SNlM0e": self.SNlM0e,
|
|
418
|
+
"model_name": self.model.model_name,
|
|
419
|
+
"timestamp": datetime.now().isoformat(),
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
found = False
|
|
423
|
+
for i, conv in enumerate(conversations):
|
|
424
|
+
if conv.get("conversation_name") == conversation_name:
|
|
425
|
+
conversations[i] = conversation_data
|
|
426
|
+
found = True
|
|
427
|
+
break
|
|
428
|
+
if not found:
|
|
429
|
+
conversations.append(conversation_data)
|
|
430
|
+
|
|
431
|
+
try:
|
|
432
|
+
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
|
|
433
|
+
with open(file_path, "w", encoding="utf-8") as f:
|
|
434
|
+
json.dump(conversations, f, indent=4, ensure_ascii=False)
|
|
435
|
+
except IOError as e:
|
|
436
|
+
console.log(f"[red]Error saving conversation to {file_path}: {e}[/red]")
|
|
437
|
+
raise
|
|
438
|
+
|
|
439
|
+
async def load_conversations(self, file_path: str) -> List[Dict]:
|
|
440
|
+
if not os.path.isfile(file_path):
|
|
441
|
+
return []
|
|
442
|
+
try:
|
|
443
|
+
with open(file_path, "r", encoding="utf-8") as f:
|
|
444
|
+
return json.load(f)
|
|
445
|
+
except (json.JSONDecodeError, IOError) as e:
|
|
446
|
+
console.log(f"[red]Error loading conversations from {file_path}: {e}[/red]")
|
|
447
|
+
return []
|
|
448
|
+
|
|
449
|
+
async def load_conversation(self, file_path: str, conversation_name: str) -> bool:
|
|
450
|
+
conversations = await self.load_conversations(file_path)
|
|
451
|
+
for conversation in conversations:
|
|
452
|
+
if conversation.get("conversation_name") == conversation_name:
|
|
453
|
+
try:
|
|
454
|
+
self._reqid = conversation["_reqid"]
|
|
455
|
+
self.conversation_id = conversation["conversation_id"]
|
|
456
|
+
self.response_id = conversation["response_id"]
|
|
457
|
+
self.choice_id = conversation["choice_id"]
|
|
458
|
+
self.SNlM0e = conversation["SNlM0e"]
|
|
459
|
+
if "model_name" in conversation:
|
|
460
|
+
try:
|
|
461
|
+
self.model = Model.from_name(conversation["model_name"])
|
|
462
|
+
self.session.headers.update(self.model.model_header)
|
|
463
|
+
except ValueError as e:
|
|
464
|
+
console.log(
|
|
465
|
+
f"[yellow]Warning: Model '{conversation['model_name']}' from saved conversation not found. Using current model '{self.model.model_name}'. Error: {e}[/yellow]"
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
console.log(f"Loaded conversation '{conversation_name}'")
|
|
469
|
+
return True
|
|
470
|
+
except KeyError as e:
|
|
471
|
+
console.log(
|
|
472
|
+
f"[red]Error loading conversation '{conversation_name}': Missing key {e}[/red]"
|
|
473
|
+
)
|
|
474
|
+
return False
|
|
475
|
+
console.log(f"[yellow]Conversation '{conversation_name}' not found in {file_path}[/yellow]")
|
|
476
|
+
return False
|
|
477
|
+
|
|
478
|
+
async def __get_snlm0e(self) -> str:
|
|
479
|
+
"""Fetches the SNlM0e value required for API requests using curl_cffi."""
|
|
480
|
+
if not self.secure_1psid:
|
|
481
|
+
raise ValueError("__Secure-1PSID cookie is required.")
|
|
482
|
+
|
|
483
|
+
try:
|
|
484
|
+
resp = await self.session.get(Endpoint.INIT.value, timeout=self.timeout)
|
|
485
|
+
resp.raise_for_status()
|
|
486
|
+
|
|
487
|
+
if "Sign in to continue" in resp.text or "accounts.google.com" in str(resp.url):
|
|
488
|
+
raise PermissionError(
|
|
489
|
+
"Authentication failed. Cookies might be invalid or expired. Please update them."
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
snlm0e_match = re.search(r'["\']SNlM0e["\']\s*:\s*["\'](.*?)["\']', resp.text)
|
|
493
|
+
if not snlm0e_match:
|
|
494
|
+
error_message = "SNlM0e value not found in response."
|
|
495
|
+
if resp.status_code == 429:
|
|
496
|
+
error_message += " Rate limit likely exceeded."
|
|
497
|
+
else:
|
|
498
|
+
error_message += (
|
|
499
|
+
f" Response status: {resp.status_code}. Check cookie validity and network."
|
|
500
|
+
)
|
|
501
|
+
raise ValueError(error_message)
|
|
502
|
+
|
|
503
|
+
if not self.secure_1psidts and "PSIDTS" not in self.session.cookies:
|
|
504
|
+
try:
|
|
505
|
+
await self.__rotate_cookies()
|
|
506
|
+
except Exception as e:
|
|
507
|
+
console.log(f"[yellow]Warning: Could not refresh PSIDTS cookie: {e}[/yellow]")
|
|
508
|
+
|
|
509
|
+
return snlm0e_match.group(1)
|
|
510
|
+
|
|
511
|
+
except Timeout as e:
|
|
512
|
+
raise TimeoutError(f"Request timed out while fetching SNlM0e: {e}") from e
|
|
513
|
+
except (RequestException, CurlError) as e:
|
|
514
|
+
raise ConnectionError(f"Network error while fetching SNlM0e: {e}") from e
|
|
515
|
+
except Exception as e:
|
|
516
|
+
if isinstance(e, HTTPError) and (
|
|
517
|
+
e.response.status_code == 401 or e.response.status_code == 403
|
|
518
|
+
):
|
|
519
|
+
raise PermissionError(
|
|
520
|
+
f"Authentication failed (status {e.response.status_code}). Check cookies. {e}"
|
|
521
|
+
) from e
|
|
522
|
+
else:
|
|
523
|
+
raise Exception(f"Error while fetching SNlM0e: {e}") from e
|
|
524
|
+
|
|
525
|
+
async def __rotate_cookies(self) -> Optional[str]:
|
|
526
|
+
"""Rotates the __Secure-1PSIDTS cookie."""
|
|
527
|
+
try:
|
|
528
|
+
response = await self.session.post(
|
|
529
|
+
Endpoint.ROTATE_COOKIES.value,
|
|
530
|
+
headers=Headers.ROTATE_COOKIES.value,
|
|
531
|
+
data='[000,"-0000000000000000000"]',
|
|
532
|
+
timeout=self.timeout,
|
|
533
|
+
)
|
|
534
|
+
response.raise_for_status()
|
|
535
|
+
|
|
536
|
+
if new_1psidts := response.cookies.get("__Secure-1PSIDTS"):
|
|
537
|
+
self.secure_1psidts = new_1psidts
|
|
538
|
+
self.session.cookies.set("__Secure-1PSIDTS", new_1psidts)
|
|
539
|
+
return new_1psidts
|
|
540
|
+
except Exception as e:
|
|
541
|
+
console.log(f"[yellow]Cookie rotation failed: {e}[/yellow]")
|
|
542
|
+
raise
|
|
543
|
+
|
|
544
|
+
async def ask(
|
|
545
|
+
self, message: str, image: Optional[Union[bytes, str, Path]] = None
|
|
546
|
+
) -> AskResponse:
|
|
547
|
+
"""
|
|
548
|
+
Sends a message to Google Gemini and returns the response using curl_cffi.
|
|
549
|
+
|
|
550
|
+
Parameters:
|
|
551
|
+
message: str
|
|
552
|
+
The message to send.
|
|
553
|
+
image: Optional[Union[bytes, str, Path]]
|
|
554
|
+
Optional image data (bytes) or path to an image file to include.
|
|
555
|
+
|
|
556
|
+
Returns:
|
|
557
|
+
dict: A dictionary containing the response content and metadata.
|
|
558
|
+
"""
|
|
559
|
+
if self.SNlM0e is None:
|
|
560
|
+
raise RuntimeError("AsyncChatbot not properly initialized. Call AsyncChatbot.create()")
|
|
561
|
+
|
|
562
|
+
params = {
|
|
563
|
+
"bl": "boq_assistant-bard-web-server_20240625.13_p0",
|
|
564
|
+
"_reqid": str(self._reqid),
|
|
565
|
+
"rt": "c",
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
image_upload_id = None
|
|
569
|
+
if image:
|
|
570
|
+
try:
|
|
571
|
+
image_upload_id = await upload_file(
|
|
572
|
+
image, proxy=self.proxies_dict, impersonate=self.impersonate
|
|
573
|
+
)
|
|
574
|
+
console.log(f"Image uploaded successfully. ID: {image_upload_id}")
|
|
575
|
+
except Exception as e:
|
|
576
|
+
console.log(f"[red]Error uploading image: {e}[/red]")
|
|
577
|
+
return self._error_response(f"Error uploading image: {e}")
|
|
578
|
+
|
|
579
|
+
if image_upload_id:
|
|
580
|
+
message_struct = [
|
|
581
|
+
[message],
|
|
582
|
+
[[[image_upload_id, 1]]],
|
|
583
|
+
[self.conversation_id, self.response_id, self.choice_id],
|
|
584
|
+
]
|
|
585
|
+
else:
|
|
586
|
+
message_struct = [
|
|
587
|
+
[message],
|
|
588
|
+
None,
|
|
589
|
+
[self.conversation_id, self.response_id, self.choice_id],
|
|
590
|
+
]
|
|
591
|
+
|
|
592
|
+
data = {
|
|
593
|
+
"f.req": json.dumps(
|
|
594
|
+
[None, json.dumps(message_struct, ensure_ascii=False)], ensure_ascii=False
|
|
595
|
+
),
|
|
596
|
+
"at": self.SNlM0e,
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
resp = None
|
|
600
|
+
try:
|
|
601
|
+
resp = await self.session.post(
|
|
602
|
+
Endpoint.GENERATE.value,
|
|
603
|
+
params=params,
|
|
604
|
+
data=data,
|
|
605
|
+
timeout=self.timeout,
|
|
606
|
+
)
|
|
607
|
+
resp.raise_for_status()
|
|
608
|
+
|
|
609
|
+
if resp is None:
|
|
610
|
+
raise ValueError("Failed to get response from Gemini API")
|
|
611
|
+
|
|
612
|
+
lines = resp.text.splitlines()
|
|
613
|
+
if len(lines) < 3:
|
|
614
|
+
raise ValueError(
|
|
615
|
+
f"Unexpected response format. Status: {resp.status_code}. Content: {resp.text[:200]}..."
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
chat_data_line = None
|
|
619
|
+
for line in lines:
|
|
620
|
+
if line.startswith(")]}'"):
|
|
621
|
+
chat_data_line = line[4:].strip()
|
|
622
|
+
break
|
|
623
|
+
elif line.startswith("["):
|
|
624
|
+
chat_data_line = line
|
|
625
|
+
break
|
|
626
|
+
|
|
627
|
+
if not chat_data_line:
|
|
628
|
+
chat_data_line = lines[3] if len(lines) > 3 else lines[-1]
|
|
629
|
+
if chat_data_line.startswith(")]}'"):
|
|
630
|
+
chat_data_line = chat_data_line[4:].strip()
|
|
631
|
+
|
|
632
|
+
response_json = json.loads(chat_data_line)
|
|
633
|
+
|
|
634
|
+
body = None
|
|
635
|
+
body_index = 0
|
|
636
|
+
|
|
637
|
+
for part_index, part in enumerate(response_json):
|
|
638
|
+
try:
|
|
639
|
+
if isinstance(part, list) and len(part) > 2:
|
|
640
|
+
main_part = json.loads(part[2])
|
|
641
|
+
if main_part and len(main_part) > 4 and main_part[4]:
|
|
642
|
+
body = main_part
|
|
643
|
+
body_index = part_index
|
|
644
|
+
break
|
|
645
|
+
except (IndexError, TypeError, json.JSONDecodeError):
|
|
646
|
+
continue
|
|
647
|
+
|
|
648
|
+
if not body:
|
|
649
|
+
return self._error_response("Failed to parse response body. No valid data found.")
|
|
650
|
+
|
|
651
|
+
try:
|
|
652
|
+
content = ""
|
|
653
|
+
if len(body) > 4 and len(body[4]) > 0 and len(body[4][0]) > 1:
|
|
654
|
+
content = body[4][0][1][0] if len(body[4][0][1]) > 0 else ""
|
|
655
|
+
|
|
656
|
+
conversation_id = (
|
|
657
|
+
body[1][0] if len(body) > 1 and len(body[1]) > 0 else self.conversation_id
|
|
658
|
+
)
|
|
659
|
+
response_id = body[1][1] if len(body) > 1 and len(body[1]) > 1 else self.response_id
|
|
660
|
+
|
|
661
|
+
factualityQueries = body[3] if len(body) > 3 else None
|
|
662
|
+
textQuery = body[2][0] if len(body) > 2 and body[2] else ""
|
|
663
|
+
|
|
664
|
+
choices = []
|
|
665
|
+
if len(body) > 4:
|
|
666
|
+
for candidate in body[4]:
|
|
667
|
+
if (
|
|
668
|
+
len(candidate) > 1
|
|
669
|
+
and isinstance(candidate[1], list)
|
|
670
|
+
and len(candidate[1]) > 0
|
|
671
|
+
):
|
|
672
|
+
choices.append({"id": candidate[0], "content": candidate[1][0]})
|
|
673
|
+
|
|
674
|
+
choice_id = choices[0]["id"] if choices else self.choice_id
|
|
675
|
+
|
|
676
|
+
images = []
|
|
677
|
+
|
|
678
|
+
if len(body) > 4 and len(body[4]) > 0 and len(body[4][0]) > 4 and body[4][0][4]:
|
|
679
|
+
for img_data in body[4][0][4]:
|
|
680
|
+
try:
|
|
681
|
+
img_url = img_data[0][0][0]
|
|
682
|
+
img_alt = img_data[2] if len(img_data) > 2 else ""
|
|
683
|
+
img_title = img_data[1] if len(img_data) > 1 else "[Image]"
|
|
684
|
+
images.append({"url": img_url, "alt": img_alt, "title": img_title})
|
|
685
|
+
except (IndexError, TypeError):
|
|
686
|
+
console.log(
|
|
687
|
+
"[yellow]Warning: Could not parse image data structure (format 1).[/yellow]"
|
|
688
|
+
)
|
|
689
|
+
continue
|
|
690
|
+
|
|
691
|
+
generated_images = []
|
|
692
|
+
if len(body) > 4 and len(body[4]) > 0 and len(body[4][0]) > 12 and body[4][0][12]:
|
|
693
|
+
try:
|
|
694
|
+
if body[4][0][12][7] and body[4][0][12][7][0]:
|
|
695
|
+
for img_index, img_data in enumerate(body[4][0][12][7][0]):
|
|
696
|
+
try:
|
|
697
|
+
img_url = img_data[0][3][3]
|
|
698
|
+
img_title = f"[Generated Image {img_index + 1}]"
|
|
699
|
+
img_alt = (
|
|
700
|
+
img_data[3][5][0]
|
|
701
|
+
if len(img_data[3]) > 5 and len(img_data[3][5]) > 0
|
|
702
|
+
else ""
|
|
703
|
+
)
|
|
704
|
+
generated_images.append(
|
|
705
|
+
{"url": img_url, "alt": img_alt, "title": img_title}
|
|
706
|
+
)
|
|
707
|
+
except (IndexError, TypeError):
|
|
708
|
+
continue
|
|
709
|
+
|
|
710
|
+
if not generated_images:
|
|
711
|
+
for part_index, part in enumerate(response_json):
|
|
712
|
+
if part_index <= body_index:
|
|
713
|
+
continue
|
|
714
|
+
try:
|
|
715
|
+
img_part = json.loads(part[2])
|
|
716
|
+
if img_part[4][0][12][7][0]:
|
|
717
|
+
for img_index, img_data in enumerate(
|
|
718
|
+
img_part[4][0][12][7][0]
|
|
719
|
+
):
|
|
720
|
+
try:
|
|
721
|
+
img_url = img_data[0][3][3]
|
|
722
|
+
img_title = f"[Generated Image {img_index + 1}]"
|
|
723
|
+
img_alt = (
|
|
724
|
+
img_data[3][5][0]
|
|
725
|
+
if len(img_data[3]) > 5
|
|
726
|
+
and len(img_data[3][5]) > 0
|
|
727
|
+
else ""
|
|
728
|
+
)
|
|
729
|
+
generated_images.append(
|
|
730
|
+
{
|
|
731
|
+
"url": img_url,
|
|
732
|
+
"alt": img_alt,
|
|
733
|
+
"title": img_title,
|
|
734
|
+
}
|
|
735
|
+
)
|
|
736
|
+
except (IndexError, TypeError):
|
|
737
|
+
continue
|
|
738
|
+
break
|
|
739
|
+
except (IndexError, TypeError, json.JSONDecodeError):
|
|
740
|
+
continue
|
|
741
|
+
except (IndexError, TypeError):
|
|
742
|
+
pass
|
|
743
|
+
|
|
744
|
+
if len(generated_images) == 0 and len(body) > 4 and len(body[4]) > 0:
|
|
745
|
+
try:
|
|
746
|
+
candidate = body[4][0]
|
|
747
|
+
if len(candidate) > 22 and candidate[22]:
|
|
748
|
+
import re
|
|
749
|
+
|
|
750
|
+
content = (
|
|
751
|
+
candidate[22][0]
|
|
752
|
+
if isinstance(candidate[22], list) and len(candidate[22]) > 0
|
|
753
|
+
else str(candidate[22])
|
|
754
|
+
)
|
|
755
|
+
urls = re.findall(r"https?://[^\s]+", content)
|
|
756
|
+
for i, url in enumerate(urls):
|
|
757
|
+
if url[-1] in [".", ",", ")", "]", "}", '"', "'"]:
|
|
758
|
+
url = url[:-1]
|
|
759
|
+
generated_images.append(
|
|
760
|
+
{"url": url, "title": f"[Generated Image {i + 1}]", "alt": ""}
|
|
761
|
+
)
|
|
762
|
+
except (IndexError, TypeError) as e:
|
|
763
|
+
console.log(
|
|
764
|
+
f"[yellow]Warning: Could not parse alternative image structure: {e}[/yellow]"
|
|
765
|
+
)
|
|
766
|
+
|
|
767
|
+
if len(images) == 0 and len(generated_images) == 0 and content:
|
|
768
|
+
try:
|
|
769
|
+
import re
|
|
770
|
+
|
|
771
|
+
urls = re.findall(
|
|
772
|
+
r"(https?://[^\s]+\.(jpg|jpeg|png|gif|webp))", content.lower()
|
|
773
|
+
)
|
|
774
|
+
|
|
775
|
+
google_urls = re.findall(
|
|
776
|
+
r"(https?://lh\d+\.googleusercontent\.com/[^\s]+)", content
|
|
777
|
+
)
|
|
778
|
+
|
|
779
|
+
general_urls = re.findall(r"(https?://[^\s]+)", content)
|
|
780
|
+
|
|
781
|
+
all_urls = []
|
|
782
|
+
if urls:
|
|
783
|
+
all_urls.extend([url_tuple[0] for url_tuple in urls])
|
|
784
|
+
if google_urls:
|
|
785
|
+
all_urls.extend(google_urls)
|
|
786
|
+
|
|
787
|
+
if not all_urls and general_urls:
|
|
788
|
+
all_urls = general_urls
|
|
789
|
+
|
|
790
|
+
if all_urls:
|
|
791
|
+
for i, url in enumerate(all_urls):
|
|
792
|
+
if url[-1] in [".", ",", ")", "]", "}", '"', "'"]:
|
|
793
|
+
url = url[:-1]
|
|
794
|
+
images.append(
|
|
795
|
+
{"url": url, "title": f"[Image in Content {i + 1}]", "alt": ""}
|
|
796
|
+
)
|
|
797
|
+
console.log(
|
|
798
|
+
f"[green]Found {len(all_urls)} potential image URLs in content.[/green]"
|
|
799
|
+
)
|
|
800
|
+
except Exception as e:
|
|
801
|
+
console.log(
|
|
802
|
+
f"[yellow]Warning: Error extracting URLs from content: {e}[/yellow]"
|
|
803
|
+
)
|
|
804
|
+
|
|
805
|
+
all_images = images + generated_images
|
|
806
|
+
|
|
807
|
+
results: AskResponse = {
|
|
808
|
+
"content": content,
|
|
809
|
+
"conversation_id": conversation_id,
|
|
810
|
+
"response_id": response_id,
|
|
811
|
+
"factualityQueries": factualityQueries,
|
|
812
|
+
"textQuery": textQuery,
|
|
813
|
+
"choices": choices,
|
|
814
|
+
"images": all_images,
|
|
815
|
+
"error": False,
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
self.conversation_id = conversation_id
|
|
819
|
+
self.response_id = response_id
|
|
820
|
+
self.choice_id = choice_id
|
|
821
|
+
self._reqid += random.randint(1000, 9000)
|
|
822
|
+
|
|
823
|
+
return results
|
|
824
|
+
|
|
825
|
+
except (IndexError, TypeError) as e:
|
|
826
|
+
console.log(f"[red]Error extracting data from response: {e}[/red]")
|
|
827
|
+
return self._error_response(f"Error extracting data from response: {e}")
|
|
828
|
+
|
|
829
|
+
except json.JSONDecodeError as e:
|
|
830
|
+
console.log(f"[red]Error parsing JSON response: {e}[/red]")
|
|
831
|
+
resp_text = resp.text[:200] if resp else "No response"
|
|
832
|
+
return self._error_response(
|
|
833
|
+
f"Error parsing JSON response: {e}. Response: {resp_text}..."
|
|
834
|
+
)
|
|
835
|
+
except Timeout as e:
|
|
836
|
+
console.log(f"[red]Request timed out: {e}[/red]")
|
|
837
|
+
return self._error_response(f"Request timed out: {e}")
|
|
838
|
+
except HTTPError as e:
|
|
839
|
+
console.log(f"[red]HTTP error {e.response.status_code}: {e}[/red]")
|
|
840
|
+
return self._error_response(f"HTTP error {e.response.status_code}: {e}")
|
|
841
|
+
except (RequestException, CurlError) as e:
|
|
842
|
+
console.log(f"[red]Network error: {e}[/red]")
|
|
843
|
+
return self._error_response(f"Network error: {e}")
|
|
844
|
+
except Exception as e:
|
|
845
|
+
console.log(
|
|
846
|
+
f"[red]An unexpected error occurred during ask: {e}[/red]", style="bold red"
|
|
847
|
+
)
|
|
848
|
+
return self._error_response(f"An unexpected error occurred: {e}")
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
class Image(BaseModel):
|
|
852
|
+
"""
|
|
853
|
+
Represents a single image object returned from Gemini.
|
|
854
|
+
|
|
855
|
+
Attributes:
|
|
856
|
+
url (str): URL of the image.
|
|
857
|
+
title (str): Title of the image (default: "[Image]").
|
|
858
|
+
alt (str): Optional description of the image.
|
|
859
|
+
proxy (str | dict | None): Proxy used when saving the image.
|
|
860
|
+
impersonate (str): Browser profile for curl_cffi to impersonate.
|
|
861
|
+
"""
|
|
862
|
+
|
|
863
|
+
url: str
|
|
864
|
+
title: str = "[Image]"
|
|
865
|
+
alt: str = ""
|
|
866
|
+
proxy: Optional[Union[str, Dict[str, str]]] = None
|
|
867
|
+
impersonate: str = "chrome110"
|
|
868
|
+
|
|
869
|
+
def __str__(self) -> str:
|
|
870
|
+
return f"{self.title}({self.url}) - {self.alt}"
|
|
871
|
+
|
|
872
|
+
def __repr__(self) -> Any:
|
|
873
|
+
short_url = self.url if len(self.url) <= 50 else self.url[:20] + "..." + self.url[-20:]
|
|
874
|
+
short_alt = self.alt[:30] + "..." if len(self.alt) > 30 else self.alt
|
|
875
|
+
return f"Image(title='{self.title}', url='{short_url}', alt='{short_alt}')"
|
|
876
|
+
|
|
877
|
+
async def save(
|
|
878
|
+
self,
|
|
879
|
+
path: str = "downloaded_images",
|
|
880
|
+
filename: Optional[str] = None,
|
|
881
|
+
cookies: Optional[Dict[str, str]] = None,
|
|
882
|
+
verbose: bool = False,
|
|
883
|
+
skip_invalid_filename: bool = True,
|
|
884
|
+
) -> Optional[str]:
|
|
885
|
+
"""
|
|
886
|
+
Save the image to disk using curl_cffi.
|
|
887
|
+
Parameters:
|
|
888
|
+
path: str, optional
|
|
889
|
+
Directory to save the image (default "downloaded_images").
|
|
890
|
+
filename: str, optional
|
|
891
|
+
Filename to use; if not provided, inferred from URL.
|
|
892
|
+
cookies: dict, optional
|
|
893
|
+
Cookies used for the image request.
|
|
894
|
+
verbose: bool, optional
|
|
895
|
+
If True, outputs status messages (default False).
|
|
896
|
+
skip_invalid_filename: bool, optional
|
|
897
|
+
If True, skips saving if the filename is invalid.
|
|
898
|
+
Returns:
|
|
899
|
+
Absolute path of the saved image if successful; None if skipped.
|
|
900
|
+
Raises:
|
|
901
|
+
HTTPError if the network request fails.
|
|
902
|
+
RequestException/CurlError for other network errors.
|
|
903
|
+
IOError if file writing fails.
|
|
904
|
+
"""
|
|
905
|
+
if not filename:
|
|
906
|
+
try:
|
|
907
|
+
from urllib.parse import unquote, urlparse
|
|
908
|
+
|
|
909
|
+
parsed_url = urlparse(self.url)
|
|
910
|
+
base_filename = os.path.basename(unquote(parsed_url.path))
|
|
911
|
+
safe_filename = re.sub(r'[<>:"/\\|?*]', "_", base_filename)
|
|
912
|
+
if safe_filename and len(safe_filename) > 0:
|
|
913
|
+
filename = safe_filename
|
|
914
|
+
else:
|
|
915
|
+
filename = f"image_{random.randint(1000, 9999)}.jpg"
|
|
916
|
+
except Exception:
|
|
917
|
+
filename = f"image_{random.randint(1000, 9999)}.jpg"
|
|
918
|
+
|
|
919
|
+
try:
|
|
920
|
+
_ = Path(filename)
|
|
921
|
+
max_len = 255
|
|
922
|
+
if len(filename) > max_len:
|
|
923
|
+
name, ext = os.path.splitext(filename)
|
|
924
|
+
filename = name[: max_len - len(ext) - 1] + ext
|
|
925
|
+
except (OSError, ValueError):
|
|
926
|
+
if verbose:
|
|
927
|
+
console.log(f"[yellow]Invalid filename generated: {filename}[/yellow]")
|
|
928
|
+
if skip_invalid_filename:
|
|
929
|
+
if verbose:
|
|
930
|
+
console.log("[yellow]Skipping save due to invalid filename.[/yellow]")
|
|
931
|
+
return None
|
|
932
|
+
filename = f"image_{random.randint(1000, 9999)}.jpg"
|
|
933
|
+
if verbose:
|
|
934
|
+
console.log(f"[yellow]Using fallback filename: {filename}[/yellow]")
|
|
935
|
+
|
|
936
|
+
proxies_dict = None
|
|
937
|
+
if isinstance(self.proxy, str):
|
|
938
|
+
proxies_dict = {"http": self.proxy, "https": self.proxy}
|
|
939
|
+
elif isinstance(self.proxy, dict):
|
|
940
|
+
proxies_dict = self.proxy
|
|
941
|
+
|
|
942
|
+
dest = None
|
|
943
|
+
try:
|
|
944
|
+
async with AsyncSession(
|
|
945
|
+
cookies=cookies,
|
|
946
|
+
proxies=cast(Any, proxies_dict),
|
|
947
|
+
impersonate=cast(Any, self.impersonate),
|
|
948
|
+
) as client:
|
|
949
|
+
if verbose:
|
|
950
|
+
console.log(f"Attempting to download image from: {self.url}")
|
|
951
|
+
|
|
952
|
+
response = await client.get(self.url)
|
|
953
|
+
response.raise_for_status()
|
|
954
|
+
|
|
955
|
+
content_type = response.headers.get("content-type", "").lower()
|
|
956
|
+
if "image" not in content_type and verbose:
|
|
957
|
+
console.log(
|
|
958
|
+
f"[yellow]Warning: Content type is '{content_type}', not an image. Saving anyway.[/yellow]"
|
|
959
|
+
)
|
|
960
|
+
|
|
961
|
+
dest_path = Path(path)
|
|
962
|
+
dest_path.mkdir(parents=True, exist_ok=True)
|
|
963
|
+
dest = dest_path / filename
|
|
964
|
+
|
|
965
|
+
dest.write_bytes(response.content)
|
|
966
|
+
|
|
967
|
+
if verbose:
|
|
968
|
+
console.log(f"Image saved successfully as {dest.resolve()}")
|
|
969
|
+
|
|
970
|
+
return str(dest.resolve())
|
|
971
|
+
|
|
972
|
+
except HTTPError as e:
|
|
973
|
+
console.log(
|
|
974
|
+
f"[red]Error downloading image {self.url}: {e.response.status_code} {e}[/red]"
|
|
975
|
+
)
|
|
976
|
+
raise
|
|
977
|
+
except (RequestException, CurlError) as e:
|
|
978
|
+
console.log(f"[red]Network error downloading image {self.url}: {e}[/red]")
|
|
979
|
+
raise
|
|
980
|
+
except IOError as e:
|
|
981
|
+
console.log(f"[red]Error writing image file to {dest}: {e}[/red]")
|
|
982
|
+
raise
|
|
983
|
+
except Exception as e:
|
|
984
|
+
console.log(f"[red]An unexpected error occurred during image save: {e}[/red]")
|
|
985
|
+
raise
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
class WebImage(Image):
|
|
989
|
+
"""
|
|
990
|
+
Represents an image retrieved from web search results.
|
|
991
|
+
|
|
992
|
+
Returned when asking Gemini to "SEND an image of [something]".
|
|
993
|
+
"""
|
|
994
|
+
|
|
995
|
+
async def save(
|
|
996
|
+
self,
|
|
997
|
+
path: str = "downloaded_images",
|
|
998
|
+
filename: Optional[str] = None,
|
|
999
|
+
cookies: Optional[Dict[str, str]] = None,
|
|
1000
|
+
verbose: bool = False,
|
|
1001
|
+
skip_invalid_filename: bool = True,
|
|
1002
|
+
) -> Optional[str]:
|
|
1003
|
+
"""
|
|
1004
|
+
Save the image to disk using curl_cffi.
|
|
1005
|
+
Parameters:
|
|
1006
|
+
path: str, optional
|
|
1007
|
+
Directory to save the image (default "downloaded_images").
|
|
1008
|
+
filename: str, optional
|
|
1009
|
+
Filename to use; if not provided, inferred from URL.
|
|
1010
|
+
cookies: dict, optional
|
|
1011
|
+
Cookies used for the image request.
|
|
1012
|
+
verbose: bool, optional
|
|
1013
|
+
If True, outputs status messages (default False).
|
|
1014
|
+
skip_invalid_filename: bool, optional
|
|
1015
|
+
If True, skips saving if the filename is invalid.
|
|
1016
|
+
Returns:
|
|
1017
|
+
Absolute path of the saved image if successful; None if skipped.
|
|
1018
|
+
Raises:
|
|
1019
|
+
HTTPError if the network request fails.
|
|
1020
|
+
RequestException/CurlError for other network errors.
|
|
1021
|
+
IOError if file writing fails.
|
|
1022
|
+
"""
|
|
1023
|
+
return await super().save(path, filename, cookies, verbose, skip_invalid_filename)
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
class GeneratedImage(Image):
|
|
1027
|
+
"""
|
|
1028
|
+
Represents an image generated by Google's AI image generator (e.g., ImageFX).
|
|
1029
|
+
|
|
1030
|
+
Attributes:
|
|
1031
|
+
cookies (dict[str, str]): Cookies required for accessing the generated image URL,
|
|
1032
|
+
typically from the GeminiClient/Chatbot instance.
|
|
1033
|
+
"""
|
|
1034
|
+
|
|
1035
|
+
cookies: Dict[str, str]
|
|
1036
|
+
|
|
1037
|
+
@field_validator("cookies")
|
|
1038
|
+
@classmethod
|
|
1039
|
+
def validate_cookies(cls, v: Dict[str, str]) -> Dict[str, str]:
|
|
1040
|
+
"""Ensures cookies are provided for generated images."""
|
|
1041
|
+
if not v or not isinstance(v, dict):
|
|
1042
|
+
raise ValueError("GeneratedImage requires a dictionary of cookies from the client.")
|
|
1043
|
+
return v
|
|
1044
|
+
|
|
1045
|
+
async def save(
|
|
1046
|
+
self,
|
|
1047
|
+
path: str = "downloaded_images",
|
|
1048
|
+
filename: Optional[str] = None,
|
|
1049
|
+
cookies: Optional[Dict[str, str]] = None,
|
|
1050
|
+
verbose: bool = False,
|
|
1051
|
+
skip_invalid_filename: bool = True,
|
|
1052
|
+
**kwargs,
|
|
1053
|
+
) -> Optional[str]:
|
|
1054
|
+
"""
|
|
1055
|
+
Save the generated image to disk.
|
|
1056
|
+
Parameters:
|
|
1057
|
+
filename: str, optional
|
|
1058
|
+
Filename to use. If not provided, a default name including
|
|
1059
|
+
a timestamp and part of the URL is used. Generated images
|
|
1060
|
+
are often in .png or .jpg format.
|
|
1061
|
+
Additional arguments are passed to Image.save.
|
|
1062
|
+
Returns:
|
|
1063
|
+
Absolute path of the saved image if successful, None if skipped.
|
|
1064
|
+
"""
|
|
1065
|
+
if filename is None:
|
|
1066
|
+
ext = ".jpg" if ".jpg" in self.url.lower() else ".png"
|
|
1067
|
+
url_part = self.url.split("/")[-1][:10]
|
|
1068
|
+
filename = f"{datetime.now().strftime('%Y%m%d%H%M%S')}_{url_part}{ext}"
|
|
1069
|
+
|
|
1070
|
+
return await super().save(
|
|
1071
|
+
path, filename, cookies or self.cookies, verbose, skip_invalid_filename
|
|
1072
|
+
)
|