Refactor with pyautogui
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from fabric import Connection
|
||||
|
||||
from .xdotool import XDoToolController
|
||||
from .python import PythonController
|
||||
|
||||
class AbstractKeyboardController(ABC):
|
||||
@abstractmethod
|
||||
def type(self, text: str):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def key(self, key: str):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def key_down(self, key: str):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def key_up(self, key: str):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class XDoToolKeyboardController(AbstractKeyboardController, XDoToolController):
|
||||
def __init__(self, ssh_connection: Connection):
|
||||
super().__init__(ssh_connection=ssh_connection)
|
||||
|
||||
def type(self, text: str):
|
||||
self._execute_xdotool_command(f"type {text}")
|
||||
|
||||
def key(self, key: str):
|
||||
self._execute_xdotool_command(f"key {key}")
|
||||
|
||||
def key_down(self, key: str):
|
||||
self._execute_xdotool_command(f"keydown {key}")
|
||||
|
||||
def key_up(self, key: str):
|
||||
self._execute_xdotool_command(f"keyup {key}")
|
||||
|
||||
class PythonKeyboardController(AbstractKeyboardController, PythonController):
|
||||
def __init__(self, http_server: str):
|
||||
super().__init__(http_server=http_server)
|
||||
self.command = "python -c \"import keyboard; {command}\""
|
||||
|
||||
def type(self, text: str):
|
||||
self._execute_python_command(self.command.format(command=f"keyboard.write('{text}')"))
|
||||
|
||||
def key(self, key: str):
|
||||
self._execute_python_command(self.command.format(command=f"keyboard.press_and_release('{key}')"))
|
||||
|
||||
def key_down(self, key: str):
|
||||
self._execute_python_command(self.command.format(command=f"keyboard.press('{key}')"))
|
||||
|
||||
def key_up(self, key: str):
|
||||
self._execute_python_command(self.command.format(command=f"keyboard.release('{key}')"))
|
||||
@@ -1,162 +0,0 @@
|
||||
from enum import Enum
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from fabric import Connection
|
||||
import re
|
||||
|
||||
from .xdotool import XDoToolController
|
||||
from .python import PythonController
|
||||
class MouseClick(Enum):
|
||||
LEFT = 1
|
||||
MIDDLE = 2
|
||||
RIGHT = 3
|
||||
WHEEL_UP = 4
|
||||
WHEEL_DOWN = 5
|
||||
|
||||
class AbstractMouseController(ABC):
|
||||
@abstractmethod
|
||||
def get_mouse(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def mouse_move(self, x: int, y: int):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def left_down(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def left_up(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def left_click(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def middle_down(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def middle_up(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def middle_click(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def right_down(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def right_up(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def right_click(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def scroll_up(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def scroll_down(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class XDoToolMouseController(AbstractMouseController, XDoToolController):
|
||||
def __init__(self, ssh_connection: Connection):
|
||||
super().__init__(ssh_connection=ssh_connection)
|
||||
|
||||
def get_mouse(self):
|
||||
output = self._execute_xdotool_command(f"")
|
||||
parts = output.split(" ")
|
||||
x = int(parts[0].split(":")[1])
|
||||
y = int(parts[1].split(":")[1])
|
||||
return x, y
|
||||
|
||||
def mouse_move(self, x: int, y: int):
|
||||
self._execute_xdotool_command(f"mousemove {x} {y}")
|
||||
|
||||
def left_down(self):
|
||||
self._execute_xdotool_command(f"mousedown 1")
|
||||
|
||||
def left_up(self):
|
||||
self._execute_xdotool_command(f"mouseup 1")
|
||||
|
||||
def left_click(self):
|
||||
self._execute_xdotool_command(f"click 1")
|
||||
|
||||
def middle_down(self):
|
||||
self._execute_xdotool_command(f"mousedown 2")
|
||||
|
||||
def middle_up(self):
|
||||
self._execute_xdotool_command(f"mouseup 2")
|
||||
|
||||
def middle_click(self):
|
||||
self._execute_xdotool_command(f"click 2")
|
||||
|
||||
def right_down(self):
|
||||
self._execute_xdotool_command(f"mousedown 3")
|
||||
|
||||
def right_up(self):
|
||||
self._execute_xdotool_command(f"mouseup 3")
|
||||
|
||||
def right_click(self):
|
||||
self._execute_xdotool_command(f"click 3")
|
||||
|
||||
def scroll_up(self):
|
||||
self._execute_xdotool_command(f"click 4")
|
||||
|
||||
def scroll_down(self):
|
||||
self._execute_xdotool_command(f"click 5")
|
||||
|
||||
class PythonMouseController(AbstractMouseController, PythonController):
|
||||
def __init__(self, http_server: str):
|
||||
super().__init__(http_server=http_server)
|
||||
self.command = "py -c \"import mouse; {command}\""
|
||||
|
||||
def get_mouse(self):
|
||||
response = self._execute_python_command(self.command.format(command=f"print(mouse.get_position())"))
|
||||
numbers = re.findall(r'-?\d+', response["output"])
|
||||
x, y = map(int, numbers)
|
||||
return x, y
|
||||
|
||||
def mouse_move(self, x: int, y: int):
|
||||
self._execute_python_command(self.command.format(command=f"mouse.move({x}, {y})"))
|
||||
|
||||
def left_down(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.press(button='left')"))
|
||||
|
||||
def left_up(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.release(button='left')"))
|
||||
|
||||
def left_click(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.click(button='left')"))
|
||||
|
||||
def middle_down(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.press(button='middle')"))
|
||||
|
||||
def middle_up(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.release(button='middle')"))
|
||||
|
||||
def middle_click(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.click(button='middle')"))
|
||||
|
||||
def right_down(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.press(button='right')"))
|
||||
|
||||
def right_up(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.release(button='right')"))
|
||||
|
||||
def right_click(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.click(button='right')"))
|
||||
|
||||
def scroll_up(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.wheel(10)"))
|
||||
|
||||
def scroll_down(self):
|
||||
self._execute_python_command(self.command.format(command="mouse.wheel(-10)"))
|
||||
@@ -1,15 +1,31 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class PythonController:
|
||||
def __init__(self, http_server: str):
|
||||
def __init__(self, http_server: str, pkgs_prefix: str = "py -c \"import pyautogui; {command}\""):
|
||||
self.http_server = http_server
|
||||
self.pkgs_prefix = pkgs_prefix # fixme: this is a hacky way to execute python commands. fix it and combine it with installation of packages
|
||||
|
||||
def _execute_python_command(self, command: str) -> None:
|
||||
payload = json.dumps({
|
||||
"command": command
|
||||
})
|
||||
def get_screenshot(self):
|
||||
"""
|
||||
Gets a screenshot from the server. With the cursor.
|
||||
"""
|
||||
response = requests.get(self.http_server + "/screenshot")
|
||||
if response.status_code == 200:
|
||||
return response.content
|
||||
else:
|
||||
print("Failed to get screenshot. Status code:", response.status_code)
|
||||
return None
|
||||
|
||||
def execute_python_command(self, command: str) -> None:
|
||||
"""
|
||||
Executes a python command on the server.
|
||||
It can be used to execute the pyautogui commands, or... any other python command. who knows?
|
||||
"""
|
||||
command = self.pkgs_prefix.format(command=command)
|
||||
payload = json.dumps({"command": command})
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
@@ -23,15 +39,3 @@ class PythonController:
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to execute the command:", e)
|
||||
|
||||
|
||||
# example usage
|
||||
if __name__ == '__main__':
|
||||
# replace with your actual server URL of the vm
|
||||
server_url = "http://192.168.7.129:5000"
|
||||
controller = PythonController(server_url)
|
||||
|
||||
# example commands
|
||||
python_command = "python -c \"import keyboard; keyboard.write('hello world')\""
|
||||
python_command = "python -c \"import mouse; mouse.move(100,100);mouse.right_click()\""
|
||||
controller._execute_python_command(python_command)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
from fabric import Connection
|
||||
from typing import List
|
||||
|
||||
|
||||
class XDoToolController:
|
||||
def __init__(self, ssh_connection: Connection):
|
||||
self.ssh_connection = ssh_connection
|
||||
|
||||
def _execute_xdotool_command(self, command: List[str]) -> None:
|
||||
result = self.ssh_connection.run(f"DISPLAY=:0 xdotool {command}", hide=True)
|
||||
return result.stdout.strip()
|
||||
Reference in New Issue
Block a user