c2 api calls now per own api
This commit is contained in:
@@ -22,14 +22,17 @@ import json
|
||||
from json.decoder import JSONDecodeError
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET
|
||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
||||
UPLOAD_SERVICE_URL, UPLOAD_SERVICE_SECRET
|
||||
)
|
||||
|
||||
from rowers.tasks import handle_c2_import_stroke_data, handle_c2_sync
|
||||
from rowers.tasks import (
|
||||
handle_c2_import_stroke_data, handle_c2_sync, handle_c2_async_workout,
|
||||
)
|
||||
import django_rq
|
||||
queue = django_rq.get_queue('default')
|
||||
queuelow = django_rq.get_queue('low')
|
||||
queuehigh = django_rq.get_queue('low')
|
||||
queuehigh = django_rq.get_queue('high')
|
||||
from rowers.utils import myqueue
|
||||
from rowers.models import C2WorldClassAgePerformance
|
||||
|
||||
@@ -185,8 +188,15 @@ def get_c2_workouts(rower):
|
||||
newids = [c2id for c2id in c2ids if not c2id in knownc2ids]
|
||||
|
||||
for c2id in newids:
|
||||
workoutid = create_async_workout(alldata,
|
||||
rower.user,c2id)
|
||||
res = myqueue(queuehigh,
|
||||
handle_c2_async_workout,
|
||||
alldata,
|
||||
rower.user.id,
|
||||
rower.c2token,
|
||||
c2id,
|
||||
)
|
||||
#workoutid = create_async_workout(alldata,
|
||||
# rower.user,c2id)
|
||||
|
||||
|
||||
return 1
|
||||
@@ -203,6 +213,16 @@ def create_async_workout(alldata,user,c2id):
|
||||
startdatetime = iso8601.parse_date(data['date'])
|
||||
weightclass = data['weight_class']
|
||||
|
||||
try:
|
||||
title = data['name']
|
||||
except KeyError:
|
||||
title = ""
|
||||
try:
|
||||
t = data['comments'].split('\n', 1)[0]
|
||||
title += t[:40]
|
||||
except:
|
||||
title = 'Imported'
|
||||
|
||||
weightcategory = 'hwt'
|
||||
if weightclass == "L":
|
||||
weightcategory = 'lwt'
|
||||
@@ -234,31 +254,120 @@ def create_async_workout(alldata,user,c2id):
|
||||
|
||||
r = Rower.objects.get(user=user)
|
||||
|
||||
authorizationstring = str('Bearer ' + r.c2token)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
'Content-Type': 'application/json'}
|
||||
url2 = "https://log.concept2.com/api/users/me/results"+str(c2id)
|
||||
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)+"/strokes"
|
||||
try:
|
||||
s = requests.get(url,headers=headers)
|
||||
except ConnectionError:
|
||||
return 0
|
||||
|
||||
w = Workout(
|
||||
user=r,
|
||||
workouttype = workouttype,
|
||||
name = name,
|
||||
date = workoutdate,
|
||||
starttime = starttime,
|
||||
startdatetime = startdatetime,
|
||||
timezone = timezone_str,
|
||||
duration = duration,
|
||||
distance=distance,
|
||||
weightcategory = weightcategory,
|
||||
uploadedtoc2 = c2id,
|
||||
csvfilename = csvfilename,
|
||||
notes = notes
|
||||
)
|
||||
if s.status_code != 200:
|
||||
return 0
|
||||
|
||||
w.save()
|
||||
strokedata = pd.DataFrame.from_dict(s.json()['data'])
|
||||
|
||||
# Check if workout has stroke data, and get the stroke data
|
||||
res = make_cumvalues(0.1*strokedata['t'])
|
||||
cum_time = res[0]
|
||||
lapidx = res[1]
|
||||
|
||||
result = add_stroke_data(user,c2id,w.id,startdatetime,csvfilename,
|
||||
workouttype = w.workouttype)
|
||||
starttimeunix = arrow.get(startdatetime).timestamp
|
||||
|
||||
unixtime = cum_time+starttimeunix
|
||||
# unixtime[0] = starttimeunix
|
||||
seconds = 0.1*strokedata.loc[:,'t']
|
||||
|
||||
nr_rows = len(unixtime)
|
||||
|
||||
try:
|
||||
latcoord = strokedata.loc[:,'lat']
|
||||
loncoord = strokedata.loc[:,'lon']
|
||||
except:
|
||||
latcoord = np.zeros(nr_rows)
|
||||
loncoord = np.zeros(nr_rows)
|
||||
|
||||
|
||||
try:
|
||||
strokelength = strokedata.loc[:,'strokelength']
|
||||
except:
|
||||
strokelength = np.zeros(nr_rows)
|
||||
|
||||
dist2 = 0.1*strokedata.loc[:,'d']
|
||||
|
||||
try:
|
||||
spm = strokedata.loc[:,'spm']
|
||||
except KeyError:
|
||||
spm = 0*dist2
|
||||
|
||||
try:
|
||||
hr = strokedata.loc[:,'hr']
|
||||
except KeyError:
|
||||
hr = 0*spm
|
||||
|
||||
pace = strokedata.loc[:,'p']/10.
|
||||
pace = np.clip(pace,0,1e4)
|
||||
pace = pace.replace(0,300)
|
||||
|
||||
velo = 500./pace
|
||||
power = 2.8*velo**3
|
||||
if workouttype == 'bike':
|
||||
velo = 1000./pace
|
||||
|
||||
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
|
||||
' Horizontal (meters)': dist2,
|
||||
' Cadence (stokes/min)':spm,
|
||||
' HRCur (bpm)':hr,
|
||||
' longitude':loncoord,
|
||||
' latitude':latcoord,
|
||||
' Stroke500mPace (sec/500m)':pace,
|
||||
' Power (watts)':power,
|
||||
' DragFactor':np.zeros(nr_rows),
|
||||
' DriveLength (meters)':np.zeros(nr_rows),
|
||||
' StrokeDistance (meters)':strokelength,
|
||||
' DriveTime (ms)':np.zeros(nr_rows),
|
||||
' StrokeRecoveryTime (ms)':np.zeros(nr_rows),
|
||||
' AverageDriveForce (lbs)':np.zeros(nr_rows),
|
||||
' PeakDriveForce (lbs)':np.zeros(nr_rows),
|
||||
' lapIdx':lapidx,
|
||||
' WorkoutState': 4,
|
||||
' ElapsedTime (sec)':seconds,
|
||||
'cum_dist': dist2
|
||||
})
|
||||
|
||||
|
||||
df.sort_values(by='TimeStamp (sec)',ascending=True)
|
||||
|
||||
res = df.to_csv(csvfilename,index_label='index',
|
||||
compression='gzip')
|
||||
|
||||
userid = r.user.id
|
||||
|
||||
uploadoptions = {
|
||||
'secret':UPLOAD_SERVICE_SECRET,
|
||||
'user':userid,
|
||||
'file': csvfilename,
|
||||
'title': title,
|
||||
'workouttype':workouttype,
|
||||
'boattype':'1x',
|
||||
'c2id':c2id,
|
||||
}
|
||||
|
||||
session = requests.session()
|
||||
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
||||
session.headers.update(newHeaders)
|
||||
|
||||
response = session.post(UPLOAD_SERVICE_URL,json=uploadoptions)
|
||||
|
||||
if response.status_code != 200:
|
||||
return 0
|
||||
|
||||
workoutid = response.json()['id']
|
||||
|
||||
return workoutid
|
||||
|
||||
return w.id
|
||||
|
||||
# convert datetime object to seconds
|
||||
def makeseconds(t):
|
||||
@@ -800,7 +909,7 @@ def get_c2_workout_list(user,page=1):
|
||||
return custom_exception_handler(401,s)
|
||||
elif (timezone.now()>r.tokenexpirydate):
|
||||
s = "Token expired. Needs to refresh."
|
||||
|
||||
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
# ready to fetch. Hurray
|
||||
|
||||
Reference in New Issue
Block a user