Created
July 19, 2018 21:37
-
-
Save lances101/01fffdb81a11e1372483d663ed61ca43 to your computer and use it in GitHub Desktop.
Python 3.6 CallBill machine look up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 1. logs in to CallBill via its API. | |
# 2. pulls location from config or CallBill profile if not defined | |
# 3. searches for matching machines based on filters in config | |
# (default: washing machines that are available) | |
# 4. once the check cycle is finished, calls config.found_func | |
# (default found_func calls OSX "say" executable) | |
# 5. waits X seconds before next attemp | |
# (default 60 seconds) | |
# | |
import requests | |
import time | |
import json | |
import subprocess | |
class config: | |
SEARCH_FOR = { | |
'serviceType': "WASHING_MACHINE", # WASHING_MACHINE, DRYER, etc | |
"state": "AVAILABLE", # AVAILABLE, STOPPABLE, OCCUPIED | |
# "externalId": "", # external machine id (displayed in UI) | |
# "location": "", # where | |
# "serviceType": "WASHING_MACHINE", | |
} | |
email = "" | |
password = "" | |
location_id = None # set here or it will be grabbed from your profile | |
delay_between_checks_secs = 60 | |
# function that runs when a machine matches | |
# by default uses OSX "say" executable | |
@staticmethod | |
def found_func(machines): | |
for machine in machines: | |
subprocess.call(f"say \"Found {machine['externalId']}\"", shell=True) | |
login_url = "https://www.involtum-services.com/api-rest/login" | |
subscription_url = "https://www.involtum-services.com/api-rest/subscription" | |
machines_url_pattern = "https://www.involtum-services.com/api-rest/location/{locationId}/connectors" | |
headers = { | |
"Language": "en", | |
"Platform": "callbill", | |
"Content-Type": "application/json", | |
} | |
r_login = requests.post(login_url, headers=headers, data=json.dumps({ | |
'email': config.email, | |
'password': config.password | |
})) | |
if not r_login.ok: | |
raise Exception("could not login") | |
login_json = r_login.json() | |
token = login_json['login']['token'] | |
print(f"Logged in. Token: {token}") | |
headers['token'] = token | |
if not config.location_id: | |
r_location = requests.get(subscription_url, headers=headers) | |
if not r_location.ok: | |
raise Exception( | |
"no location_id provided and could not extract location from callbill") | |
config.location_id = r_location.json()['data']['locationExternalId'] | |
print(f"Location id was not provided, fetched {config.location_id}") | |
machines_url = machines_url_pattern.format(locationId=config.location_id) | |
while(True): | |
print("Checking up.") | |
r_machines = requests.get(machines_url, headers=headers) | |
if not r_machines.ok: | |
raise Exception("could not get machines") | |
machines_json = r_machines.json() | |
matches = [] | |
for machine in machines_json['data']: | |
for key, value in config.SEARCH_FOR.items(): | |
if key in machine: | |
if machine[key] != value: | |
break | |
else: | |
print( | |
f"Found by criteria: {machine['externalId']} - {machine['serviceType']}") | |
matches.append(machine) | |
config.found_func(matches) | |
print(f"Check done. Waiting {config.delay_between_checks_secs}") | |
time.sleep(config.delay_between_checks_secs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment