Created
February 16, 2017 16:25
-
-
Save Technifocal/10f2adb3e047ac8dc44504fe5d75d126 to your computer and use it in GitHub Desktop.
Ring doorbell telegraf input.exec script
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
#!/usr/bin/python2 | |
import json, urllib, urllib2, base64, os | |
USERNAME = "[email protected]" | |
PASSWORD = "C0rrectH0rseBatteryStaple" | |
GUID = "GENERATE_A_GUID_AND_PUT_IT_HERE_FORMAT_IS_34f0a43b-5091-4592-9b66-e69b0e1f1b24" | |
def login(username, password): | |
postData = urllib.urlencode({'device[os]': 'android', | |
'device[hardware_id]': GUID}) | |
request = urllib2.Request("https://api.ring.com/clients_api/session", postData) | |
request.add_header("Authorization", "Basic %s" % base64.b64encode('%s:%s' % (username, password))) | |
response = json.load(urllib2.urlopen(request)) | |
if(response['profile']['email'].lower() != username.lower()): | |
sys.exit("Error - Something went wrong"); | |
return response['profile']['authentication_token'] | |
def print_batteries(auth_token): | |
request = urllib2.Request("https://api.ring.com/clients_api/doorbots?api_version=9&auth_token=%s" % auth_token) | |
try: | |
jsonResponse = json.load(urllib2.urlopen(request)) | |
except urllib2.HTTPError: | |
return False | |
for doorbot in jsonResponse["doorbots"]: | |
print("ringdoorbell,description=%s,firmwareversion=%s battery=%d" % (doorbot['description'].replace(' ', '\ '), doorbot['firmware_version'].replace(' ', '\ '), int(doorbot['battery_life']))) | |
return True | |
config = {} | |
try: | |
with open('%s/.ringdoorbell.json' % os.path.expanduser('~'), 'r') as f: | |
config = json.load(f) | |
except IOError: | |
config['auth_token'] = login(USERNAME, PASSWORD) | |
if not print_batteries(config['auth_token']): | |
config['auth_token'] = login(USERNAME, PASSWORD) | |
print_batteries(config['auth_token']) | |
with open('%s/.ringdoorbell.json' % os.path.expanduser('~'), 'w') as f: | |
json.dump(config, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment