gets a tcx for new workouts
This commit is contained in:
@@ -118,8 +118,11 @@ def make_authorization_url():
|
||||
|
||||
return HttpResponseRedirect(url)
|
||||
|
||||
def get_polar_workout_list(user):
|
||||
def get_polar_workouts(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
|
||||
exercise_list = []
|
||||
|
||||
if (r.polartoken == '') or (r.polartoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
@@ -131,19 +134,52 @@ def get_polar_workout_list(user):
|
||||
headers = {'Authorization':authorizationstring,
|
||||
'Accept': 'application/json'}
|
||||
|
||||
headers2 = {
|
||||
'Authorization':authorizationstring,
|
||||
}
|
||||
|
||||
url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions'.format(
|
||||
userid = r.polaruserid
|
||||
)
|
||||
|
||||
# url = 'https://polaraccesslink.com/v3/users/{userid}/activity-transactions'.format(
|
||||
# userid = r.polaruserid
|
||||
# )
|
||||
|
||||
print url
|
||||
|
||||
response = requests.post(url, headers=headers)
|
||||
print url
|
||||
print r.polartoken
|
||||
print response.status_code
|
||||
|
||||
return response
|
||||
if response.status_code == 201:
|
||||
transactionid = response.json()['transaction-id']
|
||||
url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions/{transactionid}'.format(
|
||||
transactionid = transactionid,
|
||||
userid = r.polaruserid
|
||||
)
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
exerciseurls = response.json()['exercises']
|
||||
for exerciseurl in exerciseurls:
|
||||
response = requests.get(exerciseurl,headers=headers)
|
||||
if response.status_code == 200:
|
||||
exercise_dict = response.json()
|
||||
tcxuri = exerciseurl+'/tcx'
|
||||
response = requests.get(tcxuri,headers=headers2)
|
||||
if response.status_code == 200:
|
||||
filename = 'media/polarimport{id}.tcx'.format(
|
||||
id = exercise_dict['id']
|
||||
)
|
||||
with open(filename,'w') as fop:
|
||||
fop.write(response.text)
|
||||
exercise_dict['filename'] = filename
|
||||
else:
|
||||
exercise_dict['filename'] = None
|
||||
|
||||
exercise_list.append(exercise_dict)
|
||||
|
||||
# commit transaction
|
||||
requests.put(url, headers=headers)
|
||||
|
||||
return exercise_list
|
||||
|
||||
def get_polar_user_info(user,physical=False):
|
||||
r = Rower.objects.get(user=user)
|
||||
@@ -160,6 +196,7 @@ def get_polar_user_info(user,physical=False):
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
|
||||
|
||||
params = {
|
||||
'user-id': r.polaruserid
|
||||
}
|
||||
@@ -174,12 +211,78 @@ def get_polar_user_info(user,physical=False):
|
||||
)
|
||||
|
||||
|
||||
print url
|
||||
|
||||
if physical:
|
||||
response = requests.post(url, headers=headers)
|
||||
else:
|
||||
response = requests.get(url, headers=headers)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def get_polar_workout(user,id,transactionid):
|
||||
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.polartoken == '') or (r.polartoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
elif (timezone.now()>r.polartokenexpirydate):
|
||||
s = "Token expired. Needs to refresh"
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
authorizationstring = str('Bearer ' + r.polartoken)
|
||||
headers = {
|
||||
'Authorization':authorizationstring,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
|
||||
|
||||
url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions'.format(
|
||||
userid = r.polaruserid
|
||||
)
|
||||
|
||||
|
||||
response = requests.post(url, headers=headers)
|
||||
|
||||
if response.status_code == 201:
|
||||
transactionid = response.json()['transaction-id']
|
||||
url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions/{transactionid}'.format(
|
||||
transactionid = transactionid,
|
||||
userid = r.polaruserid
|
||||
)
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
exerciseurls = response.json()['exercises']
|
||||
for exerciseurl in exerciseurls:
|
||||
response = requests.gedt(exerciseurl,headers=headers)
|
||||
if response.status_code == 200:
|
||||
exercise_dict = response.json()
|
||||
thisid = exercise_dict['id']
|
||||
if thisid == id:
|
||||
url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions/{transactionid}/exercises/{exerciseid}/tcx'.format(
|
||||
userid = r.polaruserid,
|
||||
transactionid = transactionid,
|
||||
exerciseid = id
|
||||
)
|
||||
|
||||
print "Get TCX"
|
||||
print url
|
||||
response = requests.get(url,headers = headers2)
|
||||
print response.status_code
|
||||
|
||||
if response.status_code == 200:
|
||||
print response.text
|
||||
|
||||
result = response.text
|
||||
# commit transaction
|
||||
response = requests.put(url,headers=headers)
|
||||
else:
|
||||
result = None
|
||||
|
||||
return result
|
||||
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user