Spaces:
Sleeping
Sleeping
Download app.py from 3DOT60/website-scanner: direct link, hf CLI and curl.
- Browser
- Download file 34.7 kB
-
https://huggingface.co/spaces/3DOT60/website-scanner/resolve/main/app.py
- Command line
-
hf download hf://spaces/3DOT60/website-scanner/app.py
-
curl -L -o app.py https://huggingface.co/spaces/3DOT60/website-scanner/resolve/main/app.py
34.7 kB
| # -*- coding: utf-8 -*- | |
| """Website Scanner for Hugging Face Deployment""" | |
| import pandas as pd | |
| import openpyxl | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from urllib.parse import urlparse | |
| from openpyxl.worksheet.datavalidation import DataValidation | |
| from openpyxl.styles import Font, Alignment, PatternFill, Border, Side | |
| from openpyxl.utils.dataframe import dataframe_to_rows | |
| import time | |
| import logging | |
| from datetime import datetime | |
| import gradio as gr | |
| import tempfile | |
| import os | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(levelname)s - %(message)s' | |
| ) | |
| # Define modern color scheme | |
| COLORS = { | |
| "primary": "4361ee", | |
| "secondary": "3a0ca3", | |
| "accent": "f72585", | |
| "success": "4cc9f0", | |
| "warning": "f8961e", | |
| "danger": "ef233c", | |
| "dark": "212529", | |
| "light": "f8f9fa", | |
| "text": "495057", | |
| "text_light": "6c757d" | |
| } | |
| # Define scoring categories and recommendations with modern descriptions | |
| CATEGORIES = { | |
| "First Impressions & Branding": [ | |
| "π¨ Needs immediate attention: The website lacks professional design and clear messaging. Recommend a complete visual overhaul with a focus on brand identity and value proposition.", | |
| "β οΈ Requires improvement: The branding is unclear or inconsistent. Suggest updating visual elements, refining the value proposition, and adding trust signals like testimonials or certifications.", | |
| "π‘ Decent but could improve: The design is functional but lacks polish. Recommend refining the layout, typography, and visual hierarchy for better impact.", | |
| "β Good with minor tweaks: Strong branding foundation with only minor visual improvements needed, such as color consistency or image quality enhancements.", | |
| "π Excellent: Professional, memorable branding that effectively communicates value. No changes needed." | |
| ], | |
| "User Experience (UX)": [ | |
| "π¨ Needs immediate attention: The user journey is confusing with significant navigation issues. Recommend a complete UX redesign with user testing.", | |
| "β οΈ Requires improvement: Navigation and flow are inconsistent. Suggest restructuring information architecture and improving wayfinding elements.", | |
| "π‘ Decent but could improve: The site is usable but has friction points. Recommend usability testing to identify and fix pain points.", | |
| "β Good with minor tweaks: Overall good UX with only minor friction points that need refinement.", | |
| "π Excellent: Intuitive, seamless user experience that guides visitors effectively. No changes needed." | |
| ], | |
| "Performance & Speed": [ | |
| "π¨ Needs immediate attention: Extremely slow loading times. Recommend full optimization including hosting upgrade, image compression, and script optimization.", | |
| "β οΈ Requires improvement: Slow load times impacting user experience. Suggest compressing assets, optimizing code, and implementing caching.", | |
| "π‘ Decent but could improve: Acceptable speed but could benefit from lazy loading, CDN implementation, and further optimization.", | |
| "β Good with minor tweaks: Good performance with only small issues to address like unoptimized images or render-blocking resources.", | |
| "π Excellent: Lightning-fast performance with optimal loading times. No changes needed." | |
| ], | |
| "Mobile Responsiveness": [ | |
| "π¨ Needs immediate attention: Poor mobile experience with broken layouts. Recommend a responsive redesign with mobile-first approach.", | |
| "β οΈ Requires improvement: Major mobile usability issues. Need to redesign mobile layout and fix touch targets and readability.", | |
| "π‘ Decent but could improve: Responsive but with usability gaps. Suggest mobile-specific adjustments to typography and interactive elements.", | |
| "β Good with minor tweaks: Good responsiveness that could benefit from additional testing and refinement on various devices.", | |
| "π Excellent: Perfect mobile experience with flawless responsiveness across all devices. No changes needed." | |
| ], | |
| "SEO & Visibility": [ | |
| "π¨ Needs immediate attention: No SEO foundations present. Recommend implementing meta tags, sitemap, schema markup, and content strategy.", | |
| "β οΈ Requires improvement: Minimal SEO implementation. Need to improve on-page elements, metadata, and content optimization.", | |
| "π‘ Decent but could improve: Basic SEO setup in place. Recommend keyword optimization and content enhancements.", | |
| "β Good with minor tweaks: Good SEO foundation that could benefit from content strategy enhancements and technical SEO improvements.", | |
| "π Excellent: Comprehensive SEO implementation with all best practices followed. No changes needed." | |
| ], | |
| "Security & Compliance": [ | |
| "π¨ Needs immediate attention: Critical security issues found. Urgently need HTTPS, privacy policy, and software updates.", | |
| "β οΈ Requires improvement: Basic security in place but missing key protections. Recommend implementing security headers and compliance features.", | |
| "π‘ Decent but could improve: Secure but with outdated components. Suggest updating plugins/CMS and implementing additional security measures.", | |
| "β Good with minor tweaks: Good security posture with only minor improvements needed like security header refinements.", | |
| "π Excellent: Fully secure and compliant with all modern standards. No changes needed." | |
| ], | |
| "Accessibility": [ | |
| "π¨ Needs immediate attention: No accessibility features detected. Recommend WCAG audit and comprehensive compliance plan.", | |
| "β οΈ Requires improvement: Major accessibility issues found (contrast, keyboard nav). Need to implement core accessibility features.", | |
| "π‘ Decent but could improve: Some basic accessibility in place. Recommend screen reader testing and contrast improvements.", | |
| "β Good with minor tweaks: Mostly compliant with some refinements needed in ARIA labels and focus management.", | |
| "π Excellent: Fully WCAG compliant with excellent accessibility throughout. No changes needed." | |
| ], | |
| "Analytics & Conversions": [ | |
| "π¨ Needs immediate attention: No tracking implemented. Need to set up GA4, conversion goals, and CRM integration.", | |
| "β οΈ Requires improvement: Basic analytics only. Recommend implementing event tracking and conversion funnel setup.", | |
| "π‘ Decent but could improve: Some tracking in place. Suggest adding UTM parameters and enhanced funnel tracking.", | |
| "β Good with minor tweaks: Well-instrumented analytics that could benefit from dashboard creation and heatmap analysis.", | |
| "π Excellent: Comprehensive analytics implementation with full conversion optimization. No changes needed." | |
| ], | |
| "Content Quality": [ | |
| "π¨ Needs immediate attention: Content is sparse, poorly written, or irrelevant. Recommend complete content audit and rewrite with focus on user needs and SEO.", | |
| "β οΈ Requires improvement: Content exists but lacks depth or quality. Suggest adding more valuable information, improving readability, and better organization.", | |
| "π‘ Decent but could improve: Content is acceptable but could be more engaging and comprehensive. Recommend adding examples, case studies, or visual aids.", | |
| "β Good with minor tweaks: High-quality content that only needs minor refinements for better clarity or SEO optimization.", | |
| "π Excellent: Exceptional, well-written content that provides clear value. No changes needed." | |
| ] | |
| } | |
| def validate_url(url): | |
| """Validate and normalize the URL with enhanced checks""" | |
| if not url: | |
| raise ValueError("URL cannot be empty") | |
| if not url.startswith(('http://', 'https://')): | |
| url = 'https://' + url | |
| try: | |
| result = urlparse(url) | |
| if not all([result.scheme, result.netloc]): | |
| raise ValueError("Invalid URL structure") | |
| # Basic domain validation | |
| if '.' not in result.netloc: | |
| raise ValueError("Invalid domain format") | |
| return url | |
| except Exception as e: | |
| logging.error(f"URL validation failed: {str(e)}") | |
| raise ValueError(f"Invalid URL: {str(e)}") | |
| def calculate_dom_depth(soup): | |
| """Calculate the approximate depth of the DOM tree""" | |
| if not soup or not soup.find(): | |
| return 0 | |
| max_depth = 0 | |
| for tag in soup.find_all(): | |
| depth = 0 | |
| current = tag | |
| while current.parent and current.parent.name not in ['[document]', 'html']: | |
| depth += 1 | |
| current = current.parent | |
| if depth > max_depth: | |
| max_depth = depth | |
| return max_depth | |
| def scan_website(url): | |
| """Perform comprehensive website scan with enhanced features""" | |
| results = { | |
| 'basic': {}, | |
| 'meta': {}, | |
| 'resources': {}, | |
| 'performance': {}, | |
| 'security': {}, | |
| 'accessibility': {}, | |
| 'issues': [] | |
| } | |
| try: | |
| validated_url = validate_url(url) | |
| headers = { | |
| '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', | |
| 'Accept-Language': 'en-US,en;q=0.9' | |
| } | |
| # Start performance timer | |
| start_time = time.time() | |
| logging.info(f"Starting scan of: {validated_url}") | |
| try: | |
| # First request - get initial page with timeout and redirect handling | |
| session = requests.Session() | |
| session.max_redirects = 5 | |
| response = session.get( | |
| validated_url, | |
| headers=headers, | |
| timeout=20, | |
| allow_redirects=True, | |
| stream=True # For better performance with large pages | |
| ) | |
| response.raise_for_status() | |
| # Check content type | |
| content_type = response.headers.get('Content-Type', '') | |
| if 'text/html' not in content_type: | |
| raise ValueError(f"Unsupported content type: {content_type}") | |
| except requests.exceptions.RequestException as e: | |
| logging.error(f"Request failed: {str(e)}") | |
| results['issues'].append(f"Request failed: {str(e)}") | |
| return {'status': 'error', 'message': str(e), 'data': results} | |
| end_time = time.time() | |
| load_time = end_time - start_time | |
| # Parse content with lxml for better performance | |
| soup = BeautifulSoup(response.text, 'lxml') | |
| # Basic info | |
| results['basic'] = { | |
| 'url': validated_url, | |
| 'final_url': response.url, | |
| 'status_code': response.status_code, | |
| 'load_time': load_time, | |
| 'content_type': content_type, | |
| 'server': response.headers.get('Server', ''), | |
| 'redirect_count': len(response.history), | |
| 'scan_timestamp': datetime.now().isoformat() | |
| } | |
| # Enhanced meta tags analysis | |
| results['meta'] = { | |
| 'title': (soup.title.string if soup.title else None), | |
| 'title_length': (len(soup.title.string) if soup.title else 0), | |
| 'description': (soup.find('meta', attrs={'name': 'description'})['content'] | |
| if soup.find('meta', attrs={'name': 'description'}) else None), | |
| 'description_length': (len(soup.find('meta', attrs={'name': 'description'})['content']) | |
| if soup.find('meta', attrs={'name': 'description'}) else 0), | |
| 'viewport': bool(soup.find('meta', attrs={'name': 'viewport'})), | |
| 'charset': (soup.find('meta', attrs={'charset': True})['charset'] | |
| if soup.find('meta', attrs={'charset': True}) else None), | |
| 'canonical': (soup.find('link', rel='canonical')['href'] | |
| if soup.find('link', rel='canonical') else None), | |
| 'og_tags': {tag['property']: tag['content'] | |
| for tag in soup.find_all('meta', property=lambda x: x and x.startswith('og:'))} | |
| } | |
| # Enhanced favicon detection | |
| favicon = None | |
| for rel in ['icon', 'shortcut icon', 'apple-touch-icon']: | |
| favicon = soup.find('link', rel=lambda x: x and x.lower() == rel) | |
| if favicon: | |
| break | |
| results['meta']['has_favicon'] = bool(favicon) | |
| if favicon: | |
| results['meta']['favicon_url'] = favicon.get('href', '') | |
| results['meta']['favicon_type'] = favicon.get('type', '') | |
| results['meta']['favicon_sizes'] = favicon.get('sizes', '') | |
| # Enhanced security analysis | |
| results['security'] = { | |
| 'https': validated_url.startswith('https'), | |
| 'hsts': 'strict-transport-security' in response.headers, | |
| 'hsts_max_age': (response.headers.get('strict-transport-security', '') | |
| .split('max-age=')[-1].split(';')[0] if 'strict-transport-security' in response.headers else None), | |
| 'xss_protection': 'X-XSS-Protection' in response.headers, | |
| 'content_security_policy': 'Content-Security-Policy' in response.headers, | |
| 'x_frame_options': 'X-Frame-Options' in response.headers, | |
| 'content_type_options': 'X-Content-Type-Options' in response.headers, | |
| 'referrer_policy': 'Referrer-Policy' in response.headers | |
| } | |
| # Resource analysis with enhanced detection | |
| results['resources'] = { | |
| 'images': len(soup.find_all('img')), | |
| 'image_formats': list(set(img.get('src', '').split('.')[-1].lower() | |
| for img in soup.find_all('img') if img.get('src'))), | |
| 'scripts': len(soup.find_all('script')), | |
| 'inline_scripts': len([s for s in soup.find_all('script') if not s.get('src')]), | |
| 'stylesheets': len(soup.find_all('link', rel='stylesheet')), | |
| 'iframes': len(soup.find_all('iframe')), | |
| 'videos': len(soup.find_all('video')), | |
| 'audios': len(soup.find_all('audio')), | |
| 'embeds': len(soup.find_all('embed')), | |
| 'objects': len(soup.find_all('object')) | |
| } | |
| # Enhanced performance metrics | |
| results['performance'] = { | |
| 'page_size_kb': len(response.content) / 1024, | |
| 'requests': (results['resources']['images'] + | |
| results['resources']['scripts'] + | |
| results['resources']['stylesheets']), | |
| 'dom_elements': len(soup.find_all()), | |
| 'dom_depth': calculate_dom_depth(soup) | |
| } | |
| # Basic accessibility checks | |
| results['accessibility'] = { | |
| 'alt_text_images': len([img for img in soup.find_all('img') if img.get('alt')]), | |
| 'aria_attributes': len(soup.find_all(attrs={"aria-label": True})), | |
| 'lang_attribute': bool(soup.find('html').get('lang')), | |
| 'skip_links': len(soup.find_all('a', attrs={'href': lambda x: x and x.startswith('#')})) | |
| } | |
| logging.info("Scan completed successfully") | |
| return {'status': 'success', 'data': results} | |
| except Exception as e: | |
| logging.error(f"Scan error: {str(e)}", exc_info=True) | |
| results['issues'].append(f"Scan error: {str(e)}") | |
| return {'status': 'error', 'message': str(e), 'data': results} | |
| def auto_score_website(analysis_data, response_text=None): | |
| """Enhanced scoring system with weighted metrics""" | |
| scores = {} | |
| # Performance scoring (weighted) | |
| load_time = analysis_data['basic']['load_time'] | |
| page_size = analysis_data['performance']['page_size_kb'] | |
| requests = analysis_data['performance']['requests'] | |
| dom_depth = analysis_data['performance']['dom_depth'] | |
| perf_score = 0 | |
| if load_time < 1.5: | |
| perf_score += 4 | |
| elif load_time < 3: | |
| perf_score += 3 | |
| elif load_time < 5: | |
| perf_score += 2 | |
| elif load_time < 8: | |
| perf_score += 1 | |
| if page_size < 300: | |
| perf_score += 2 | |
| elif page_size < 800: | |
| perf_score += 1 | |
| if requests < 15: | |
| perf_score += 2 | |
| elif requests < 30: | |
| perf_score += 1 | |
| if dom_depth < 15: | |
| perf_score += 1 | |
| elif dom_depth > 30: | |
| perf_score -= 1 | |
| scores['Performance & Speed'] = min(max(1, perf_score // 2), 5) | |
| # Security scoring | |
| sec_score = 1 # Base score | |
| # HTTPS is critical | |
| if analysis_data['security']['https']: | |
| sec_score += 3 | |
| else: | |
| sec_score = 1 # Fail if no HTTPS | |
| # Other security headers | |
| if analysis_data['security']['hsts']: | |
| sec_score += 1 | |
| if analysis_data['security']['content_security_policy']: | |
| sec_score += 1 | |
| if analysis_data['security']['x_frame_options']: | |
| sec_score += 1 | |
| scores['Security & Compliance'] = min(sec_score, 5) | |
| # SEO scoring | |
| seo_score = 1 # Base score | |
| # Title check | |
| title = analysis_data['meta'].get('title') | |
| if title and 30 <= len(title) <= 60: | |
| seo_score += 2 | |
| # Description check | |
| description = analysis_data['meta'].get('description') | |
| if description and 50 <= len(description) <= 160: | |
| seo_score += 2 | |
| # Viewport is critical for mobile SEO | |
| if analysis_data['meta']['viewport']: | |
| seo_score += 1 | |
| # Canonical URL | |
| if analysis_data['meta'].get('canonical'): | |
| seo_score += 1 | |
| # OpenGraph tags | |
| if analysis_data['meta'].get('og_tags'): | |
| seo_score += 1 | |
| scores['SEO & Visibility'] = min(seo_score, 5) | |
| # Mobile responsiveness | |
| mobile_score = 1 | |
| if analysis_data['meta']['viewport']: | |
| mobile_score += 3 | |
| # Check for responsive design indicators | |
| if analysis_data['resources']['stylesheets'] > 0: | |
| mobile_score += 1 | |
| scores['Mobile Responsiveness'] = min(mobile_score, 5) | |
| # First impressions | |
| first_imp_score = 2 # Base score | |
| if analysis_data['meta']['has_favicon']: | |
| first_imp_score += 1 | |
| if title: | |
| first_imp_score += 1 | |
| if description: | |
| first_imp_score += 1 | |
| scores['First Impressions & Branding'] = min(first_imp_score, 5) | |
| # Content quality (basic assessment) | |
| content_score = 3 # Default | |
| # Check for meaningful content (only if response_text is provided) | |
| if response_text: | |
| soup = BeautifulSoup(response_text, 'lxml') | |
| text_content = ' '.join([p.get_text() for p in soup.find_all(['p', 'h1', 'h2', 'h3'])]) | |
| word_count = len(text_content.split()) | |
| if word_count > 500: | |
| content_score += 1 | |
| elif word_count < 100: | |
| content_score -= 1 | |
| scores['Content Quality'] = min(max(1, content_score), 5) | |
| # Accessibility | |
| accessibility_score = 1 # Base score | |
| # Image alt text | |
| total_images = analysis_data['resources']['images'] | |
| alt_images = analysis_data['accessibility']['alt_text_images'] | |
| if total_images > 0: | |
| alt_ratio = alt_images / total_images | |
| if alt_ratio > 0.9: | |
| accessibility_score += 2 | |
| elif alt_ratio > 0.5: | |
| accessibility_score += 1 | |
| # Language attribute | |
| if analysis_data['accessibility']['lang_attribute']: | |
| accessibility_score += 1 | |
| # ARIA attributes | |
| if analysis_data['accessibility']['aria_attributes'] > 0: | |
| accessibility_score += 1 | |
| scores['Accessibility'] = min(accessibility_score, 5) | |
| return scores | |
| def create_styled_spreadsheet(df, scan_data, url): | |
| """Create professionally styled Excel workbook with enhanced features""" | |
| wb = openpyxl.Workbook() | |
| # ===== Scorecard Sheet ===== | |
| ws = wb.active | |
| ws.title = "Scorecard" | |
| # Define modern styles - using only solid 6-digit hex colors | |
| header_fill = PatternFill( | |
| start_color=COLORS["primary"], | |
| end_color=COLORS["primary"], | |
| fill_type="solid" | |
| ) | |
| header_font = Font(color="FFFFFF", bold=True, size=12, name="Calibri") | |
| thin_border = Border( | |
| left=Side(style='thin', color=COLORS["text_light"]), | |
| right=Side(style='thin', color=COLORS["text_light"]), | |
| top=Side(style='thin', color=COLORS["text_light"]), | |
| bottom=Side(style='thin', color=COLORS["text_light"]) | |
| ) | |
| cell_font = Font(name='Calibri', size=11, color=COLORS["text"]) | |
| highlight_fill = PatternFill(start_color=COLORS["light"], end_color=COLORS["light"], fill_type="solid") | |
| # Write headers | |
| headers = ["Category", "Score", "Recommendation", "Priority", "Details"] | |
| ws.append(headers) | |
| # Style headers | |
| for col in range(1, len(headers) + 1): | |
| cell = ws.cell(row=1, column=col) | |
| cell.fill = header_fill | |
| cell.font = header_font | |
| cell.border = thin_border | |
| cell.alignment = Alignment(horizontal="center", vertical="center") | |
| # Conditional formatting for scores | |
| score_colors = { | |
| 1: COLORS["danger"], | |
| 2: COLORS["warning"], | |
| 3: COLORS["accent"], | |
| 4: COLORS["success"], | |
| 5: COLORS["primary"] | |
| } | |
| # Write data with enhanced formatting | |
| for idx, row in df.iterrows(): | |
| ws.append([ | |
| row['Category'], | |
| row['Score'], | |
| row['Recommendation'], | |
| "High" if row['Score'] in [1, 2] else "Medium" if row['Score'] == 3 else "Low", | |
| get_category_details(row['Category'], scan_data) | |
| ]) | |
| # Style all cells in row | |
| for col in range(1, len(headers) + 1): | |
| cell = ws.cell(row=idx+2, column=col) | |
| cell.font = cell_font | |
| cell.border = thin_border | |
| cell.alignment = Alignment(wrap_text=True, vertical="top") | |
| # Highlight high priority items | |
| if row['Score'] in [1, 2]: | |
| cell.fill = highlight_fill | |
| # Special score cell styling | |
| score_cell = ws.cell(row=idx+2, column=2) | |
| score_cell.fill = PatternFill( | |
| start_color=score_colors.get(row['Score'], "FFFFFF"), | |
| end_color=score_colors.get(row['Score'], "FFFFFF"), | |
| fill_type="solid" | |
| ) | |
| score_cell.font = Font(bold=True, color="FFFFFF" if row['Score'] in [1, 2, 5] else COLORS["text"]) | |
| score_cell.alignment = Alignment(horizontal="center") | |
| # Set column widths and row heights | |
| ws.column_dimensions['A'].width = 28 # Category | |
| ws.column_dimensions['B'].width = 8 # Score | |
| ws.column_dimensions['C'].width = 65 # Recommendation | |
| ws.column_dimensions['D'].width = 12 # Priority | |
| ws.column_dimensions['E'].width = 40 # Details | |
| for row in range(2, len(df) + 2): | |
| ws.row_dimensions[row].height = 60 # Increased row height | |
| # Freeze headers and add filter | |
| ws.freeze_panes = "A2" | |
| ws.auto_filter.ref = ws.dimensions | |
| # ===== Scan Details Sheet ===== | |
| ws2 = wb.create_sheet(title="Scan Details") | |
| # Section headers with enhanced organization | |
| sections = [ | |
| ("Basic Information", "basic"), | |
| ("Meta Tags Analysis", "meta"), | |
| ("Resources Breakdown", "resources"), | |
| ("Performance Metrics", "performance"), | |
| ("Security Headers", "security"), | |
| ("Accessibility Checks", "accessibility") | |
| ] | |
| row_num = 1 | |
| for section_name, section_key in sections: | |
| # Section header | |
| ws2.merge_cells(start_row=row_num, start_column=1, end_row=row_num, end_column=2) | |
| header_cell = ws2.cell(row=row_num, column=1, value=section_name) | |
| header_cell.font = Font(bold=True, size=14, color=COLORS["primary"]) | |
| header_cell.fill = PatternFill(start_color=COLORS["light"], end_color=COLORS["light"], fill_type="solid") | |
| row_num += 1 | |
| # Sub-headers | |
| ws2.cell(row=row_num, column=1, value="Metric").font = Font(bold=True) | |
| ws2.cell(row=row_num, column=2, value="Value").font = Font(bold=True) | |
| row_num += 1 | |
| # Data rows with conditional formatting | |
| if section_key in scan_data: | |
| for key, value in scan_data[section_key].items(): | |
| # Format key | |
| key_cell = ws2.cell(row=row_num, column=1, value=key.replace('_', ' ').title()) | |
| key_cell.font = Font(bold=False) | |
| # Format value based on content | |
| value_cell = ws2.cell(row=row_num, column=2, value=str(value)) | |
| # Special formatting for URLs | |
| if isinstance(value, str) and value.startswith(('http://', 'https://')): | |
| value_cell.hyperlink = value | |
| value_cell.font = Font(color=COLORS["primary"], underline="single") | |
| # Special formatting for booleans | |
| elif isinstance(value, bool): | |
| value_cell.font = Font(color=COLORS["success"] if value else COLORS["danger"]) | |
| value_cell.value = "β Yes" if value else "β No" | |
| row_num += 1 | |
| row_num += 1 # Add space between sections | |
| # Format details sheet | |
| ws2.column_dimensions['A'].width = 28 | |
| ws2.column_dimensions['B'].width = 50 | |
| # Freeze headers | |
| ws2.freeze_panes = "A2" | |
| # ===== Executive Summary Sheet ===== | |
| ws3 = wb.create_sheet(title="Summary") | |
| # Add logo placeholder (in real implementation, you could add an actual image) | |
| ws3.merge_cells(start_row=1, start_column=1, end_row=3, end_column=2) | |
| ws3.cell(row=1, column=1, value="Website Review Report").font = Font(bold=True, size=18, color=COLORS["primary"]) | |
| # Add summary metrics | |
| summary_metrics = [ | |
| ["Website URL", url], | |
| ["Scan Date", scan_data['basic']['scan_timestamp']], | |
| ["Overall Score", f"{df['Score'].mean():.1f}/5.0"], | |
| ["Page Title", scan_data['meta'].get('title', 'N/A')], | |
| ["Load Time", f"{scan_data['basic']['load_time']:.2f} seconds"], | |
| ["Page Size", f"{scan_data['performance']['page_size_kb']:.1f} KB"], | |
| ["Resource Requests", scan_data['performance']['requests']], | |
| ["Uses HTTPS", "β Yes" if scan_data['security']['https'] else "β No (Critical)"], | |
| ["Mobile Ready", "β Yes" if scan_data['meta']['viewport'] else "β No"], | |
| ["Critical Issues", len(df[df['Score'] == 1])], | |
| ["Areas Needing Improvement", len(df[df['Score'] == 2])], | |
| ["Well Performing Areas", len(df[df['Score'] >= 4])] | |
| ] | |
| start_row = 5 | |
| for i, (metric, value) in enumerate(summary_metrics, start=start_row): | |
| ws3.cell(row=i, column=1, value=metric).font = Font(bold=True) | |
| ws3.cell(row=i, column=2, value=value) | |
| # Highlight critical issues | |
| if metric == "Uses HTTPS" and value.startswith("β"): | |
| ws3.cell(row=i, column=2).font = Font(color=COLORS["danger"], bold=True) | |
| elif metric in ["Critical Issues", "Areas Needing Improvement"] and int(value) > 0: | |
| ws3.cell(row=i, column=2).font = Font(color=COLORS["warning"], bold=True) | |
| elif metric == "Well Performing Areas" and int(value) > 0: | |
| ws3.cell(row=i, column=2).font = Font(color=COLORS["success"], bold=True) | |
| # Add score distribution chart placeholder | |
| chart_row = start_row + len(summary_metrics) + 2 | |
| ws3.merge_cells(start_row=chart_row, start_column=1, end_row=chart_row, end_column=2) | |
| ws3.cell(row=chart_row, column=1, value="Score Distribution Chart").font = Font(italic=True, color=COLORS["text_light"]) | |
| # Add recommendations section | |
| rec_row = chart_row + 2 | |
| ws3.cell(row=rec_row, column=1, value="Top Recommendations").font = Font(bold=True, size=14, color=COLORS["primary"]) | |
| rec_row += 1 | |
| # Get top 3 recommendations (lowest scores) | |
| top_issues = df.nsmallest(3, 'Score') | |
| for idx, row in top_issues.iterrows(): | |
| ws3.cell(row=rec_row, column=1, value=f"β’ {row['Recommendation']}").font = Font(bold=True) | |
| ws3.cell(row=rec_row, column=1).alignment = Alignment(wrap_text=True) | |
| rec_row += 1 | |
| # Format summary sheet | |
| ws3.column_dimensions['A'].width = 25 | |
| ws3.column_dimensions['B'].width = 40 | |
| return wb | |
| def get_category_details(category, scan_data): | |
| """Generate detailed insights for each category""" | |
| details = [] | |
| if category == "Performance & Speed": | |
| details.append(f"β± Load Time: {scan_data['basic']['load_time']:.2f}s") | |
| details.append(f"π¦ Page Size: {scan_data['performance']['page_size_kb']:.1f}KB") | |
| details.append(f"π Requests: {scan_data['performance']['requests']}") | |
| details.append(f"π³ DOM Depth: {scan_data['performance']['dom_depth']}") | |
| elif category == "Security & Compliance": | |
| details.append(f"π HTTPS: {'β Yes' if scan_data['security']['https'] else 'β No'}") | |
| if scan_data['security']['https']: | |
| details.append(f"π‘ HSTS: {'β Yes' if scan_data['security']['hsts'] else 'β No'}") | |
| details.append(f"π‘ CSP Header: {'β Yes' if scan_data['security']['content_security_policy'] else 'β No'}") | |
| elif category == "SEO & Visibility": | |
| details.append(f"π· Title Length: {scan_data['meta']['title_length']} chars") | |
| details.append(f"π Description: {'β Present' if scan_data['meta']['description'] else 'β Missing'}") | |
| details.append(f"π± Viewport: {'β Present' if scan_data['meta']['viewport'] else 'β Missing'}") | |
| elif category == "Mobile Responsiveness": | |
| details.append(f"π± Viewport: {'β Present' if scan_data['meta']['viewport'] else 'β Missing'}") | |
| details.append(f"πΌ Images: {scan_data['resources']['images']}") | |
| details.append(f"π¨ Responsive CSS: {scan_data['resources']['stylesheets']} sheets") | |
| elif category == "First Impressions & Branding": | |
| details.append(f"π· Title: {'β Present' if scan_data['meta']['title'] else 'β Missing'}") | |
| details.append(f"πΌ Favicon: {'β Present' if scan_data['meta']['has_favicon'] else 'β Missing'}") | |
| elif category == "Accessibility": | |
| details.append(f"πΌ Alt Text: {scan_data['accessibility']['alt_text_images']}/{scan_data['resources']['images']} images") | |
| details.append(f"βΏ ARIA Attributes: {scan_data['accessibility']['aria_attributes']}") | |
| details.append(f"π Language: {'β Set' if scan_data['accessibility']['lang_attribute'] else 'β Missing'}") | |
| return " | ".join(details) | |
| def scan_and_generate_report(url): | |
| """Scan website and generate report with file handling for Hugging Face""" | |
| try: | |
| validated_url = validate_url(url) | |
| scan_result = scan_website(validated_url) | |
| if scan_result['status'] == 'error': | |
| return f"Scan failed: {scan_result['message']}", None | |
| scores = auto_score_website(scan_result['data']) | |
| results = [] | |
| for category, score in scores.items(): | |
| results.append({ | |
| 'Category': category, | |
| 'Score': score, | |
| 'Recommendation': CATEGORIES[category][score-1] | |
| }) | |
| df = pd.DataFrame(results) | |
| # Create Excel file in memory | |
| wb = create_styled_spreadsheet(df, scan_result['data'], validated_url) | |
| # Save to temporary file | |
| with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp: | |
| filename = tmp.name | |
| wb.save(filename) | |
| # Generate summary | |
| summary = [ | |
| f"## Website Scan Results for {validated_url}", | |
| f"**Scan Date:** {scan_result['data']['basic']['scan_timestamp']}", | |
| f"**Overall Score:** {df['Score'].mean():.1f}/5.0", | |
| ] | |
| # Add critical issues if any | |
| critical_issues = df[df['Score'] <= 2] | |
| if not critical_issues.empty: | |
| summary.append("\n### Critical Issues Found:") | |
| for _, row in critical_issues.iterrows(): | |
| summary.append(f"- **{row['Category']}** (Score: {row['Score']}/5): {row['Recommendation']}") | |
| return "\n".join(summary), filename | |
| except Exception as e: | |
| logging.error(f"Error during scan: {str(e)}", exc_info=True) | |
| return f"Error: {str(e)}", None | |
| def create_interface(): | |
| """Create Gradio interface for Hugging Face Spaces""" | |
| with gr.Blocks(title="Website Quality Scanner", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π Website Quality Scanner") | |
| gr.Markdown("Analyze website quality, performance, SEO, and security. Get a detailed report with recommendations.") | |
| with gr.Row(): | |
| url_input = gr.Textbox( | |
| label="Website URL", | |
| placeholder="https://example.com", | |
| max_lines=1, | |
| container=False | |
| ) | |
| scan_button = gr.Button("Scan Website", variant="primary") | |
| with gr.Row(): | |
| progress_bar = gr.Markdown(label="Scan Progress", visible=False) | |
| with gr.Row(): | |
| results_output = gr.Markdown(label="Scan Results") | |
| with gr.Row(): | |
| file_output = gr.File(label="Download Report", visible=False) | |
| def scan_website_wrapper(url): | |
| progress_bar.visible = True | |
| yield "Starting scan...", None, None | |
| try: | |
| # Validate URL first | |
| validated_url = validate_url(url) | |
| yield f"Scanning {validated_url}...", None, None | |
| # Perform scan | |
| summary, filename = scan_and_generate_report(url) | |
| if filename: | |
| yield summary, filename, gr.update(visible=True) | |
| else: | |
| yield summary, None, gr.update(visible=False) | |
| except Exception as e: | |
| yield f"Error: {str(e)}", None, gr.update(visible=False) | |
| finally: | |
| progress_bar.visible = False | |
| scan_button.click( | |
| fn=scan_website_wrapper, | |
| inputs=url_input, | |
| outputs=[results_output, file_output, file_output], | |
| show_progress="full" | |
| ) | |
| # Add examples | |
| gr.Examples( | |
| examples=[ | |
| ["https://google.com"], | |
| ["https://wikipedia.org"], | |
| ["https://github.com"] | |
| ], | |
| inputs=url_input, | |
| outputs=[results_output, file_output], | |
| fn=scan_and_generate_report, | |
| cache_examples=True, | |
| label="Try these examples:" | |
| ) | |
| return demo | |
| # Create and launch the interface | |
| if __name__ == "__main__": | |
| demo = create_interface() | |
| demo.launch(debug=True) |