-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeedtest.py
142 lines (107 loc) · 3.7 KB
/
Speedtest.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import re
import subprocess
import time
import csv
import speedtest
# Script Variables
FILENAME = 'internet_speedtest.csv'
HEADER = ['Date',
'Time',
'Ping [ms]',
'Download Speed [Mbit/s]',
'Upload Speed [Mbit/s]']
def main():
# Get directory of script
abspath = os.path.abspath(__file__)
script_path = os.path.dirname(abspath)
os.chdir(script_path)
try:
# Run the Speed Test
speed_test = speedtest.Speedtest()
download = round(speed_test.download() / 104857, 2)
upload = round(speed_test.upload() / 104857, 2)
ping = 0
except:
ping = 0
download = 0
upload = 0
# Get the Date/Time
time_date = time.strftime('%m/%d/%y')
time_time = time.strftime('%H:%M')
# Create a CSV Line
csv_line = (time_date, time_time, ping, download, upload)
csv_line_string = '%s, %s, %f, %f, %f' % csv_line
print(csv_line_string)
# Write to CSV File
WriteToCsv(filename=FILENAME, values=csv_line, header=HEADER)
# Upload file to Google Drive
# drive = AuthenticateGoogleDrive()
# DeleteDriveFile(drive=drive, filename=FILENAME, folder='root')
# drive_file = drive.CreateFile()
# drive_file.SetContentFile(FILENAME)
# drive_file.Upload()
def WriteToCsv(filename, values, header=None):
""" Take in a list of values and write them to a CSV
If 'filename' doesn't exist, then a new file is created. If 'filename' does
exist, it is is appended to the file.
args:
filename: string: Filename of the file to write to, including extension.
values: List of values to write to the CSV
header: List of values to write as a header to the CSV file. Will only
write in the case that the file does not already exist
"""
if os.path.exists(filename):
# Append the row if the file already exists
with open(filename, 'a') as fd:
writer = csv.writer(fd)
writer.writerow(values)
else:
# Create a new file if the file does not exist
with open(filename, 'w') as fd:
writer = csv.writer(fd)
if header is not None: writer.writerow(header)
writer.writerow(values)
def AuthenticateGoogleDrive(credentials='mycreds.txt'):
""" Use authentication token to retun Google Drive class
Function was first defined here:
https://stackoverflow.com/a/24542604/5096199
args:
credentials: string. Name of file containing Google Drive credentials
returns:
drive: Instance of pydrive
"""
gauth = GoogleAuth()
gauth.LoadCredentialsFile(credentials)
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile(credentials)
drive = GoogleDrive(gauth)
return drive
def DeleteDriveFile(drive, filename, folder='root'):
""" Delete a file located on Google Drive
args:
drive: Instance of gDrive given from PyDrive
folder: string. Name of folder where the file is located
filename: string. Name of the file, including extension
returns:
drive: Instance of pydrive
"""
list_dictionary = {'q': "'%s' in parents and trashed=false" % folder}
file_list = drive.ListFile(list_dictionary).GetList()
try:
for file1 in file_list:
if file1['title'] == filename:
file1.Delete()
except:
pass
if __name__ == "__main__":
main()