Private
Public Access
1
0

working, downloading tcx

This commit is contained in:
Sander Roosendaal
2018-06-05 21:54:47 +02:00
parent 6b03bc26c6
commit 278e99a38c
4 changed files with 31 additions and 21 deletions

View File

@@ -34,9 +34,13 @@ from rowers.models import checkworkoutuser
import dataprep
from dataprep import columndict
from io import StringIO
import stravalib
from stravalib.exc import ActivityUploadFailed,TimeoutExceeded
from django_mailbox.models import Message,Mailbox,MessageAttachment
from rowsandall_app.settings import (
POLAR_CLIENT_ID, POLAR_REDIRECT_URI, POLAR_CLIENT_SECRET,
)
@@ -148,6 +152,9 @@ def get_polar_workouts(user):
if response.status_code == 201:
workoutsbox = Mailbox.objects.filter(name='workouts')[0]
transactionid = response.json()['transaction-id']
url = baseurl+'/users/{userid}/exercise-transactions/{transactionid}'.format(
transactionid = transactionid,
@@ -157,26 +164,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']
)
print 'downloading workout {id} to {filename}'.format(
filename=filename,
filename = 'media/mailbox_attachments/polarimport{id}.tcx'.format(
id = exercise_dict['id']
)
with open(filename,'wb') as fop:
fop.write(response.text)
fop.write(response.content)
msg = Message(mailbox=workoutsbox,
from_header=user.email,
subject = 'Import from Polar Flow')
msg.save()
a = MessageAttachment(message=msg,document=filename)
a.save()
exercise_dict['filename'] = filename
else:
exercise_dict['filename'] = ''