-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathexport.py
69 lines (55 loc) · 1.87 KB
/
export.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
#!/usr/bin/env python
from lib.endomondo import Endomondo
import lib.tcx as tcx
import re
import getpass
import sys
import os
# create a somewhat useful filename for the specified workout
def create_filename(workout):
ret = ''
if workout.start_time:
ret = workout.start_time.strftime("%Y%m%d") + "_"
ret += str(workout.id)
name = workout.name
if name:
name = re.sub(r'[\[\]/\\;,><&*:%=+@!#\(\)\|\?\^]', '', name)
name = re.sub(r"[' \t]", '_', name)
ret += "_" + name
ret += ".tcx"
return ret
# create a new directory to store the exported files in
def create_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
# create the TCX file for the specified workout
def create_tcx_file(workout):
directory_name = 'export'
activity = workout.get_activity()
name = create_filename(workout)
create_directory(directory_name)
filename = os.path.join(directory_name, name)
print "writing %s, %s, %s trackpoints" % (filename, activity.sport, len(activity.trackpoints))
writer = tcx.Writer()
tcxfile = writer.write(activity)
if tcxfile:
with open(filename, 'w') as f:
f.write(tcxfile)
def main():
try:
print "Endomondo: export most recent workouts as TCX files"
email = raw_input("Email: ")
password = getpass.getpass()
maximum_workouts = raw_input("Maximum number of workouts (press Enter to ignore)")
endomondo = Endomondo(email, password)
workouts = endomondo.get_workouts(maximum_workouts)
print "fetched latest", len(workouts), "workouts"
for workout in workouts:
create_tcx_file(workout)
print "done."
return 0
except ValueError, exception:
sys.stderr.write(str(exception) + "\n")
return 1
if __name__ == "__main__":
sys.exit(main())