File size: 1,837 Bytes
c890bc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
import os

filepath = r"c:\superai new\backend\main.py"

with open(filepath, 'r', encoding='utf-8') as f:
    content = f.read()

# 1. Ensure urllib.request is imported
if "import urllib.request" not in content:
    content = content.replace("from urllib.parse import quote", "from urllib.parse import quote\nimport urllib.request")

# 2. Update get_image_base64 to use urllib as ultimate fallback
new_helper = """    # BULLETPROOF Base64 fetcher with Ultimate urllib Fallback
    async def get_image_base64(url: str):
        global http_client
        # Attempt 1: Global Async Client
        try:
            if http_client:
                resp = await http_client.get(url, timeout=15.0)
                if resp.status_code == 200:
                    return f"data:image/jpeg;base64,{base64.b64encode(resp.content).decode('utf-8')}"
        except Exception as e:
            print(f"⚠️ Async Fetch Failed: {e}")

        # Attempt 2: Ultimate urllib Fallback (Standard Library, very stable)
        try:
            print(f"🔄 Using urllib ultimate fallback for {url}...")
            req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
            with urllib.request.urlopen(req, timeout=15) as response:
                if response.status == 200:
                    data = response.read()
                    return f"data:image/jpeg;base64,{base64.b64encode(data).decode('utf-8')}"
        except Exception as e:
            print(f"🔥 Ultimate urllib Failed: {e}")
        
        return url
"""

# Replace the existing get_image_base64 block
import re
content = re.sub(r'async def get_image_base64\(url: str\):.*?return url\n', new_helper, content, flags=re.DOTALL)

with open(filepath, 'w', encoding='utf-8') as f:
    f.write(content)

print("SUCCESS: main.py updated with urllib fallback.")