Create plexpy_bw_calc.py

This commit is contained in:
12nick12 2019-02-06 19:41:32 -05:00 committed by GitHub
parent 686a716b42
commit f8d7e095b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 0 deletions

44
plexpy_bw_calc.py Normal file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python
import json
import requests
import math
f = open("/home/hass/.homeassistant/python_scripts/plexpy/.creds", "r")
data = f.read().splitlines()
f.close()
host = data[4]
api_key = data[5]
url_base = data[6]
ssl = data[7]
if ssl == 'true':
http = 'https'
if ssl == 'false':
http = 'http'
url = http + "://" + host + "/" + url_base + "/api/v2?apikey=" + api_key + "&cmd=get_activity"
response = requests.get(url)
json_input = response.text
decoded = json.loads(json_input)
stream_count = decoded['response']['data']['stream_count']
stream_count = int(stream_count)
bandwidthTotal = 0
bw_total = decoded['response']['data']['total_bandwidth']
bw_total = float(bw_total) / 1024
#for i in range(0, stream_count):
# bandwidth = decoded['response']['data']['sessions'][i]['bandwidth']
# bandwidth = float(bandwidth)
# if bandwidth < 102400:
# bandwidthTotal = bandwidthTotal + bandwidth / 1024
#print("%.2f" % bandwidthTotal)
print("%.2f" % bw_total)