-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-usb-uploader.py
366 lines (278 loc) · 12.3 KB
/
linux-usb-uploader.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from pyudev import Context, Monitor, MonitorObserver
import re, glob, time, json, urllib.request, urllib.parse, cherrypy, threading, sqlite3, sys, webbrowser, os, requests, subprocess
def startup():
strava_client_secret = "1234abcde"
strava_client_id = "0"
web_port = 9090
sys.stdout = simpleLogger("strava-upload.log")
sys.stderr = simpleLogger("strava-upload-errors.log")
upload_fits.token = strava_get_token()
if not upload_fits.token:
strava_oauth_exchange(strava_client_secret,strava_client_id, web_port)
check_strava_token(strava_get_token())
else:
check_strava_token(strava_get_token())
#If we get this far either something went wrong or we got a token from Strava
upload_fits.token = strava_get_token()
start_udev_monitoring()
def check_strava_token(token):
url = "https://www.strava.com/api/v3/athlete"
try:
headers = {'Authorization': 'Bearer ' + token}
req = urllib.request.Request(url, None, headers)
response = urllib.request.urlopen(req)
response = json.loads(response.read().decode('utf-8'))
return True
except:
print("Current Strava token is invalid. Removing database file. Please restart the script and try again.")
os.unlink("stravatoken.sqlite")
sys.exit(0)
def start_udev_monitoring():
#Setting up Linux UDEV monitoring in a separate thread
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block')
observer = MonitorObserver(monitor, callback=find_garmin, name='monitor-observer')
observer.daemon = True
observer.start()
#End UDEV init
def strava_oauth_exchange(strava_client_secret,strava_client_id, web_port):
strava_check_create_tables()
#Send the user off to Strava to authorize us and start local webserver
strava_oauth_url = "https://www.strava.com/oauth/authorize?client_id=%s&response_type=code&redirect_uri=http://localhost:%s/strava_token_exchange&scope=write&approval_prompt=auto" % (strava_client_id, web_port)
webbrowser.open_new_tab(strava_oauth_url)
##Disable cherrypy logging to stdout, bind to all IPs, start in a separate thread
cherrypy.engine.autoreload.on = False
cherrypy.log.screen=False
cherrypy.server.socket_host = "0.0.0.0"
cherrypy.server.socket_port = web_port
cherrypy.quickstart(webServer(strava_client_secret,strava_client_id))
def find_garmin(device):
fit_dir=False
try:
if device.attributes['partition'] == b"1":
partition = device.sys_name
for parent_dev in device.ancestors:
try:
model = parent_dev.attributes['model']
if model == b'Edge 800 SD Card':
print("Edge 800 SD card partition found: %s" % partition)
print("Waiting for the slow ass Garmin to get mounted")
#Attempt to mount the SDCARD partition and keep trying to find the .fit files
#Doing this in a loop because the SDCARD may get auto mounted before we mount it
#We keep checking if the .fit files exist regardless of how it got mounted
while(fit_dir==False):
fit_dir = find_fits(partition)
try:
if mount_sdcard(partition):
fit_dir = find_fits(partition)
break
except:
raise
else:
time.sleep(1)
if fit_dir:
print ("Found .fit directory: %s" % fit_dir)
print ("Using Strava token: %s" % upload_fits.token)
upload_fits(fit_dir, partition)
return
except:
pass
except:
return False
def mount_sdcard(partition):
tmpdir = '/tmp/strava-upload-sdcard'
if not os.path.exists(tmpdir):
print("Creating temporary mount directory")
mkdir_result = subprocess.call(['mkdir %s' % tmpdir], shell=True)
if mkdir_result != 0:
print ("Failed to create directory: %s" % tmpdir)
return False
print ("Attempting to mount SDCARD partition (%s)" % partition)
mount_result = subprocess.call(['mount /dev/%s %s' % (partition, tmpdir)], shell=True)
if mount_result != 0:
print ("Failed to mount /dev/%s - probably don't have permissions to" % partition)
return False
return True
def find_fits(partition):
re_mnt = re.compile("%s\s(.*?)\s" % partition)
garmin_path = "/Garmin/Activities/"
try:
mtab = open("/proc/mounts").read()
mountpoint = re_mnt.search(mtab).group(1)
except:
#print("Failed to find mount point for: %s" % partition)
return False
try:
fitfile = glob.glob(mountpoint + garmin_path + "*.fit")[0]
return mountpoint+garmin_path
except:
#print("Did not find any .fit files on: %s" % mountpoint + garmin_path)
return False
def strava_check_create_tables():
""" Create tables for the database, these should always be up to date """
conn = sqlite3.connect('stravatoken.sqlite')
c = conn.cursor()
tables = {
'tokens': "CREATE TABLE tokens (token TEXT UNIQUE ON CONFLICT REPLACE)"
}
# Go through each table and check if it exists, if it doesn't, run the SQL statement to create it.
for (table_name, sql_statement) in tables.items():
query = "SELECT name FROM sqlite_master WHERE type='table' AND name=:table_name"
if not c.execute(query, {'table_name': table_name}).fetchone():
# Run the command.
c.execute(sql_statement)
conn.commit()
c.close()
def strava_get_token():
""" Get an token by user """
conn = sqlite3.connect('stravatoken.sqlite')
c = conn.cursor()
query = "SELECT token FROM tokens"
try:
result = c.execute(query).fetchone()
except:
return False
if (result):
c.close()
return result[0]
else:
c.close()
return False
def strava_delete_token(token):
""" Delete a user's token from the token table """
conn = sqlite3.connect('stravatoken.sqlite')
c = conn.cursor()
query = "DELETE FROM tokens WHERE token = :token"
c.execute(query, {'token': token})
conn.commit()
c.close()
# Web server stuff
class webServer:
def __init__(self,strava_client_secret, strava_client_id): #Get the strava client ID/secrets for the Oauth exchange later
self.strava_client_secret = strava_client_secret
self.strava_client_id = strava_client_id
@cherrypy.expose
def strava_token_exchange(self,state=None,code=None):
if code:
params = urllib.parse.urlencode({'client_id': self.strava_client_id, 'client_secret': self.strava_client_secret, 'code': code})
params = params.encode('utf-8')
req = urllib.request.Request("https://www.strava.com/oauth/token")
req.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
try:
response = urllib.request.urlopen(req,params)
response = json.loads(response.read().decode('utf-8'))
self.strava_insert_token(response['access_token'])
cherrypy.engine.exit()
return "Token exchange completed successfully. Shutting down the web service. You can close this window now."
except:
cherrypy.engine.exit()
return "Token exchange with Strava failed. Restart the script and try again."
else:
cherrypy.engine.exit()
return "Invalid or empty access code received from Strava. Restart the script and try again."
#strava_token_exchange._cp_config = {'response.stream': True}
def strava_insert_token(self,token):
""" Insert a user's strava token into the token table """
conn = sqlite3.connect('stravatoken.sqlite')
c = conn.cursor()
query = "INSERT INTO tokens VALUES (:token)"
c.execute(query, {'token': token})
conn.commit()
c.close()
# End Web server stuff
def store_filename(filename):
conn = sqlite3.connect('uploads.sqlite')
c = conn.cursor()
result = c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='uploads';").fetchone()
if not result:
c.execute('''create table uploads(filename text)''')
c.execute("INSERT INTO uploads VALUES (?)" , [filename])
conn.commit()
conn.close()
def hasbeen_uploaded(filename):
conn = sqlite3.connect('uploads.sqlite')
c = conn.cursor()
result = len(c.execute("SELECT filename FROM uploads WHERE filename = ?", (filename,)).fetchall())
if result > 0:
return True
else:
return False
def upload_fits(fit_dir, partition):
skipped = 0
upload_files = []
fitfiles = glob.glob("%s/*.fit" % fit_dir)
for fitfile in fitfiles:
if not hasbeen_uploaded(os.path.basename(fitfile)):
upload_files.append(fitfile)
else:
skipped +=1
if len(upload_files)>0:
for fitfile in upload_files:
upload_id, filename = upload_fit_file(upload_fits.token, fitfile)
if upload_id:
thread = threading.Thread(target=get_upload_status, args=(upload_fits.token,upload_id, filename))
thread.start()
while threading.activeCount() > 2: # wait for all files to finish processing
pass
else: #nothing to upload
print ("Nothing to upload.")
print ("%s files skipped." % skipped)
#unmount the SD card partition
print ("Unmounting SD card: /dev/%s" % partition)
unmount_result = subprocess.call(['umount /dev/%s' % partition], shell=True)
if mount_result != 0:
print ("Failed to unmount /dev/%s - probably don't have permissions to" % partition)
def upload_fit_file(token, fitfile):
fitdata = open(fitfile,"rb").read()
headers = {'Authorization': 'Bearer ' + token}
data = {'file': (fitfile, fitdata), "data_type": (None,"fit")}
try:
res= requests.post("http://www.strava.com/api/v3/uploads", files=data, headers=headers)
upload_id = res.json()['id']
print("Uploaded file: %s" % fitfile)
except:
print("There was an error uploading the file")
print(traceback.format_exc())
return False, fitfile
return upload_id, fitfile
def get_upload_status(token, upload_id, filename):
start_time = time.time()
while 1:
headers = {'Authorization': 'Bearer ' + token}
res = requests.get("https://www.strava.com/api/v3/uploads/%s" % upload_id, headers=headers)
status = res.json()['status']
error = res.json()['error']
if error:
print("Error: %s" % error)
if error.find("duplicate") != -1:
store_filename(os.path.basename(filename))
return
elif status == "Your activity is ready.":
store_filename(os.path.basename(filename))
print("%s successfully uploaded and processed in %s seconds." % (os.path.basename(filename),round(time.time()-start_time,2)))
return
time.sleep(1)
#override print function to add timestamps
def print(s, **kwargs):
__builtins__.print(time.strftime("[%b %d %H:%M:%S] ") + s, **kwargs)
class simpleLogger():
def __init__(self,logfile):
self.logfile = logfile
open(logfile,"w").write("") ##clear out any previous contents
def write(self,logtext):
logfile = open(self.logfile,"a")
logfile.write(logtext)
logfile.close()
return 0
def flush(self):
return 0
def main_loop():
startup()
while 1:
time.sleep(0.1)
if __name__ == "__main__":
try:
main_loop()
except KeyboardInterrupt:
sys.exit(0)