update
Browse files472 unmodified lines47347447547647747847948048148248348448548685 unmodified lines57257357457557657725 unmodified lines603604605606607608609472 unmodified linesif not parsed_url.netloc:return "Error: Invalid URL provided"# Set headers to mimic a browser requestheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}# Make the requestresponse = requests.get(url, headers=headers, timeout=10)response.raise_for_status()# Parse HTML contentsoup = BeautifulSoup(response.content, 'html.parser')85 unmodified linesreturn website_content.strip()except requests.exceptions.RequestException as e:return f"Error accessing website: {str(e)}"except Exception as e:25 unmodified lineswebsite_text = website_text[:8000] # Limit to 8000 chars for prompt sizequery = f"{query}\n\n[Website content to redesign below]\n{website_text}"elif website_text.startswith("Error"):query = f"{query}\n\n[Error extracting website: {website_text}]"# Enhance query with search if enabledenhanced_query = enhance_query_with_search(query, enable_search)472 unmodified lines47347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151285 unmodified lines59859960060160260360460560660760860961061161261361461561625 unmodified lines642643644645646647648649650651652653654655656657658472 unmodified linesif not parsed_url.netloc:return "Error: Invalid URL provided"# Set comprehensive headers to mimic a real browser requestheaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8','Accept-Language': 'en-US,en;q=0.9','Accept-Encoding': 'gzip, deflate, br','DNT': '1','Connection': 'keep-alive','Upgrade-Insecure-Requests': '1','Sec-Fetch-Dest': 'document','Sec-Fetch-Mode': 'navigate','Sec-Fetch-Site': 'none','Sec-Fetch-User': '?1','Cache-Control': 'max-age=0'}# Create a session to maintain cookies and handle redirectssession = requests.Session()session.headers.update(headers)# Make the request with retry logicmax_retries = 3for attempt in range(max_retries):try:response = session.get(url, timeout=15, allow_redirects=True)response.raise_for_status()breakexcept requests.exceptions.HTTPError as e:if e.response.status_code == 403 and attempt < max_retries - 1:# Try with different User-Agent on 403session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'continueelse:raise# Parse HTML contentsoup = BeautifulSoup(response.content, 'html.parser')85 unmodified linesreturn website_content.strip()except requests.exceptions.HTTPError as e:if e.response.status_code == 403:return f"Error: Website blocked access (403 Forbidden). This website may have anti-bot protection. Try a different website or provide a description of what you want to build instead."elif e.response.status_code == 404:return f"Error: Website not found (404). Please check the URL and try again."elif e.response.status_code >= 500:return f"Error: Website server error ({e.response.status_code}). Please try again later."else:return f"Error accessing website: HTTP {e.response.status_code} - {str(e)}"except requests.exceptions.Timeout:return "Error: Request timed out. The website may be slow or unavailable."except requests.exceptions.ConnectionError:return "Error: Could not connect to the website. Please check your internet connection and the URL."except requests.exceptions.RequestException as e:return f"Error accessing website: {str(e)}"except Exception as e:25 unmodified lineswebsite_text = website_text[:8000] # Limit to 8000 chars for prompt sizequery = f"{query}\n\n[Website content to redesign below]\n{website_text}"elif website_text.startswith("Error"):# Provide helpful guidance when website extraction failsfallback_guidance = """Since I couldn't extract the website content, please provide additional details about what you'd like to build:1. What type of website is this? (e.g., e-commerce, blog, portfolio, dashboard)2. What are the main features you want?3. What's the target audience?4. Any specific design preferences? (colors, style, layout)This will help me create a better design for you."""query = f"{query}\n\n[Error extracting website: {website_text}]{fallback_guidance}"# Enhance query with search if enabledenhanced_query = enhance_query_with_search(query, enable_search)
app.py+55-6