Advertisement
Guest User

BeamtestKey.py

a guest
Aug 12th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 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": "passwordhere",
  19. "code": "2FA-CODE" # Unnecessary if two-factor authentication is disabled.
  20. }
  21.  
  22. SESSION = Session()
  23. MOUSE = PyMouse()
  24. KEYBOARD = 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. def join_interactive(channel, *, session=SESSION):
  37. """Retrieve interactive connection information."""
  38. return session.get(_build("/interactive/{channel}/robot").format(
  39. channel=channel)).json()
  40.  
  41. def on_error(error):
  42. """Handle error packets."""
  43. print("Oh no, there was an error!", error.message)
  44.  
  45. def on_report(report):
  46. """Handle report packets."""
  47.  
  48. # Tactile Mouse Click Control
  49. for tactile in report.tactile:
  50. if tactile.pressFrequency:
  51. print("Tactile report received!", tactile, sep='\n')
  52. if tactile.id==4:
  53. KEYBOARD.press_key(character='h')
  54. KEYBOARD.release_key(character='h')
  55. print("PRESSING H")
  56. elif tactile.id==5:
  57. KEYBOARD.press_key(character='q')
  58. KEYBOARD.release_key(character='q')
  59. print("PRESSING Q")
  60. elif tactile.id==6:
  61. KEYBOARD.press_key(character='r')
  62. KEYBOARD.release_key(character='r')
  63. print("PRESSING R")
  64. elif tactile.id==7:
  65. KEYBOARD.press_key(character='e')
  66. KEYBOARD.release_key(character='e')
  67. print("PRESSING E")
  68. elif tactile.id==8:
  69. KEYBOARD.press_key(character='c')
  70. KEYBOARD.release_key(character='c')
  71. print("PRESSING C")
  72. elif tactile.id==9:
  73. KEYBOARD.press_key(character='1')
  74. KEYBOARD.release_key(character='1')
  75. print("PRESSING 1")
  76. elif tactile.id==10:
  77. KEYBOARD.press_key(character='2')
  78. KEYBOARD.release_key(character='2')
  79. print("PRESSING 2")
  80. elif tactile.holding:
  81. print("Tactile report received!", tactile, sep='\n')
  82. if tactile.id==0:
  83. KEYBOARD.press_key(character='w')
  84. KEYBOARD.release_key(character='w')
  85. print("PRESSING W")
  86. elif tactile.id==1:
  87. KEYBOARD.press_key(character='a')
  88. KEYBOARD.release_key(character='a')
  89. print("PRESSING A")
  90. elif tactile.id==2:
  91. KEYBOARD.press_key(character='s')
  92. KEYBOARD.release_key(character='s')
  93. print("PRESSING S")
  94. elif tactile.id==3:
  95. KEYBOARD.press_key(character='d')
  96. KEYBOARD.release_key(character='d')
  97. print("PRESSING D")
  98. elif tactile.id==11:
  99. KEYBOARD.press_key(character='g')
  100. KEYBOARD.release_key(character='g')
  101. print("PRESSING G")
  102.  
  103. # Joystick Mouse Movement Control
  104. for joystick in report.joystick:
  105. if not isnan(joystick.coordMean.x) and not isnan(joystick.coordMean.y):
  106. print("Joystick report received!", joystick, sep='\n')
  107. mouse_x, mouse_y = MOUSE.position()
  108.  
  109. MOUSE.move(
  110. round(joystick.coordMean.x*20) + mouse_x,
  111. round(joystick.coordMean.y*20) + mouse_y
  112. )
  113.  
  114.  
  115. @asyncio.coroutine
  116. def run():
  117. """Run the interactive app."""
  118.  
  119. # Authenticate with Beam and retrieve the channel id from the response.
  120. channel_id = login( # **AUTHENTICATION is a cleaner way of doing this.
  121. AUTHENTICATION["username"],
  122. AUTHENTICATION["password"],
  123. AUTHENTICATION["code"]
  124. )["channel"]["id"]
  125.  
  126. # Get Interactive connection information.
  127. data = join_interactive(channel_id)
  128.  
  129. # Initialize a connection with Interactive.
  130. connection = yield from start(data["address"], channel_id, data["key"])
  131.  
  132. # Handlers, to be called when Interactive packets are received.
  133. handlers = {
  134. proto.id.error: on_error,
  135. proto.id.report: on_report
  136. }
  137.  
  138. # wait_message is a coroutine that will return True when it receives
  139. # a complete packet from Interactive, or False if we got disconnected.
  140. while (yield from connection.wait_message()):
  141.  
  142. # Decode the Interactive packet.
  143. decoded, _ = connection.get_packet()
  144. packet_id = proto.id.get_packet_id(decoded)
  145.  
  146. # Handle the packet with the proper handler, if its type is known.
  147. if packet_id in handlers:
  148. handlers[packet_id](decoded)
  149. elif decoded is None:
  150. print("Unknown bytes were received. Uh oh!", packet_id)
  151. else:
  152. print("We got packet {} but didn't handle it!".format(packet_id))
  153.  
  154. connection.close()
  155.  
  156.  
  157. if __name__ == "__main__":
  158. loop = asyncio.get_event_loop()
  159.  
  160. try:
  161. loop.run_until_complete(run())
  162. finally:
  163. loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement