passing tests
This commit is contained in:
@@ -382,118 +382,9 @@ def async_get_workout(user,stravaid):
|
||||
return job
|
||||
|
||||
# Get a Strava workout summary data and stroke data by ID
|
||||
def get_workout(user,stravaid,do_async=False):
|
||||
if do_async: # pragma: no cover
|
||||
res = async_get_workout(user,stravaid)
|
||||
return {},pd.DataFrame()
|
||||
try:
|
||||
thetoken = strava_open(user)
|
||||
except NoTokenError: # pragma: no cover
|
||||
s = "Token error"
|
||||
return custom_exception_handler(401,s)
|
||||
def get_workout(user,stravaid,do_async=True):
|
||||
return async_get_workout(user,stravaid)
|
||||
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate): # pragma: no cover
|
||||
s = "Token expired. Needs to refresh."
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
# ready to fetch. Hurray
|
||||
fetchresolution = 'high'
|
||||
series_type = 'time'
|
||||
authorizationstring = str('Bearer ' + r.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"
|
||||
try:
|
||||
startdatetime = workoutsummary['start_date']
|
||||
except KeyError: # pragma: no cover
|
||||
startdatetime = timezone.now()
|
||||
|
||||
spm = get_strava_stream(r,'cadence',stravaid)
|
||||
hr = get_strava_stream(r,'heartrate',stravaid)
|
||||
t = get_strava_stream(r,'time',stravaid)
|
||||
velo = get_strava_stream(r,'velocity_smooth',stravaid)
|
||||
d = get_strava_stream(r,'distance',stravaid)
|
||||
coords = get_strava_stream(r,'latlng',stravaid)
|
||||
power = get_strava_stream(r,'watts',stravaid)
|
||||
|
||||
if t is not None:
|
||||
nr_rows = len(t)
|
||||
else: # pragma: no cover
|
||||
duration = int(workoutsummary['elapsed_time'])
|
||||
t = pd.Series(range(duration+1))
|
||||
|
||||
nr_rows = len(t)
|
||||
|
||||
|
||||
if nr_rows == 0: # pragma: no cover
|
||||
return (0,"Error: Time data had zero length")
|
||||
|
||||
if d is None: # pragma: no cover
|
||||
d = 0*t
|
||||
|
||||
if spm is None: # pragma: no cover
|
||||
spm = np.zeros(nr_rows)
|
||||
|
||||
if power is None: # pragma: no cover
|
||||
power = np.zeros(nr_rows)
|
||||
|
||||
if hr is None: # pragma: no cover
|
||||
hr = np.zeros(nr_rows)
|
||||
|
||||
if velo is None: # pragma: no cover
|
||||
velo = np.zeros(nr_rows)
|
||||
|
||||
dt = np.diff(t).mean()
|
||||
wsize = round(5./dt)
|
||||
|
||||
velo2 = ewmovingaverage(velo,wsize)
|
||||
|
||||
if coords is not None:
|
||||
try:
|
||||
lat = coords[:,0]
|
||||
lon = coords[:,1]
|
||||
except IndexError: # pragma: no cover
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
else: # pragma: no cover
|
||||
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
|
||||
|
||||
df = pd.DataFrame({'t':10*t,
|
||||
'd':10*d,
|
||||
'p':10*pace,
|
||||
'spm':spm,
|
||||
'hr':hr,
|
||||
'lat':lat,
|
||||
'lon':lon,
|
||||
'power':power,
|
||||
'strokelength':strokelength,
|
||||
})
|
||||
|
||||
|
||||
|
||||
# startdatetime = datetime.datetime.strptime(startdatetime,"%Y-%m-%d-%H:%M:%S")
|
||||
|
||||
return [workoutsummary,df]
|
||||
|
||||
# Generate Workout data for Strava (a TCX file)
|
||||
def createstravaworkoutdata(w,dozip=True):
|
||||
|
||||
Reference in New Issue
Block a user