fixed
This commit is contained in:
142
rowers/tasks.py
142
rowers/tasks.py
@@ -82,148 +82,6 @@ def add(x, y):
|
||||
return x + y
|
||||
|
||||
|
||||
@app.task
|
||||
def handle_strava_import_stroke_data(title,
|
||||
useremail,
|
||||
stravatoken,
|
||||
stravaid,
|
||||
starttimeunix,
|
||||
csvfilename,debug=True,**kwargs):
|
||||
# ready to fetch. Hurray
|
||||
fetchresolution = 'high'
|
||||
series_type = 'time'
|
||||
authorizationstring = str('Bearer ' + stravatoken)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
'Content-Type': 'application/json',
|
||||
'resolution': 'medium',}
|
||||
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)
|
||||
workoutsummary = requests.get(url,headers=headers).json()
|
||||
|
||||
workoutsummary['timezone'] = "Etc/UTC"
|
||||
startdatetime = workoutsummary['start_date']
|
||||
|
||||
r = type('Rower', (object,), {"stravatoken": stravatoken})
|
||||
|
||||
spmjson = get_strava_stream(r,'cadence',stravaid)
|
||||
hrjson = get_strava_stream(r,'heartrate',stravaid)
|
||||
timejson = get_strava_stream(r,'time',stravaid)
|
||||
velojson = get_strava_stream(r,'velocity_smooth',stravaid)
|
||||
distancejson = get_strava_stream(r,'distance',stravaid)
|
||||
latlongjson = get_strava_stream(r,'latlng',stravaid)
|
||||
wattsjson = get_strava_stream(r,'watts',stravaid)
|
||||
|
||||
try:
|
||||
t = np.array(timejson.json()[0]['data'])
|
||||
nr_rows = len(t)
|
||||
d = np.array(distancejson.json()[1]['data'])
|
||||
if nr_rows == 0:
|
||||
return 0
|
||||
except IndexError:
|
||||
d = 0*t
|
||||
# return (0,"Error: No Distance information in the Strava data")
|
||||
except KeyError:
|
||||
return 0
|
||||
|
||||
try:
|
||||
spm = np.array(spmjson.json()[1]['data'])
|
||||
except:
|
||||
spm = np.zeros(nr_rows)
|
||||
|
||||
try:
|
||||
watts = np.array(wattsjson.json()[1]['data'])
|
||||
except:
|
||||
watts = np.zeros(nr_rows)
|
||||
|
||||
try:
|
||||
hr = np.array(hrjson.json()[1]['data'])
|
||||
except IndexError:
|
||||
hr = np.zeros(nr_rows)
|
||||
except KeyError:
|
||||
hr = np.zeros(nr_rows)
|
||||
|
||||
try:
|
||||
velo = np.array(velojson.json()[1]['data'])
|
||||
except IndexError:
|
||||
velo = np.zeros(nr_rows)
|
||||
except KeyError:
|
||||
velo = np.zeros(nr_rows)
|
||||
|
||||
f = np.diff(t).mean()
|
||||
if f != 0:
|
||||
windowsize = 2*(int(10./(f)))+1
|
||||
else:
|
||||
windowsize = 1
|
||||
|
||||
if windowsize > 3 and windowsize < len(velo):
|
||||
velo2 = savgol_filter(velo,windowsize,3)
|
||||
else:
|
||||
velo2 = velo
|
||||
|
||||
coords = np.array(latlongjson.json()[0]['data'])
|
||||
try:
|
||||
lat = coords[:,0]
|
||||
lon = coords[:,1]
|
||||
except IndexError:
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
except KeyError:
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
|
||||
strokelength = velo*60./(spm)
|
||||
strokelength[np.isinf(strokelength)] = 0.0
|
||||
|
||||
pace = 500./(1.0*velo2)
|
||||
pace[np.isinf(pace)] = 0.0
|
||||
|
||||
unixtime = starttimeunix+t
|
||||
|
||||
strokedistance = 60.*velo2/spm
|
||||
|
||||
nr_strokes = len(t)
|
||||
|
||||
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
|
||||
' ElapsedTime (sec)':t,
|
||||
' Horizontal (meters)':d,
|
||||
' Stroke500mPace (sec/500m)':pace,
|
||||
' Cadence (stokes/min)':spm,
|
||||
' HRCur (bpm)':hr,
|
||||
' latitude':lat,
|
||||
' longitude':lon,
|
||||
' StrokeDistance (meters)':strokelength,
|
||||
'cum_dist':d,
|
||||
' DragFactor':np.zeros(nr_strokes),
|
||||
' DriveLength (meters)':np.zeros(nr_strokes),
|
||||
' StrokeDistance (meters)':strokedistance,
|
||||
' DriveTime (ms)':np.zeros(nr_strokes),
|
||||
' StrokeRecoveryTime (ms)':np.zeros(nr_strokes),
|
||||
' AverageDriveForce (lbs)':np.zeros(nr_strokes),
|
||||
' PeakDriveForce (lbs)':np.zeros(nr_strokes),
|
||||
' lapIdx':np.zeros(nr_strokes),
|
||||
' Power (watts)':watts,
|
||||
})
|
||||
|
||||
|
||||
df.sort_values(by='TimeStamp (sec)',ascending=True)
|
||||
|
||||
res = df.to_csv(csvfilename,index_label='index')
|
||||
|
||||
d = {
|
||||
'stravaid':stravaid
|
||||
}
|
||||
|
||||
res = send_template_email(useremail,[workoutemailbox],
|
||||
title,'workoutemail.html',
|
||||
d,
|
||||
attach_file=csvfilename)
|
||||
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
os.remove(csvfilename)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
@app.task
|
||||
|
||||
Reference in New Issue
Block a user