From 4d4190b3c47df69920971da244a6a798b4c91307 Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Wed, 13 Nov 2024 16:16:44 +0200 Subject: feat(g4f/Provider/Blackbox.py): Improve validated token retrieval mechanism --- g4f/Provider/Blackbox.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/g4f/Provider/Blackbox.py b/g4f/Provider/Blackbox.py index 952507ff..3dc4236f 100644 --- a/g4f/Provider/Blackbox.py +++ b/g4f/Provider/Blackbox.py @@ -85,22 +85,38 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin): } @classmethod - async def fetch_validated(cls): + async def fetch_validated(cls): async with aiohttp.ClientSession() as session: try: - async with session.get('https://www.blackbox.ai/_next/static/chunks/2052-cdfeaea1ea292ff5.js') as response: + # Get the HTML of the page + async with session.get(cls.url) as response: + if response.status != 200: + print("Failed to load the page.") + return cls._last_validated_value + page_content = await response.text() - validated_match = re.search(r'w="([0-9a-fA-F-]{36})"', page_content) - - if validated_match: - validated_value = validated_match.group(1) - cls._last_validated_value = validated_value - return validated_value + # Find all JavaScript file links + js_files = re.findall(r'static/chunks/\d{4}-[a-fA-F0-9]+\.js', page_content) + + key_pattern = re.compile(r'w="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"') + + # Check each JavaScript file for the key + for js_file in js_files: + js_url = f"{cls.url}/_next/{js_file}" + async with session.get(js_url) as js_response: + if js_response.status == 200: + js_content = await js_response.text() + match = key_pattern.search(js_content) + if match: + validated_value = match.group(1) + cls._last_validated_value = validated_value + return validated_value except Exception as e: print(f"Error fetching validated value: {e}") return cls._last_validated_value + @staticmethod def generate_id(length=7): characters = string.ascii_letters + string.digits -- cgit v1.2.3