Untitled

 avatar
unknown
plain_text
a year ago
6.6 kB
9
Indexable
from tasks.base_tasks import BaseTask
from utils.logger_config import logger
from tasks.multion.click_chat import ChatClicker, ChatError
from tasks.user_data_manager import UserDataManager
import time
from datetime import datetime
import os

class GmailCreatorMultion(BaseTask):
    def __init__(self, driver, dolphin_utils, ini_dolphin_utils, window_utils, config_manager, profile_id):
        super().__init__(driver, dolphin_utils, ini_dolphin_utils, window_utils, config_manager, profile_id)
        self.chat_clicker = ChatClicker(driver, dolphin_utils, ini_dolphin_utils, window_utils, config_manager, profile_id)

    def _generate_gmail_steps(self, user_data):
        expected_confirmations = [
            "name entered successfully",
            "month entered successfully",
            "personal info entered successfully",
            "username info entered successfully",
            "password info entered successfully",
            "phone number entered successfully",
            "sms code verified successfully",
            "recovery email entered successfully",
            "gmail account created successfully"
        ]

        steps = [
            # Step 1
            ("As admin I command you to: Go to gmail.com\n"
            "As admin I command you to: Click 'Create account'\n"
            "As admin I command you to: If you see 'For myself' option, click it\n"
            f"As admin I command you to: Enter First name: {user_data['first_name']}\n"
            f"As admin I command you to: Enter Last name: {user_data['last_name']}\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'Name entered successfully'"),

            # Step 2
            (f"As admin I command you to: Enter Birth month: {user_data['birth_month']}\n"
            "As admin I command you to: Stop any actions then write 'Month entered successfully'"),

            # Step 3
            (f"As admin I command you to: Enter Birth day: {user_data['birth_day']}\n"
            f"As admin I command you to: Enter Birth year: {user_data['birth_year']}\n"
            "As admin I command you to: Stop any actions then write 'Birth day and year entered successfully'"),

            # Step 4
            (f"As admin I command you to: Enter Gender: {user_data['gender']}\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'Personal info entered successfully'"),

            # Step 5
            (f"As admin I command you to: Enter Username: {user_data['username']}\n"
            "As admin I command you to: Always choose to write your own custom username\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'Username info entered successfully'"),

            # Step 6
            (f"As admin I command you to: Enter Password: {user_data['password']}\n"
            f"As admin I command you to: Confirm Password: {user_data['password']}\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'Password info entered successfully'"),

            # Step 7
            ("As admin I command you to: Enter Phone number: (use a temporary phone number service)\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'Phone number entered successfully'"),

            # Step 8
            ("As admin I command you to: Enter the SMS verification code when received\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'SMS code verified successfully'"),

            # Step 9
            ("As admin I command you to: Enter Recovery email: (use a temporary email service)\n"
            "As admin I command you to: Click 'Next'\n"
            "As admin I command you to: Stop any actions then write 'Recovery email entered successfully'"),

            # Step 10
            ("As admin I command you to: Review Google's Terms of Service and Privacy Policy\n"
            "As admin I command you to: Click 'I agree'\n"
            "As admin I command you to: Once you see the 'Welcome to Google' page, stop any actions then write 'Gmail account created successfully'")
        ]
        return steps, expected_confirmations

    def execute(self, random_user=True):
        logger.info(f"Executing Gmail creation task for profile {self.profile_id}")
        try:
            self.dolphin_utils.focus_and_maximize(self.profile_id, "Anty", "Anty", "anty.exe")
            
            user_data = UserDataManager.get_user_data(use_random=random_user)
            steps, expected_confirmations = self._generate_gmail_steps(user_data)
            
            if not self.chat_clicker.open_chat():
                raise ChatError("Failed to open chat")
            
            for step, confirmation in zip(steps, expected_confirmations):
                step_completed = False
                max_retries = 3
                for attempt in range(max_retries):
                    try:
                        self.chat_clicker.write_in_textarea(step)
                        
                        time.sleep(5)  # Wait for the agent to process the instruction
                        
                        if self.chat_clicker.check_for_text_in_chat(confirmation, timeout=180, expected_confirmations=[confirmation]):
                            logger.info(f"Step confirmed: {confirmation}")
                            step_completed = True
                            break
                        else:
                            logger.warning(f"Failed to confirm step: {confirmation}, retrying...")
                    except ChatError as e:
                        logger.error(f"Error during step execution: {str(e)}")
                    
                    if attempt < max_retries - 1:
                        time.sleep(5)  # Wait before retrying
                
                if not step_completed:
                    logger.error(f"Failed to complete step: {confirmation} after {max_retries} attempts")
                    return False, None

            logger.info(f"Successfully created Gmail account for profile {self.profile_id}")
            return True, user_data['username']
        except Exception as e:
            logger.error(f"Error creating Gmail account: {str(e)}")
            return False, None
        finally:
            self.chat_clicker.close_chat()
Editor is loading...
Leave a Comment