Advertisement
Guest User

Untitled

a guest
Aug 12th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. """Move the mouse based on Beam Interactive controls."""
  2.  
  3. import asyncio
  4.  
  5. from urllib.parse import urljoin
  6.  
  7. from math import isnan
  8.  
  9. from beam_interactive import start, proto
  10. from requests import Session
  11. from pymouse import PyMouse
  12. from pykeyboard import PyKeyboard
  13.  
  14. URL = "https://beam.pro/api/v1/"
  15.  
  16. AUTHENTICATION = {
  17. "username": "SkilledSniper1",
  18. "password": "deletedcausepasswordthisnotmypassword",
  19. "code": "2FA-CODE" # Unnecessary if two-factor authentication is disabled.
  20. }
  21.  
  22. SESSION = Session()
  23. MOUSE = PyMouse()
  24. KEYBO = PyKeyboard()
  25.  
  26. def _build(endpoint, *, url=URL):
  27. """Build an address for an API endpoint."""
  28. return urljoin(url, endpoint.lstrip('/'))
  29.  
  30.  
  31. def login(username, password, code='', *, session=SESSION):
  32. """Log into Beam via the API."""
  33. auth = dict(username=username, password=password, code=code)
  34. return session.post(_build("/users/login"), data=auth).json()
  35.  
  36.  
  37. def join_interactive(channel, *, session=SESSION):
  38. """Retrieve interactive connection information."""
  39. return session.get(_build("/interactive/{channel}/robot").format(
  40. channel=channel)).json()
  41.  
  42.  
  43. def on_error(error):
  44. """Handle error packets."""
  45. print("Oh no, there was an error!", error.message)
  46.  
  47.  
  48. def on_report(report):
  49. """Handle report packets."""
  50.  
  51. ``` # Tactile Mouse Click Control
  52. for tactile in report.tactile:
  53. if tactile.pressFrequency:
  54. print("Tactile report received!", tactile, sep='\n')
  55. MOUSE.click(*MOUSE.position()) ```
  56.  
  57. # Joystick Mouse Movement Control
  58. for joystick in report.joystick:
  59. if not isnan(joystick.coordMean.x) and not isnan(joystick.coordMean.y):
  60. print("Joystick report received!", joystick, sep='\n')
  61. mouse_x, mouse_y = MOUSE.position()
  62.  
  63. MOUSE.move(
  64. round(joystick.coordMean.x*20) + mouse_x,
  65. round(joystick.coordMean.y*20) + mouse_y
  66. )
  67.  
  68.  
  69. @asyncio.coroutine
  70. def run():
  71. """Run the interactive app."""
  72.  
  73. # Authenticate with Beam and retrieve the channel id from the response.
  74. channel_id = login( # **AUTHENTICATION is a cleaner way of doing this.
  75. AUTHENTICATION["username"],
  76. AUTHENTICATION["password"],
  77. AUTHENTICATION["code"]
  78. )["channel"]["id"]
  79.  
  80. # Get Interactive connection information.
  81. data = join_interactive(channel_id)
  82.  
  83. # Initialize a connection with Interactive.
  84. connection = yield from start(data["address"], channel_id, data["key"])
  85.  
  86. # Handlers, to be called when Interactive packets are received.
  87. handlers = {
  88. proto.id.error: on_error,
  89. proto.id.report: on_report
  90. }
  91.  
  92. # wait_message is a coroutine that will return True when it receives
  93. # a complete packet from Interactive, or False if we got disconnected.
  94. while (yield from connection.wait_message()):
  95.  
  96. # Decode the Interactive packet.
  97. decoded, _ = connection.get_packet()
  98. packet_id = proto.id.get_packet_id(decoded)
  99.  
  100. # Handle the packet with the proper handler, if its type is known.
  101. if packet_id in handlers:
  102. handlers[packet_id](decoded)
  103. elif decoded is None:
  104. print("Unknown bytes were received. Uh oh!", packet_id)
  105. else:
  106. print("We got packet {} but didn't handle it!".format(packet_id))
  107.  
  108. connection.close()
  109.  
  110.  
  111. if __name__ == "__main__":
  112. loop = asyncio.get_event_loop()
  113.  
  114. try:
  115. loop.run_until_complete(run())
  116. finally:
  117. loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement