Private
Public Access
1
0

doing trainingpeaks, untested

This commit is contained in:
Sander Roosendaal
2023-02-15 19:25:45 +01:00
parent 8912ee3c8e
commit 3eaef9ae70
12 changed files with 230 additions and 320 deletions

View File

@@ -47,6 +47,8 @@ import sys
import json
import traceback
from time import strftime
import base64
from io import BytesIO
from scipy import optimize
from scipy.signal import savgol_filter
@@ -105,7 +107,8 @@ except KeyError: # pragma: no cover
NK_API_LOCATION = CFG["nk_api_location"]
TP_CLIENT_ID = CFG["tp_client_id"]
TP_CLIENT_SECRET = CFG["tp_client_secret"]
TP_API_LOCATION = CFG["tp_api_location"]
tpapilocation = TP_API_LOCATION
from requests_oauthlib import OAuth1, OAuth1Session
@@ -344,6 +347,46 @@ def handle_add_workouts_team(ws, t, debug=False, **kwargs):
return 1
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")
}
resp = requests.post(tpapilocation+"/v3/file",
data=json.dumps(data),
headers=headers, verify=False)
if resp.status_code not in (200, 202): # pragma: no cover
dologging('tp_export.log',resp.status_code)
dologging('tp_export.log',resp.reason)
dologging('tp_export.log',json.dumps(data))
return 0, resp.reason, resp.status_code, headers
else:
return 1, "ok", 200, resp.headers
return 0, 0, 0, 0 # pragma: no cover
@app.task
def check_tp_workout_id(workout, location, attempts=5, debug=False, **kwargs):
authorizationstring = str('Bearer ' + workout.user.tptoken)
@@ -361,6 +404,37 @@ def check_tp_workout_id(workout, location, attempts=5, debug=False, **kwargs):
return 1
@app.task
def handle_workout_tp_upload(w, thetoken, tcxfilename, debug=False, **kwargs):
tpid = 0
r = w.user
if not tcxfilename:
return 0
res, reason, status_code, headers = uploadactivity(
thetoken, tcxfilename,
name=w.name
)
if res == 0:
w.tpid = -1
try:
os.remove(tcxfilename)
except WindowsError:
pass
w.save()
return 0
w.uploadedtotp = res
tpid = res
w.save()
os.remove(tcxfilename)
check_tp_workout_id(w,headers['Location'])
return tpid
@app.task
def instroke_static(w, metric, debug=False, **kwargs):
f1 = w.csvfilename[6:-4]