Skip to content

Instantly share code, notes, and snippets.

@Bahm
Last active December 23, 2016 06:01
Show Gist options
  • Save Bahm/079455f2e3324419b5973c860598e798 to your computer and use it in GitHub Desktop.
Save Bahm/079455f2e3324419b5973c860598e798 to your computer and use it in GitHub Desktop.
mouse.py + HTTPServer. 127.0.0.1:8080/?endCoords=100,100&click=true
#!/usr/bin/env python3
import math
from pymouse import PyMouse
from random import randint
from time import sleep
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
class MouseMovementCalculator:
def __init__(self, gravity, wind, mouseSpeed, targetError):
self.gravity = gravity
self.wind = wind
self.mouseSpeed = mouseSpeed
self.targetError = targetError
def calcCoordsAndDelay(self, startCoords, endCoords):
veloX, veloY = (0, 0)
coordsAndDelay = []
xs, ys = startCoords
xe, ye = endCoords
totalDist = math.hypot(xs - xe, ys - ye)
self._windX = 0
self._windY = 0
while True:
veloX, veloY = self._calcVelocity((xs, ys), (xe, ye), veloX, veloY, totalDist)
xs += veloX
ys += veloY
w = round(max(randint(0, max(0, round(100/self.mouseSpeed)-1))*6, 5)*0.9)
coordsAndDelay.append(( xs, ys, w ))
if math.hypot(xs - xe, ys - ye) < 1:
break
if round(xe) != round(xs) or round(ye) != round(ys):
coordsAndDelay.append(( round(xe), round(ye), 0 ))
return coordsAndDelay
def _calcVelocity(self, curCoords, endCoords, veloX, veloY, totalDist):
xs, ys = curCoords
xe, ye = endCoords
dist = math.hypot(xs - xe, ys - ye)
self.wind = max(min(self.wind, dist), 1)
maxStep = None
D = max(min(round(round(totalDist)*0.3)/7, 25), 5)
rCnc = randint(0, 5)
if rCnc == 1:
D = 2
if D <= round(dist):
maxStep = D
else:
maxStep = round(dist)
if dist >= self.targetError:
self._windX = self._windX / math.sqrt(3) + (randint(0, round(self.wind) * 2) - self.wind) / math.sqrt(5)
self._windY = self._windY / math.sqrt(3) + (randint(0, round(self.wind) * 2) - self.wind) / math.sqrt(5)
else:
self._windX = self._windX / math.sqrt(2)
self._windY = self._windY / math.sqrt(2)
veloX = veloX + self._windX
veloY = veloY + self._windY
if(dist != 0):
veloX = veloX + self.gravity * (xe - xs) / dist
veloY = veloY + self.gravity * (ye - ys) / dist
if math.hypot(veloX, veloY) > maxStep:
randomDist = maxStep / 2.0 + randint(0, math.floor(round(maxStep) / 2))
veloMag = math.sqrt(veloX * veloX + veloY * veloY)
veloX = (veloX / veloMag) * randomDist
veloY = (veloY / veloMag) * randomDist
return (veloX, veloY)
class HTTPServer_RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
params = parse_qs(urlparse(self.path).query)
startCoords = mouse.position()
endCoords = None
click = False
if params.get('endCoords') != None:
endCoords = [int(s) for s in params.get('endCoords')[0].split(',')]
if params.get('click') != None:
click = params.get('click')[0] == 'true'
if endCoords != None:
coordsAndDelay = mouseCalc.calcCoordsAndDelay(startCoords, endCoords)
for x, y, delay in coordsAndDelay:
mouse.move(round(x), round(y))
sleep(delay/1000)
if click:
pos = mouse.position()
mouse.click(pos[0], pos[1], 1) # 1 = left, 2 = right, 3 = middle
message = "success"
self.wfile.write(bytes(message, "utf8"))
return
if __name__ == '__main__':
mouse = PyMouse()
mouseSpeed = 30
mouseCalc = MouseMovementCalculator(7, 5, mouseSpeed, 10*mouseSpeed)
server_address = ('127.0.0.1', 8080)
httpd = HTTPServer(server_address, HTTPServer_RequestHandler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment