kfsone

iPython automation #1

Jun 11th, 2017
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2.  
  3. hub      = "philips.lan"  # or provide the ip address
  4. appname  = "ipytest"      # call it what you like
  5.  
  6. username = None
  7.  
  8.  
  9. def query(query_path, body=None):
  10.     if body is None:  # Go the old route
  11.         result = requests.get("http://" + hub + query_path)
  12.     else:
  13.         result = requests.post("http://" + hub + query_path, json=body)
  14.     return result.json()
  15.  
  16.  
  17. def register():
  18.     global username  # Because we are going to modify it.
  19.     while username is None:
  20.         result = query("/api", {'devicetype': appname})
  21.         status = result[0]
  22.         error = status.get('error')
  23.         if error:
  24.             # Check for code 101, not authorized
  25.             if error['type'] == 101:
  26.                 input("NOT AUTHORIZED! "
  27.                       "Press the button on your hub and hit return.")
  28.                 continue
  29.             raise Exception("Unknown error from hub: " + str(error))
  30.  
  31.         success = status['success']
  32.         username = success['username']
  33.         print("* Registered as", username)
RAW Paste Data