Private
Public Access
1
0

import list rp3 working

This commit is contained in:
Sander Roosendaal
2021-01-27 08:39:33 +01:00
parent 3ef8d4ba57
commit 2d7d07a914
5 changed files with 139 additions and 123 deletions

View File

@@ -21,7 +21,6 @@ from rowsandall_app.settings import (
RP3_CLIENT_ID, RP3_CLIENT_KEY, RP3_REDIRECT_URI, RP3_CLIENT_SECRET
)
tpapilocation = "https://api.trainingpeaks.com"
from celery import Celery,app
from django_rq import job
@@ -45,6 +44,8 @@ oauth_data = {
from rowers.rower_rules import is_workout_user
graphql_url = "https://rp3rowing-app.com/graphql"
# Checks if user has UnderArmour token, renews them if they are expired
def rp3_open(user):
@@ -73,8 +74,6 @@ def get_token(code):
data=post_data,verify=False,
)
print(response.json())
try:
token_json = response.json()
@@ -93,124 +92,23 @@ def make_authorization_url(request):
return imports_make_authorization_url(oauth_data)
def getidfromresponse(response):
t = json.loads(response.text)
def get_rp3_workout_list(user):
r = Rower.objects.get(user=user)
links = t["_links"]
auth_token = rp3_open(user)
id = links["self"][0]["id"]
headers = {'Authorization': 'Bearer ' + auth_token }
return int(id)
def createtpworkoutdata(w):
filename = w.csvfilename
row = rowingdata(csvfile=filename)
tcxfilename = filename[:-4]+'.tcx'
try:
newnotes = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
except TypeError:
newnotes = 'from '+w.workoutsource+' via rowsandall.com'
row.exporttotcx(tcxfilename,notes=newnotes)
return tcxfilename
def rp3_check(access_token):
headers = {
"Content-Type": "application/json",
'Accept': 'application/json',
'authorization': 'Bearer %s' % access_token
}
resp = requests.post(tpapilocation+"/v1/info/version",
headers=headers,verify=False)
return resp
def uploadactivity(access_token,filename,description='',
name='Rowsandall.com workout'):
data_gz = BytesIO()
with open(filename,'rb') as inF:
s = inF.read()
with gzip.GzipFile(fileobj=data_gz,mode="w") as gzf:
gzf.write(s)
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer %s' % access_token
}
data = {
"UploadClient": "rowsandall",
"Filename": filename,
"SetWorkoutPublic": True,
"Title":name,
"Type": "rowing",
"Comment": description,
"Data": base64.b64encode(data_gz.getvalue()).decode("ascii")
get_workouts_list = """{
workouts{
id
executed_at
}
}"""
response = requests.post(
url=graphql_url,
headers=headers,
json={'query': get_workouts_list}
)
resp = requests.post(tpapilocation+"/v1/file",
data = json.dumps(data),
headers=headers,verify=False)
if resp.status_code != 200:
return 0,resp.reason,resp.status_code,headers
else:
return resp.json()[0]["Id"],"ok",200,""
return 0,0,0,0
def workout_rp3_upload(user,w):
message = "Uploading to TrainingPeaks"
tpid = 0
r = w.user
thetoken = rp3_open(r.user)
# need some code if token doesn't refresh
if (is_workout_user(user,w)):
tcxfile = createtpworkoutdata(w)
if tcxfile:
res,reason,status_code,headers = uploadactivity(
thetoken,tcxfile,
name=w.name
)
if res == 0:
message = "Upload to TrainingPeaks failed with status code "+str(status_code)+": "+reason
w.tpid = -1
try:
os.remove(tcxfile)
except WindowsError:
pass
return message,tpid
else: # res != 0
w.uploadedtotp = res
tpid = res
w.save()
os.remove(tcxfile)
return 'Successfully synchronized to TrainingPeaks',tpid
else: # no tcxfile
message = "Upload to TrainingPeaks failed"
w.uploadedtotp = -1
tpid = -1
w.save()
return message,tpid
else: # not allowed to upload
message = "You are not allowed to export this workout to TP"
tpid = 0
return message,tpid
return message,tpid
return response