Private
Public Access
1
0

async strava sync

This commit is contained in:
Sander Roosendaal
2020-03-10 15:53:18 +01:00
parent f2635651aa
commit 718453de96
5 changed files with 76 additions and 41 deletions

View File

@@ -29,6 +29,8 @@ from rowers.utils import myqueue
import rowers.mytypes as mytypes import rowers.mytypes as mytypes
import gzip import gzip
from rowers.tasks import handle_strava_sync
from rowsandall_app.settings import ( from rowsandall_app.settings import (
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET
@@ -442,7 +444,7 @@ def createstravaworkoutdata(w,dozip=True):
# Upload the TCX file to Strava and set the workout activity type # Upload the TCX file to Strava and set the workout activity type
# to rowing on Strava # to rowing on Strava
def handle_stravaexport(f2,workoutname,stravatoken,description='', def handle_stravaexport(f2,workoutname,stravatoken,description='',
activity_type='Rowing',quick=False): activity_type='Rowing',quick=False,asynchron=False):
# w = Workout.objects.get(id=workoutid) # w = Workout.objects.get(id=workoutid)
client = stravalib.Client(access_token=stravatoken) client = stravalib.Client(access_token=stravatoken)
@@ -628,7 +630,7 @@ def add_workout_from_data(user,importid,data,strokedata,
return id,message return id,message
def workout_strava_upload(user,w, quick=False): def workout_strava_upload(user,w, quick=False,asynchron=True):
try: try:
thetoken = strava_open(user) thetoken = strava_open(user)
except NoTokenError: except NoTokenError:
@@ -641,8 +643,20 @@ def workout_strava_upload(user,w, quick=False):
if (r.stravatoken == '') or (r.stravatoken is None): if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize" s = "Token doesn't exist. Need to authorize"
raise NoTokenError("Your hovercraft is full of eels") raise NoTokenError("Your hovercraft is full of eels")
else:
if (is_workout_user(user,w)): if (is_workout_user(user,w)):
if asynchron:
tcxfile, tcxmesg = createstravaworkoutdata(w)
if not tcxfile:
return "Failed to create workout data",0
job = myqueue(queue,
handle_strava_sync,
r.stravatoken,
w.id,
tcxfile,w.name,r.stravaexportas,
w.notes
)
return "Asynchronous sync",-1
try: try:
tcxfile,tcxmesg = createstravaworkoutdata(w) tcxfile,tcxmesg = createstravaworkoutdata(w)
if tcxfile: if tcxfile:
@@ -651,7 +665,7 @@ def workout_strava_upload(user,w, quick=False):
f,w.name, f,w.name,
r.stravatoken, r.stravatoken,
description=w.notes+'\n from '+w.workoutsource+' via rowsandall.com', description=w.notes+'\n from '+w.workoutsource+' via rowsandall.com',
activity_type=r.stravaexportas,quick=quick) activity_type=r.stravaexportas,quick=quick,asynchron=asynchron)
if res==0: if res==0:
message = mes message = mes
w.uploadedtostrava = -1 w.uploadedtostrava = -1
@@ -687,7 +701,7 @@ def workout_strava_upload(user,w, quick=False):
os.remove(tcxfile) os.remove(tcxfile)
return message,stravaid return message,stravaid
return message,stravaid return message,stravaid
return message,stravaid
def handle_strava_import_stroke_data(title, def handle_strava_import_stroke_data(title,

View File

@@ -101,6 +101,7 @@ import rowers.utils as utils
import requests import requests
import rowers.longtask as longtask import rowers.longtask as longtask
import arrow import arrow
import stravalib
from rowers.utils import get_strava_stream from rowers.utils import get_strava_stream
@@ -135,6 +136,27 @@ def handle_c2_sync(workoutid,url,headers,data,debug=False,**kwargs):
return res return res
@app.task
def handle_strava_sync(stravatoken,workoutid,filename,name,activity_type,description,debug=False,**kwargs):
client = stravalib.Client(access_token=stravatoken)
with open(filename,'rb') as f:
act = client.upload_activity(f,'tcx.gz',name=name)
res = act.wait(poll_interval=5.0, timeout=60)
try:
act = client.update_activity(res.id,activity_type=activity_type,
description=description,device_name='Rowsandall.com')
except TypeError:
act = client.update_activity(res.id,activity_type=activity_type,
description=description)
result = update_workout_field_sql(workoutid,'uploadedtostrava',res.id,debug=debug)
try:
os.remove(filename)
except WindowsError:
pass
return 1
@app.task @app.task
def handle_c2_import_stroke_data(c2token, def handle_c2_import_stroke_data(c2token,
c2id,workoutid, c2id,workoutid,

View File

@@ -535,7 +535,7 @@ def do_sync(w,options, quick=False):
if ('upload_to_Strava' in options and upload_to_strava) or (w.user.strava_auto_export and ispromember(w.user.user)): if ('upload_to_Strava' in options and upload_to_strava) or (w.user.strava_auto_export and ispromember(w.user.user)):
try: try:
message,id = stravastuff.workout_strava_upload( message,id = stravastuff.workout_strava_upload(
w.user.user,w,quick=quick w.user.user,w,quick=quick,asynchron=True,
) )
except NoTokenError: except NoTokenError:
id = 0 id = 0

View File

@@ -305,7 +305,6 @@ def myqueue(queue,function,*args,**kwargs):
return MockJob() return MockJob()
elif settings.CELERY: elif settings.CELERY:
kwargs['debug'] = True kwargs['debug'] = True
job = function.delay(*args,**kwargs) job = function.delay(*args,**kwargs)
else: else:
if settings.DEBUG: if settings.DEBUG:

View File

@@ -4810,7 +4810,7 @@ def workout_upload_view(request,
# upload to C2 # upload to C2
if (upload_to_c2): if (upload_to_c2):
try: try:
message,id = c2stuff.workout_c2_upload(request.user,w,asynchron=True) message,id = c2stuff.workout_c2_upload(request.user,w)
except NoTokenError: except NoTokenError:
id = 0 id = 0
message = "Something went wrong with the Concept2 sync" message = "Something went wrong with the Concept2 sync"
@@ -4822,7 +4822,7 @@ def workout_upload_view(request,
if (upload_to_strava): if (upload_to_strava):
try: try:
message,id = stravastuff.workout_strava_upload( message,id = stravastuff.workout_strava_upload(
request.user,w request.user,w,asynchron=True,
) )
except NoTokenError: except NoTokenError:
id = 0 id = 0