Private
Public Access
1
0
This commit is contained in:
Sander Roosendaal
2018-06-29 23:10:19 +02:00
parent 951e707b6e
commit 89cf064f03
2 changed files with 170 additions and 170 deletions

View File

@@ -6,6 +6,7 @@ import cgi
import requests import requests
import requests.auth import requests.auth
import json import json
import yaml
from django.utils import timezone from django.utils import timezone
from datetime import datetime from datetime import datetime
import numpy as np import numpy as np
@@ -15,6 +16,8 @@ import math
from math import sin,cos,atan2,sqrt from math import sin,cos,atan2,sqrt
import os,sys import os,sys
import gzip import gzip
from scipy import optimize
from scipy.signal import savgol_filter
from pytz import timezone as tz,utc from pytz import timezone as tz,utc
@@ -26,6 +29,8 @@ from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django_mailbox.models import Message,Mailbox,MessageAttachment
import django_rq import django_rq
queue = django_rq.get_queue('default') queue = django_rq.get_queue('default')
queuelow = django_rq.get_queue('low') queuelow = django_rq.get_queue('low')
@@ -50,7 +55,7 @@ from iso8601 import ParseError
import pytz import pytz
import arrow import arrow
from rowers.tasks import handle_strava_import_stroke_data
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET
@@ -103,7 +108,7 @@ def make_authorization_url(request):
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
# Get list of workouts available on Strava # Get list of workouts available on Strava
def get_strava_workout_list(user): def get_strava_workout_list(user,limit_n=0):
r = Rower.objects.get(user=user) r = Rower.objects.get(user=user)
if (r.stravatoken == '') or (r.stravatoken is None): if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize" s = "Token doesn't exist. Need to authorize"
@@ -114,8 +119,15 @@ def get_strava_workout_list(user):
headers = {'Authorization': authorizationstring, headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal', 'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'} 'Content-Type': 'application/json'}
url = "https://www.strava.com/api/v3/athlete/activities" url = "https://www.strava.com/api/v3/athlete/activities"
s = requests.get(url,headers=headers)
if limit_n==0:
params = {}
else:
params = {'per_page':limit_n}
s = requests.get(url,headers=headers,params=params)
return s return s
@@ -126,7 +138,7 @@ def get_strava_workouts(rower):
if not isprorower(rower): if not isprorower(rower):
return 0 return 0
res = get_strava_workout_list(rower.user) res = get_strava_workout_list(rower.user,limit_n=10)
if (res.status_code != 200): if (res.status_code != 200):
return 0 return 0
@@ -142,6 +154,9 @@ def get_strava_workouts(rower):
]) ])
newids = [stravaid for stravaid in stravaids if not stravaid in knownstravaids] newids = [stravaid for stravaid in stravaids if not stravaid in knownstravaids]
print 'strava ids',stravaids
print newids
for stravaid in newids: for stravaid in newids:
result = create_async_workout(alldata,rower.user,stravaid) result = create_async_workout(alldata,rower.user,stravaid)
@@ -207,34 +222,18 @@ def create_async_workout(alldata,user,stravaid,debug=False):
weightcategory = 'hwt' weightcategory = 'hwt'
# Create CSV file name and save data to CSV file # Create CSV file name and save data to CSV file
csvfilename ='media/{code}_{importid}.csv'.format( csvfilename ='media/mailbox_attachments/{code}_{importid}.csv'.format(
importid=stravaid, importid=stravaid,
code = uuid4().hex[:16] code = uuid4().hex[:16]
) )
# w = Workout(
# user=r,
# workouttype = workouttype,
# name = title,
# date = workoutdate,
# starttime = starttime,
# startdatetime = rowdatetime,
# timezone = thetimezone,
# duration = duration,
# distance=distance,
# weightcategory = weightcategory,
# uploadedtostrava = stravaid,
# csvfilename = csvfilename,
# notes = ''
# )
# Check if workout has stroke data, and get the stroke data # Check if workout has stroke data, and get the stroke data
starttimeunix = arrow.get(rowdatetime).timestamp starttimeunix = arrow.get(rowdatetime).timestamp
job = myqueue(queue, result = handle_strava_import_stroke_data(
handle_strava_import_stroke_data,
title, title,
user.email, user.email,
r.stravatoken, r.stravatoken,
@@ -478,3 +477,146 @@ def workout_strava_upload(user,w):
return message,stravaid return message,stravaid
return message,stravaid return message,stravaid
return message,stravaid return message,stravaid
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')
workoutsbox = Mailbox.objects.filter(name='workouts')[0]
body = 'stravaid {stravaid}'.format(stravaid=stravaid)
msg = Message(mailbox=workoutsbox,
from_header=useremail,
subject=title,
body=body)
msg.save()
a = MessageAttachment(message=msg,document=csvfilename[6:])
a.save()
return res

View File

@@ -82,148 +82,6 @@ def add(x, y):
return 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 @app.task