From 7d99bcf1908fd9caaf218801460245bcb0af9661 Mon Sep 17 00:00:00 2001 From: Vinyzu <50874994+Vinyzu@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:26:05 +0100 Subject: [PATCH] Adding timeout kwarg and fixing emulate_behaviour kwarg --- cdp_patches/input/async_input.py | 27 ++++++++++++-------------- cdp_patches/input/sync_input.py | 33 +++++++++++++++----------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/cdp_patches/input/async_input.py b/cdp_patches/input/async_input.py index 28974ca..683773c 100644 --- a/cdp_patches/input/async_input.py +++ b/cdp_patches/input/async_input.py @@ -88,34 +88,31 @@ async def _sleep_timeout(self, timeout: Optional[float] = None) -> None: await asyncio.sleep(timeout) - async def click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True) -> None: + async def click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) - await self.down(button=button, x=x, y=y, emulate_behaviour=emulate_behaviour) + await self.down(button=button, x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout) if self.emulate_behaviour and emulate_behaviour: - await self._sleep_timeout() + await self._sleep_timeout(timeout=timeout) await self.up(button=button, x=x, y=y) self.last_x, self.last_y = x, y - async def double_click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True) -> None: + async def double_click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) - emulate_behaviour = self.emulate_behaviour - await self.click(button=button, x=x, y=y) - if self.emulate_behaviour: + await self.click(button=button, x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour) + if self.emulate_behaviour and emulate_behaviour: # self._sleep_timeout(random.uniform(0.14, 0.21)) - await self._sleep_timeout() - self.emulate_behaviour = False - await self.click(button=button, x=x, y=y) + await self._sleep_timeout(timeout=timeout) + await self.click(button=button, x=x, y=y, emulate_behaviour=False, timeout=timeout) - self.emulate_behaviour = emulate_behaviour self.last_x, self.last_y = x, y - async def down(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True) -> None: + async def down(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) if self.emulate_behaviour and emulate_behaviour: - await self.move(x=x, y=y) + await self.move(x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour) self._base.down(button=button, x=x, y=y) self.last_x, self.last_y = x, y @@ -125,7 +122,7 @@ async def up(self, button: Literal["left", "right", "middle"], x: Union[int, flo self._base.up(button=button, x=x, y=y) self.last_x, self.last_y = x, y - async def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True) -> None: + async def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) if self.emulate_behaviour and emulate_behaviour: @@ -135,7 +132,7 @@ async def move(self, x: Union[int, float], y: Union[int, float], emulate_behavio for human_x, human_y in humanized_points.points: # Threaded Movement as Calls to API are too slow threading.Thread(target=self._base.move, args=(int(human_x), int(human_y))).start() - await self._sleep_timeout() + await self._sleep_timeout(timeout=timeout) else: self._base.move(x=x, y=y) diff --git a/cdp_patches/input/sync_input.py b/cdp_patches/input/sync_input.py index b42f91a..ea0e3e8 100644 --- a/cdp_patches/input/sync_input.py +++ b/cdp_patches/input/sync_input.py @@ -79,34 +79,31 @@ def _sleep_timeout(self, timeout: Optional[float] = None) -> None: time.sleep(timeout) - def click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float]) -> None: + def click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) - self.down(button=button, x=x, y=y) - if self.emulate_behaviour: - self._sleep_timeout() + self.down(button=button, x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout) + if self.emulate_behaviour and emulate_behaviour: + self._sleep_timeout(timeout=timeout) self.up(button=button, x=x, y=y) self.last_x, self.last_y = x, y - def double_click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float]) -> None: + def double_click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) - emulate_behaviour = self.emulate_behaviour - self.click(button=button, x=x, y=y) - if self.emulate_behaviour: + self.click(button=button, x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour) + if emulate_behaviour and self.emulate_behaviour: # self._sleep_timeout(random.uniform(0.14, 0.21)) - self._sleep_timeout() - self.emulate_behaviour = False - self.click(button=button, x=x, y=y) + self._sleep_timeout(timeout=timeout) + self.click(button=button, x=x, y=y, emulate_behaviour=False, timeout=timeout) - self.emulate_behaviour = emulate_behaviour self.last_x, self.last_y = x, y - def down(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float]) -> None: + def down(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) - if self.emulate_behaviour: - self.move(x=x, y=y) + if self.emulate_behaviour and emulate_behaviour: + self.move(x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout) self._base.down(button=button, x=x, y=y) self.last_x, self.last_y = x, y @@ -116,17 +113,17 @@ def up(self, button: Literal["left", "right", "middle"], x: Union[int, float], y self._base.up(button=button, x=x, y=y) self.last_x, self.last_y = x, y - def move(self, x: Union[int, float], y: Union[int, float]) -> None: + def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: x, y = int(x), int(y) - if self.emulate_behaviour: + if self.emulate_behaviour and emulate_behaviour: humanized_points = HumanizeMouseTrajectory((self.last_x, self.last_y), (x, y)) # Move Mouse to new random locations for human_x, human_y in humanized_points.points: # Threaded Movement as Calls to API are too slow threading.Thread(target=self._base.move, args=(int(human_x), int(human_y))).start() - self._sleep_timeout() + self._sleep_timeout(timeout=timeout) self._base.move(x=x, y=y) self.last_x, self.last_y = x, y