-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_speed.py
executable file
·47 lines (37 loc) · 1.48 KB
/
check_speed.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
#!/usr/bin/env python
import datetime
import json
import os
import speedtest
DATA_FILE = "/Users/mgalloy/data/speed.json"
if __name__ == "__main__":
# get upload/download speeds
servers = []
s = speedtest.Speedtest()
all_servers = s.get_servers(servers)
best_server = s.get_best_server()
download_speed = s.download() / 1000 / 1000 # Mbs
upload_speed = s.upload() / 1000 / 1000 # Mbs
today = datetime.date.today()
download_pt = {"value": download_speed, "title": "%s" % today}
upload_pt = {"value": upload_speed, "title": "%s" % today}
if os.path.isfile(DATA_FILE):
with open(DATA_FILE, "r") as f:
result = json.load(f)
result["graph"]["datasequences"][0]["datapoints"].append(upload_pt)
result["graph"]["datasequences"][1]["datapoints"].append(download_pt)
else:
result = {"graph": {"datasequences": [{ "datapoints": [upload_pt],
"title": "Upload @ 3 am" },
{ "datapoints": [download_pt],
"title": "Download @ 3 am" }],
"total": False,
"type": "line",
"refreshEveryNSeconds": 3600,
"title": "Internet speed",
"yAxis": {"minValue": 0, "maxValue": 1000, "units": {"suffix": "Mbs"}}
}
}
# update json file
with open(DATA_FILE, "w") as f:
json.dump(result, f)