Skip to content

Commit df08275

Browse files
committed
### refactor(g4f/Provider/Blackbox.py): Optimize fetch_validated method
1 parent 4d4190b commit df08275

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

g4f/Provider/Blackbox.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
3535
"gemini-1.5-flash": {'mode': True, 'id': 'Gemini'},
3636
"llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"},
3737
'llama-3.1-70b': {'mode': True, 'id': "llama-3.1-70b"},
38-
'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405"},
38+
'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405"},
3939
#
4040
'Python Agent': {'mode': True, 'id': "Python Agent"},
4141
'Java Agent': {'mode': True, 'id': "Java Agent"},
@@ -86,21 +86,23 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
8686

8787
@classmethod
8888
async def fetch_validated(cls):
89+
# Якщо ключ вже збережений в пам'яті, повертаємо його
90+
if cls._last_validated_value:
91+
return cls._last_validated_value
92+
93+
# Якщо ключ не знайдено, виконуємо пошук
8994
async with aiohttp.ClientSession() as session:
9095
try:
91-
# Get the HTML of the page
9296
async with session.get(cls.url) as response:
9397
if response.status != 200:
9498
print("Failed to load the page.")
9599
return cls._last_validated_value
96100

97101
page_content = await response.text()
98-
# Find all JavaScript file links
99102
js_files = re.findall(r'static/chunks/\d{4}-[a-fA-F0-9]+\.js', page_content)
100103

101104
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})"')
102105

103-
# Check each JavaScript file for the key
104106
for js_file in js_files:
105107
js_url = f"{cls.url}/_next/{js_file}"
106108
async with session.get(js_url) as js_response:
@@ -109,7 +111,7 @@ async def fetch_validated(cls):
109111
match = key_pattern.search(js_content)
110112
if match:
111113
validated_value = match.group(1)
112-
cls._last_validated_value = validated_value
114+
cls._last_validated_value = validated_value # Зберігаємо в пам'яті
113115
return validated_value
114116
except Exception as e:
115117
print(f"Error fetching validated value: {e}")

0 commit comments

Comments
 (0)