added auto export for all platforms
This commit is contained in:
@@ -17,6 +17,7 @@ import os,sys
|
||||
import gzip
|
||||
import base64
|
||||
import yaml
|
||||
from uuid import uuid4
|
||||
|
||||
# Django
|
||||
from django.shortcuts import render_to_response
|
||||
@@ -46,7 +47,8 @@ from rowsandall_app.settings import (
|
||||
POLAR_CLIENT_ID, POLAR_REDIRECT_URI, POLAR_CLIENT_SECRET,
|
||||
)
|
||||
|
||||
baseurl = 'https://polaraccesslink.com/v3-example'
|
||||
#baseurl = 'https://polaraccesslink.com/v3-example'
|
||||
baseurl = 'https://polaraccesslink.com/v3'
|
||||
|
||||
# Custom exception handler, returns a 401 HTTP message
|
||||
# with exception details in the json data
|
||||
@@ -111,7 +113,6 @@ def get_token(code):
|
||||
def make_authorization_url():
|
||||
# Generate a random string for the state parameter
|
||||
# Save it for use later to prevent xsrf attacks
|
||||
from uuid import uuid4
|
||||
state = str(uuid4())
|
||||
|
||||
params = {"client_id": POLAR_CLIENT_ID,
|
||||
@@ -124,6 +125,43 @@ def make_authorization_url():
|
||||
|
||||
return HttpResponseRedirect(url)
|
||||
|
||||
def get_polar_notifications():
|
||||
url = baseurl+'/notifications'
|
||||
state = str(uuid4())
|
||||
auth_string = '{id}:{secret}'.format(
|
||||
id= POLAR_CLIENT_ID,
|
||||
secret=POLAR_CLIENT_SECRET
|
||||
)
|
||||
|
||||
headers = { 'Authorization': 'Basic %s' % base64.b64encode(auth_string) }
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
|
||||
available_data = []
|
||||
|
||||
if response.status_code == 200:
|
||||
available_data = response.json()['available-user-data']
|
||||
|
||||
return available_data
|
||||
|
||||
def get_all_new_workouts(available_data,testing=False):
|
||||
for record in available_data:
|
||||
if testing:
|
||||
print record
|
||||
if record['data-type'] == 'EXERCISE':
|
||||
try:
|
||||
r = Rower.objects.get(polaruserid=record['user-id'])
|
||||
u = r.user
|
||||
if r.polar_auto_import:
|
||||
exercise_list = get_polar_workouts(u)
|
||||
if testing:
|
||||
print exercise_list
|
||||
except Rower.DoesNotExist:
|
||||
pass
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
def get_polar_workouts(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
|
||||
@@ -180,8 +218,9 @@ def get_polar_workouts(user):
|
||||
tcxuri = exerciseurl+'/tcx'
|
||||
response = requests.get(tcxuri,headers=headers2)
|
||||
if response.status_code == 200:
|
||||
filename = 'media/mailbox_attachments/polarimport{id}.tcx'.format(
|
||||
id = exercise_dict['id']
|
||||
filename = 'media/mailbox_attachments/{code}_{id}.tcx'.format(
|
||||
id = exercise_dict['id'],
|
||||
code = uuid4().hex[:16]
|
||||
)
|
||||
|
||||
with open(filename,'wb') as fop:
|
||||
|
||||
Reference in New Issue
Block a user