Private
Public Access
1
0

incomplete version of garmin training payload

This commit is contained in:
Sander Roosendaal
2021-03-01 21:31:25 +01:00
parent f2ec2585cc
commit f1379bfcf5
2 changed files with 172 additions and 1 deletions

View File

@@ -132,7 +132,7 @@ def get_garmin_file(r,callbackURL,starttime,fileType):
def get_garmin_workout_list(user):
r = Rower.objects.get(user=user)
if (r.garmintoken == '') or (r.stravatoken is None):
if (r.garmintoken == '') or (r.garmintoken is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
@@ -148,6 +148,111 @@ def get_garmin_workout_list(user):
return result
def garmin_can_export_session(user):
result = get_garmin_permissions(user)
if 'WORKOUT_IMPORT' in result:
return True
return False
from rowers import utils
def step_to_garmin(step):
out = {
'type': step['type']
}
try:
steps = step['steps']
lijst = []
for s in steps:
sout = step_to_garmin(s)
lijst.append(sout)
out['steps'] = lijst
except KeyError:
pass
return out
def ps_to_garmin(ps):
payload = {
'workoutName': ps.name,
'sport': 'GENERIC',
'description':'Uploaded from Rowsandall.com',
'estimatedDurationInSecs':60*ps.approximate_duration,
'estimatedDistanceInMeters': ps.approximate_distance,
'workoutProvider': 'Rowsandall.com',
'workoutSourceId': 'Rowsandall.com',
}
steps = []
steplist = utils.ps_dict_order_dict(ps.steps)
while steplist:
step, steplist = utils.peel(steplist)
steps.append(step)
steps.append(step)
lijst = []
for step in steps:
lijst.append(step_to_garmin(step))
payload['steps'] = lijst
return payload
def get_garmin_permissions(user):
r = Rower.objects.get(user=user)
if (r.garmintoken == '') or (r.garmintoken is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
garmin = OAuth1Session(oauth_data['client_id'],
client_secret=oauth_data['client_secret'],
resource_owner_key=r.garmintoken,
resource_owner_secret=r.garminrefreshtoken,
)
url = 'https://apis.garmin.com/userPermissions/'
result = garmin.get(url)
if result.status_code == 200:
return result.json()
return []
def garmin_session_create(ps,user):
if not ps.steps:
return 0
if not garmin_can_export_session(user):
return 0
garmindict = ps_to_garmin(ps)
r = Rower.objects.get(user=user)
if (r.garmintoken == '') or (r.garmintoken is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
garmin = OAuth1Session(oauth_data['client_id'],
client_secret=oauth_data['client_secret'],
resource_owner_key=r.garmintoken,
resource_owner_secret=r.garminrefreshtoken,
)
url = 'http://apis.garmin.com/training-api/schedule/'
response = garmin.post(url,data=garmindict)
if response.status_code != 200:
return 0
return response.json()['workoutId']
def garmin_getworkout(garminid,r,activity):
starttime = activity['startTimeInSeconds']
startdatetime = arrow.get(starttime)