Improve on the controller, flesh it out as much as possible without it being buggy
- User
Improve on the controller, flesh it out as much as possible without it being buggy
Assistant
Here's a significantly enhanced and more robust version of
browser_control/controller.py:""" browser_control/controller.py Advanced browser automation controller for Instagram interactions with comprehensive multi-account management, anti-detection measures, and enterprise-grade reliability. """ import os import time import random import logging import json import pickle import threading from datetime import datetime, timedelta from typing import Dict, List, Optional, Tuple, Any from dataclasses import dataclass from enum import Enum from contextlib import contextmanager import hashlib import sqlite3 from pathlib import Path from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ( NoSuchElementException, TimeoutException, StaleElementReferenceException, WebDriverException, ElementClickInterceptedException, ElementNotInteractableException ) from selenium.webdriver.remote.webelement import WebElement # Configure comprehensive logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - [%(funcName)s:%(lineno)d] - %(message)s', handlers=[ logging.FileHandler('data/logs/controller.log'), logging.StreamHandler() ] ) logger = logging.getLogger("InstagramController") class ActionType(Enum): """Enumeration of Instagram actions for rate limiting""" LOGIN = "login" POST = "post" COMMENT = "comment" LIKE = "like" FOLLOW = "follow" UNFOLLOW = "unfollow" STORY = "story" DM = "dm" METRICS = "metrics" @dataclass class AccountMetrics: """Data structure for Instagram account metrics""" username: str followers: int following: int posts: int engagement_rate: float avg_likes: int avg_comments: int story_views: int profile_visits: int reach: int impressions: int timestamp: datetime def to_dict(self) -> Dict[str, Any]: return { 'username': self.username, 'followers': self.followers, 'following': self.following, 'posts': self.posts, 'engagement_rate': self.engagement_rate, 'avg_likes': self.avg_likes, 'avg_comments': self.avg_comments, 'story_views': self.story_views, 'profile_visits': self.profile_visits, 'reach': self.reach, 'impressions': self.impressions, 'timestamp': self.timestamp.isoformat() } @dataclass class ActionResult: """Result of an Instagram action""" success: bool action_type: ActionType username: str message: str data: Optional[Dict[str, Any]] = None timestamp: datetime = None def __post_init__(self): if self.timestamp is None: self.timestamp = datetime.now() class RateLimitManager: """Advanced rate limiting with per-action type limits""" def __init__(self, config: Dict[str, Any]): self.config = config self.action_history: Dict[str, List[Tuple[ActionType, datetime]]] = {} self.daily_limits = { ActionType.POST: 10, ActionType.COMMENT: 50, ActionType.LIKE: 200, ActionType.FOLLOW: 30, ActionType.UNFOLLOW: 30, ActionType.STORY: 15, ActionType.DM: 20, ActionType.METRICS: 100 } self.hourly_limits = { ActionType.POST: 3, ActionType.COMMENT: 15, ActionType.LIKE: 60, ActionType.FOLLOW: 10, ActionType.UNFOLLOW: 10, ActionType.STORY: 5, ActionType.DM: 8, ActionType.METRICS: 20 } self._lock = threading.Lock() def can_perform_action(self, username: str, action_type: ActionType) -> bool: """Check if action is within rate limits""" with self._lock: now = datetime.now() if username not in self.action_history: self.action_history[username] = [] # Clean old actions (older than 24 hours) self.action_history[username] = [ (action, timestamp) for action, timestamp in self.action_history[username] if now - timestamp < timedelta(hours=24) ] # Count recent actions daily_count = sum(1 for action, _ in self.action_history[username] if action == action_type) hourly_count = sum( 1 for action, timestamp in self.action_history[username] if action == action_type and now - timestamp < timedelta(hours=1) ) return (daily_count < self.daily_limits[action_type] and hourly_count < self.hourly_limits[action_type]) def record_action(self, username: str, action_type: ActionType): """Record an action for rate limiting""" with self._lock: if username not in self.action_history: self.action_history[username] = [] self.action_history[username].append((action_type, datetime.now())) class SessionManager: """Manages browser sessions with persistence and recovery""" def __init__(self, data_dir: str = "data/sessions"): self.data_dir = Path(data_dir) self.data_dir.mkdir(parents=True, exist_ok=True) def save_session(self, username: str, cookies: List[Dict], user_agent: str): """Save session data for persistence""" session_data = { 'cookies': cookies, 'user_agent': user_agent, 'timestamp': datetime.now().isoformat() } session_file = self.data_dir / f"{username}_session.pkl" with open(session_file, 'wb') as f: pickle.dump(session_data, f) logger.info(f"Session saved for {username}") def load_session(self, username: str) -> Optional[Dict]: """Load saved session data""" session_file = self.data_dir / f"{username}_session.pkl" if not session_file.exists(): return None try: with open(session_file, 'rb') as f: session_data = pickle.load(f) # Check if session is not too old (7 days) session_time = datetime.fromisoformat(session_data['timestamp']) if datetime.now() - session_time > timedelta(days=7): session_file.unlink() # Delete old session return None return session_data except Exception as e: logger.error(f"Failed to load session for {username}: {e}") return None class HumanBehaviorSimulator: """Simulates human-like behavior patterns""" @staticmethod def typing_pattern(text: str, element: WebElement, base_speed: float = 0.1): """Simulate human typing with realistic patterns""" words = text.split(' ') for i, word in enumerate(words): for j, char in enumerate(word): element.send_keys(char) # Vary typing speed based on character complexity if char.isalpha(): delay = random.uniform(base_speed * 0.8, base_speed * 1.2) elif char.isdigit(): delay = random.uniform(base_speed * 1.2, base_speed * 1.5) else: delay = random.uniform(base_speed * 1.5, base_speed * 2.0) # Occasional longer pauses (thinking) if random.random() < 0.05: delay *= random.uniform(3, 8) time.sleep(delay) # Space between words if i < len(words) - 1: element.send_keys(' ') time.sleep(random.uniform(base_speed * 2, base_speed * 4)) @staticmethod def mouse_movement(driver: webdriver.Chrome, element: WebElement): """Simulate natural mouse movement to element""" actions = ActionChains(driver) # Get element location location = element.location size = element.size # Calculate random point within element x_offset = random.randint(5, size['width'] - 5) y_offset = random.randint(5, size['height'] - 5) # Move to element with slight randomness actions.move_to_element_with_offset(element, x_offset, y_offset) actions.pause(random.uniform(0.1, 0.3)) actions.perform() @staticmethod def scroll_behavior(driver: webdriver.Chrome, direction: str = "down", distance: int = None): """Simulate natural scrolling behavior""" if distance is None: distance = random.randint(200, 800) scroll_steps = random.randint(3, 8) step_size = distance // scroll_steps for _ in range(scroll_steps): if direction == "down": driver.execute_script(f"window.scrollBy(0, {step_size});") else: driver.execute_script(f"window.scrollBy(0, -{step_size});") time.sleep(random.uniform(0.1, 0.3)) class InstagramController: """Advanced Instagram automation controller with enterprise features""" def __init__(self, config: Dict[str, Any], accounts: Dict[str, str]): self.config = config self.accounts = accounts self.active_drivers: Dict[str, webdriver.Chrome] = {} self.session_manager = SessionManager() self.rate_limiter = RateLimitManager(config) self.behavior_sim = HumanBehaviorSimulator() self.action_history: List[ActionResult] = [] self._setup_database() self._setup_driver_options() # Thread safety self._driver_lock = threading.Lock() self._action_lock = threading.Lock() # Performance monitoring self.performance_metrics = { 'actions_performed': 0, 'success_rate': 0.0, 'avg_action_time': 0.0, 'errors_count': 0 } def _setup_database(self): """Initialize SQLite database for persistent storage""" db_path = Path("data/controller.db") db_path.parent.mkdir(parents=True, exist_ok=True) self.db_conn = sqlite3.connect(str(db_path), check_same_thread=False) self.db_conn.execute(''' CREATE TABLE IF NOT EXISTS action_history ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, action_type TEXT, success BOOLEAN, message TEXT, data TEXT, timestamp DATETIME ) ''') self.db_conn.execute(''' CREATE TABLE IF NOT EXISTS metrics_history ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, followers INTEGER, following INTEGER, posts INTEGER, engagement_rate REAL, timestamp DATETIME ) ''') self.db_conn.commit() def _setup_driver_options(self): """Configure advanced Chrome options for maximum stealth""" self.chrome_options = Options() # Basic stealth options if self.config['browser_settings']['headless']: self.chrome_options.add_argument("--headless=new") # Advanced anti-detection self.chrome_options.add_argument("--disable-blink-features=AutomationControlled") self.chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) self.chrome_options.add_experimental_option('useAutomationExtension', False) # Performance and stability self.chrome_options.add_argument("--no-sandbox") self.chrome_options.add_argument("--disable-dev-shm-usage") self.chrome_options.add_argument("--disable-gpu") self.chrome_options.add_argument("--disable-extensions") self.chrome_options.add_argument("--disable-plugins") self.chrome_options.add_argument("--disable-images") # Faster loading self.chrome_options.add_argument("--mute-audio") # Window configuration self.chrome_options.add_argument(f"--window-size={self.config['browser_settings']['window_size']}") self.chrome_options.add_argument(f"--user-agent={self.config['browser_settings']['user_agent']}") # Memory optimization self.chrome_options.add_argument("--memory-pressure-off") self.chrome_options.add_argument("--max_old_space_size=4096") # Proxy configuration if proxy := self.config['browser_settings'].get('proxy'): self.chrome_options.add_argument(f"--proxy-server={proxy}") def initialize_driver(self, username: str) -> bool: """Initialize WebDriver with advanced configuration""" with self._driver_lock: try: # Create driver instance service = Service() driver = webdriver.Chrome(service=service, options=self.chrome_options) # Execute stealth scripts driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.execute_script("Object.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5]})") driver.execute_script("Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en']})") # Set timeouts driver.implicitly_wait(10) driver.set_page_load_timeout(30) # Store driver self.active_drivers[username] = driver logger.info(f"Driver initialized successfully for {username}") return True except Exception as e: logger.error(f"Driver initialization failed for {username}: {e}") return False @contextmanager def safe_driver_operation(self, username: str): """Context manager for safe driver operations""" driver = self.active_drivers.get(username) if not driver: raise ValueError(f"No active driver for {username}") try: yield driver except Exception as e: logger.error(f"Driver operation failed for {username}: {e}") self._capture_screenshot(driver, f"error_{username}") raise def login(self, username: str, password: str) -> ActionResult: """Advanced login with session recovery and 2FA handling""" start_time = time.time() # Check rate limits if not self.rate_limiter.can_perform_action(username, ActionType.LOGIN): return ActionResult(False, ActionType.LOGIN, username, "Rate limit exceeded") # Try to restore session first session_data = self.session_manager.load_session(username) if not self.initialize_driver(username):