Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.8 kB
2
Indexable
Never
import argparse
import random
import time
import pyautogui

from datetime import timedelta, parser

# Define valid time unit characters and their corresponding conversion factors
TIME_UNITS = {
    'h': 3600,
    'm': 60,
    's': 1
}

def parse_duration(duration_str):
    """Parses a duration string in the format "XdYh" (e.g., "2h30m45s")
    into seconds.

    Args:
        duration_str (str): The duration string to parse.

    Returns:
        int: The duration in seconds.

    Raises:
        ValueError: If the duration string is in an invalid format.
    """

    try:
        # Extract potential time components (default to 0 if not specified)
        days, hours, minutes, seconds = map(
            lambda x: int(x) if x else 0, duration_str.lower().split('d') + ['0'] * 3)

        # Check for a valid time unit character at the end
        if not duration_str[-1:] in TIME_UNITS:
            raise ValueError("Invalid duration format. Specify time units (h, m, or s).")

        # Calculate total duration in seconds
        duration = days * 24 * 3600 + hours * TIME_UNITS['h'] + minutes * TIME_UNITS['m'] + seconds * TIME_UNITS['s']
        return duration
    except ValueError as e:
        raise ValueError(f"Invalid duration: {duration_str}. {e}")

def simulate_activity(duration):
    """Simulates keyboard and mouse activity for the specified duration.

    Args:
        duration (int): The duration in seconds.
    """

    end_time = time.time() + duration

    while time.time() < end_time:
        # Randomly wait between 30 and 75 seconds before simulating Alt+Tab
        wait_time = random.uniform(30, 75)
        time.sleep(wait_time)

        # Simulate Alt+Tab keypress
        pyautogui.hotkey('alt', 'tab')

        # Randomly wait between 30 and 60 seconds before moving the mouse
        wait_time = random.uniform(30, 60)
        time.sleep(wait_time)

        # Randomly move the mouse within a reasonable area (adjust as needed)
        max_x, max_y = pyautogui.size()  # Get screen dimensions
        mouse_x = random.randint(100, max_x - 100)
        mouse_y = random.randint(100, max_y - 100)
        pyautogui.moveTo(mouse_x, mouse_y)

def main():
    parser = argparse.ArgumentParser(description="Simulates keyboard and mouse activity.")
    parser.add_argument("duration", type=str, help="Duration for which to simulate activity (e.g., 2h30m45s)")
    args = parser.parse_args()

    try:
        duration = parse_duration(args.duration)
        simulate_activity(duration)
        print(f"Activity simulation completed after {duration} seconds.")
    except ValueError as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()
Leave a Comment