-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathping_calculations_API.py
executable file
·68 lines (48 loc) · 1.82 KB
/
ping_calculations_API.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import requests
resp = requests.get('https://io.adafruit.com/api/feeds/ping/data', headers={'x-aio-key':'YOUR_ADAFRUIT_API_KEY'})
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('GET /tasks/ {}'.format(resp.status_code))
count = 0
ping_list = []
ping_avg = 0
for item in resp.json():
print('{} {}'.format(item['id'], item['value']))
count = count + 1
print('Count: ', count)
ping_list.append(round(float(item['value']), 3))
ping_avg = round(sum(ping_list)/len(ping_list), 3)
ping_min = ping_list[0]
ping_max = ping_list[0]
for ping in ping_list:
#print('ping_list value: ', ping)
if ping < ping_min:
ping_min = ping
#print('new ping_min value: ', ping_min)
if ping > ping_max:
ping_max = ping
#print('new ping_max value: ', ping_max)
avg_url = "https://io.adafruit.com/api/feeds/ping-avg/data"
avg_payload = "{\n\t\"value\":\"" + str(ping_avg) + "\"\n}"
avg_headers = {
'x-aio-key': "YOUR_ADAFRUIT_API_KEY",
'content-type': "application/json"
}
avg_response = requests.request("POST", avg_url, data=avg_payload, headers=avg_headers)
print(avg_response.text)
min_url = "https://io.adafruit.com/api/feeds/ping-min/data"
min_payload = "{\n\t\"value\":\"" + str(ping_min) + "\"\n}"
min_headers = {
'x-aio-key': "YOUR_ADAFRUIT_API_KEY",
'content-type': "application/json"
}
min_response = requests.request("POST", min_url, data=min_payload, headers=min_headers)
print(min_response.text)
max_url = "https://io.adafruit.com/api/feeds/ping-max/data"
max_payload = "{\n\t\"value\":\"" + str(ping_max) + "\"\n}"
max_headers = {
'x-aio-key': "YOUR_ADAFRUIT_API_KEY",
'content-type': "application/json"
}
max_response = requests.request("POST", max_url, data=max_payload, headers=max_headers)
print(max_response.text)