working, downloading tcx
This commit is contained in:
@@ -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'] = ''
|
||||
|
||||
@@ -5,16 +5,18 @@
|
||||
{% block title %}Workouts{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>New Workouts Available on Polar Flow</h1>
|
||||
<h1>New Workouts Imported From Polar Flow</h1>
|
||||
|
||||
<p>Due to a limitation in Polar Flow's API, we can only access new workouts.</p>
|
||||
<p>Due to a limitation in Polar Flow's API, we can only access new workouts. We
|
||||
have imported the following workouts and are now processing them. You will
|
||||
receive email when the workouts are ready. </p>
|
||||
|
||||
{% if workouts %}
|
||||
<div class="grid_12 alpha">
|
||||
<table width="70%" class="listtable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th> Import </th>
|
||||
<th> Imported </th>
|
||||
<th> Date</th>
|
||||
<th> Duration </th>
|
||||
<th> Distance </th>
|
||||
@@ -25,7 +27,12 @@
|
||||
{% for workout in workouts %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="/rowers/workout/polarimport/{{ workout|lookup:'id' }}/{{ workout|lookup:'transactionid' }}">Import</a></td>
|
||||
{% if workout|lookup:'filename' == '' %}
|
||||
NO
|
||||
{% else %}
|
||||
YES
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{ workout|lookup:'starttime' }}</td>
|
||||
<td>{{ workout|lookup:'duration' }} </td>
|
||||
<td>{{ workout|lookup:'distance' }} m</td>
|
||||
|
||||
@@ -329,7 +329,6 @@ urlpatterns = [
|
||||
url(r'^workout/sporttracksimport/(?P<sporttracksid>\d+)/$',views.workout_getsporttracksworkout_view),
|
||||
url(r'^workout/sporttracksimport/all/$',views.workout_getsporttracksworkout_all),
|
||||
url(r'^workout/polarimport/$',views.workout_polarimport_view),
|
||||
url(r'^workout/polarimport/(?P<polarid>\d+)/(?P<transactionid>\d+)$',views.workout_getpolarworkout_view),
|
||||
url(r'^workout/runkeeperimport/$',views.workout_runkeeperimport_view),
|
||||
url(r'^workout/runkeeperimport/(?P<runkeeperid>\d+)/$',views.workout_getrunkeeperworkout_view),
|
||||
url(r'^workout/underarmourimport/$',views.workout_underarmourimport_view),
|
||||
|
||||
@@ -9852,12 +9852,6 @@ def workout_c2import_view(request,page=1,message=""):
|
||||
'page':page,
|
||||
})
|
||||
|
||||
# Import a workout from Polar
|
||||
@login_required()
|
||||
def workout_getpolarworkout_view(request,polarid,transactionid):
|
||||
result = polarstuff.get_polar_workout(request.user,polarid,transactionid)
|
||||
|
||||
return HttpResponse(result)
|
||||
|
||||
# Import a workout from Strava
|
||||
@login_required()
|
||||
|
||||
Reference in New Issue
Block a user