import sys from typing import List, Dict, Any class ConsoleView: def display_message(self, message: str): try: print(message) except UnicodeEncodeError: print(message.encode('utf-8', errors='replace').decode('utf-8')) def display_error(self, error: str): try: print(f"[ERROR] {error}") except UnicodeEncodeError: print(f"[ERROR] {error.encode('utf-8', errors='replace').decode('utf-8')}") def display_success(self, message: str): try: print(f"[SUCCESS] {message}") except UnicodeEncodeError: print(f"[SUCCESS] {message.encode('utf-8', errors='replace').decode('utf-8')}") def display_strategies(self, strategies: List[Dict[str, str]]): print("\n=== Available Scrapers ===") for idx, strategy in enumerate(strategies, 1): print(f"{idx}. {strategy['name']}") print(f" Source: {strategy['source']}") print() def display_scraped_data(self, data: Any, saved_path: str = None): if hasattr(data, 'to_dict'): data = data.to_dict() print("\n=== Scraping Results ===") print(f"Source: {data.get('source', 'N/A')}") print(f"Strategy: {data.get('strategy_name', 'N/A')}") print(f"Total Items: {data.get('total_items', len(data.get('items', [])))}") print(f"Scraped At: {data.get('scraped_at', 'N/A')}") if saved_path: print(f"Saved To: {saved_path}") print("\n--- Items Preview ---") items = data.get('items', []) for idx, item in enumerate(items[:5], 1): try: title = item.get('title', 'N/A') print(f"{idx}. {title}") except UnicodeEncodeError: print(f"{idx}. {item.get('title', 'N/A').encode('utf-8', errors='replace').decode('utf-8')}") if item.get('content'): content = item.get('content', '') try: truncated = content[:80] + "..." if len(content) > 80 else content print(f" {truncated}") except UnicodeEncodeError: truncated = content[:80].encode('utf-8', errors='replace').decode('utf-8') print(f" {truncated}") print() if len(items) > 5: print(f"... and {len(items) - 5} more items") def display_list(self, items: List[Any]): for item in items: print(item)