-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwx_importer.py
70 lines (53 loc) · 2.17 KB
/
pwx_importer.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
from xml.etree import ElementTree as et
from ast import literal_eval
import datetime
import dateutil.parser
from statistics import mean
def open_pwx_and_fix_tags(file_path):
root = et.parse(file_path).getroot()
def fix_tag_name(input_tag):
return str.replace(input_tag, '{http://www.peaksware.com/PWX/1/0}', '')
for child in root.getiterator():
child.tag = fix_tag_name(child.tag)
return root
def get_start_time(pwx):
return dateutil.parser.parse(pwx.find('workout').find('time').text)
def get_workout_data(pwx):
def increment_dates(input_date, seconds_incrementor):
return (input_date + datetime.timedelta(seconds=seconds_incrementor)).isoformat()
start_time = get_start_time(pwx)
sample_params_list = []
sample_params_dict = {}
for sample in pwx.find('workout').findall('sample'):
if sample_params_dict:
sample_params_list.append(sample_params_dict.copy())
sample_params_dict.clear()
for child in sample:
if child.tag == 'timeoffset':
sample_params_dict[child.tag] = str(increment_dates(start_time, literal_eval(child.text)))
elif child.tag != 'extension':
sample_params_dict[child.tag] = str(literal_eval(child.text))
return sample_params_list
def get_activity_type(pwx):
activity_type = pwx.find('workout').find('sportType').text
if activity_type in ("Bike", "Mountain Bike"):
activity_type = "Biking"
elif activity_type == "Run":
activity_type = "Running"
else:
activity_type = "Other"
return activity_type
def get_duration(pwx):
return pwx.find('workout').find('summarydata').find('duration').text
def get_dist(pwx):
try:
dist = pwx.find('workout').find('summarydata').find('dist').text
except:
dist = 0
return dist
def get_avg_heart_rate(hr_list):
avg_hr = mean([int(d['hr']) for d in hr_list if 'hr' in d])
return str(int(avg_hr))
def get_max_heart_rate(hr_list):
max_hr = max([int(d['hr']) for d in hr_list if 'hr' in d])
return str(int(max_hr))