-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.py
More file actions
92 lines (83 loc) · 2.53 KB
/
Copy pathsrc.py
File metadata and controls
92 lines (83 loc) · 2.53 KB
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
import os
import httplib2
from settings import *
from pyspeedtest import SpeedTest
from datetime import datetime, timedelta
from apiclient import discovery
from time import sleep
from csv import writer
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
def get_credentials():
"""
Gets valid user credentials from storage.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'internet-speed-test-data.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def run_speed_test(ap, device, number_of_tests, interval, stressed, room, notes=""):
"""
Run the speed test for a duration given some interval.
"""
count = 0
st = SpeedTest()
test_data = []
while number_of_tests > count:
count += 1
print "Running speed test: " + str(count)
try:
test_data.append([
ap,
device,
round(st.ping(), 2),
round(st.download() / BITS_TO_MEGA_BITS, 2),
round(st.upload() / BITS_TO_MEGA_BITS, 2),
stressed,
room,
notes
])
sleep(interval * 60)
except:
print "There was an exception while testing."
if len(test_data) > 0:
append_to_drive(test_data)
print "Completed %d speed tests" % count
def append_to_drive(values):
"""
Append the data products to the google spreadsheet.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build(
'sheets',
'v4',
http=http,
discoveryServiceUrl=GOOGLE_DRIVE_URL
)
service.spreadsheets().values().append(
spreadsheetId=SPEADSHEET_ID,
range=RANGE_NAME,
valueInputOption=VALUE_INPUT_OPTION,
body={'values': values}
).execute()
run_speed_test(
ap=ACCESS_POINT,
device=DEVICE,
number_of_tests=NUMBER_OF_TESTS,
interval=INTERNVAL,
stressed=STRESSED_NETWORK,
room=ROOM,
notes=NOTES
)