-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathspeedtest-charts.py
executable file
·112 lines (87 loc) · 2.92 KB
/
speedtest-charts.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
import datetime
import pygsheets
import speedtest
import argparse
# Set options
parser = argparse.ArgumentParser(
description='Simple Python script to push speedtest results \
(using speedtest-cli) to a Google Docs spreadsheet'
)
parser.add_argument(
"-w, --workbookname", action="store", default="Speedtest", type=str,
dest="workbookname",
help='Sets the woorkbook name, default is "Speedtest"'
)
parser.add_argument(
"-b, --bymonth", action="store_true", default=False,
dest="bymonth",
help='Creats a new sheet for each month named MMM YYYY (ex: Jun 2018)'
)
cliarg = parser.parse_args()
# Set constants
DATE = datetime.datetime.now().strftime("%m/%d/%Y %H:%M:%S")
header = [['A1', 'B1', 'C1', 'D1'], ['Date', 'Download', 'Upload', 'Ping']]
if cliarg.bymonth:
sheetname = datetime.datetime.now().strftime("%b %Y")
# set variable scope
download = ''
upload = ''
ping = ''
def get_credentials():
"""Function to check for valid OAuth access tokens."""
gc = pygsheets.authorize(outh_file="credentials.json")
return gc
def submit_into_spreadsheet(download, upload, ping):
"""Function to submit speedtest result."""
gc = get_credentials()
try:
speedtest = gc.open(cliarg.workbookname)
except pygsheets.SpreadsheetNotFound:
speedtest = gc.create(cliarg.workbookname)
if cliarg.bymonth:
try:
sheet = speedtest.worksheet('title', sheetname)
except pygsheets.WorksheetNotFound:
sheet = speedtest.add_worksheet(sheetname)
else:
sheet = speedtest.sheet1
headnew = str(sheet.cell('A1').value)
headcur = str(header[1][0])
if headnew != headcur:
# create header row
for index in range(len(header[0])):
head = sheet.cell(header[0][index])
head.value = header[1][index]
head.update()
data = [DATE, download, upload, ping]
sheet.append_table(values=data)
def getresults():
"""Function to generate speedtest result."""
spdtest = speedtest.Speedtest()
spdtest.get_best_server()
download = round(spdtest.download() / 1000 / 1000, 2)
upload = round(spdtest.upload() / 1000 / 1000, 2)
ping = round(spdtest.results.ping)
return(download, upload, ping)
def main():
# Check for proper credentials
print("Checking OAuth validity...")
try:
get_credentials()
except pygsheets.AuthenticationError:
print("Authentication Failed")
raise
# Run speedtest and store output
print("Starting speed test...")
download, upload, ping = getresults()
print(
"Starting speed finished (Download: ", download,
", Upload: ", upload,
", Ping: ", ping, ")")
# Write to spreadsheet
print("Writing to spreadsheet...")
submit_into_spreadsheet(download, upload, ping)
print("Successfuly written to spreadsheet!")
if __name__ == "__main__":
main()