From 6b03bc26c6be1d556232d7036c90cce04a5adaeb Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Tue, 5 Jun 2018 16:43:58 +0200 Subject: [PATCH] changed to v3-example for debugging --- rowers/polarstuff.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/rowers/polarstuff.py b/rowers/polarstuff.py index 3efd6d3d..4c718145 100644 --- a/rowers/polarstuff.py +++ b/rowers/polarstuff.py @@ -41,6 +41,8 @@ from rowsandall_app.settings import ( POLAR_CLIENT_ID, POLAR_REDIRECT_URI, POLAR_CLIENT_SECRET, ) +baseurl = 'https://polaraccesslink.com/v3-example' + # Custom exception handler, returns a 401 HTTP message # with exception details in the json data def custom_exception_handler(exc,message): @@ -114,7 +116,6 @@ def make_authorization_url(): import urllib url = "https://flow.polar.com/oauth2/authorization" +urllib.urlencode(params) - print url return HttpResponseRedirect(url) @@ -138,19 +139,17 @@ def get_polar_workouts(user): 'Authorization':authorizationstring, } - url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions'.format( + url = baseurl+'/users/{userid}/exercise-transactions'.format( userid = r.polaruserid ) response = requests.post(url, headers=headers) - print url - print r.polartoken - print response.status_code + if response.status_code == 201: transactionid = response.json()['transaction-id'] - url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions/{transactionid}'.format( + url = baseurl+'/users/{userid}/exercise-transactions/{transactionid}'.format( transactionid = transactionid, userid = r.polaruserid ) @@ -158,21 +157,29 @@ def get_polar_workouts(user): response = requests.get(url, headers=headers) if response.status_code == 200: exerciseurls = response.json()['exercises'] + print 'Found {n} workouts'.format( + n=len(exerciseurls) + ) for exerciseurl in exerciseurls: response = requests.get(exerciseurl,headers=headers) if response.status_code == 200: exercise_dict = response.json() + print 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: + print 'downloading workout {id} to {filename}'.format( + filename=filename, + id = exercise_dict['id'] + ) + with open(filename,'wb') as fop: fop.write(response.text) exercise_dict['filename'] = filename else: - exercise_dict['filename'] = None + exercise_dict['filename'] = '' exercise_list.append(exercise_dict) @@ -202,7 +209,7 @@ def get_polar_user_info(user,physical=False): } if not physical: - url = 'https://polaraccesslink.com/v3/users/{userid}'.format( + url = baseurl+'/users/{userid}'.format( userid = r.polaruserid ) else: @@ -236,7 +243,7 @@ def get_polar_workout(user,id,transactionid): } - url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions'.format( + url = baseurl+'/users/{userid}/exercise-transactions'.format( userid = r.polaruserid ) @@ -245,7 +252,7 @@ def get_polar_workout(user,id,transactionid): if response.status_code == 201: transactionid = response.json()['transaction-id'] - url = 'https://polaraccesslink.com/v3/users/{userid}/exercise-transactions/{transactionid}'.format( + url = baseurl+'/users/{userid}/exercise-transactions/{transactionid}'.format( transactionid = transactionid, userid = r.polaruserid ) @@ -259,16 +266,13 @@ def get_polar_workout(user,id,transactionid): 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( + url = baseurl+'/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