Skip to content

Commit

Permalink
Adding timeout kwarg and fixing emulate_behaviour kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinyzu committed Mar 25, 2024
1 parent de86681 commit 7d99bcf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
27 changes: 12 additions & 15 deletions cdp_patches/input/async_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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)
Expand Down
33 changes: 15 additions & 18 deletions cdp_patches/input/sync_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 7d99bcf

Please sign in to comment.