Private
Public Access
1
0

getting intervals_import working

This commit is contained in:
2025-10-22 16:50:37 +02:00
parent c76334c50a
commit 0796f34904
9 changed files with 265 additions and 205 deletions

View File

@@ -434,49 +434,6 @@ def handle_loadnextweek(rower, debug=False, **kwargs):
return 0
@app.task
def handle_assignworkouts(workouts, rowers, remove_workout, debug=False, **kwargs):
for workout in workouts:
uploadoptions = {
'secret': UPLOAD_SERVICE_SECRET,
'title': workout.name,
'boattype': workout.boattype,
'workouttype': workout.workouttype,
'inboard': workout.inboard,
'oarlength': workout.oarlength,
'summary': workout.summary,
'elapsedTime': 3600.*workout.duration.hour+60*workout.duration.minute+workout.duration.second,
'totalDistance': workout.distance,
'useImpeller': workout.impeller,
'seatNumber': workout.seatnumber,
'boatName': workout.boatname,
'portStarboard': workout.empowerside,
}
for rower in rowers:
failed = False
csvfilename = 'media/{code}.csv'.format(code=uuid4().hex[:16])
try:
with open(csvfilename,'wb') as f:
shutil.copy(workout.csvfilename,csvfilename)
except FileNotFoundError:
try:
with open(csvfilename,'wb') as f:
csvfilename = csvfilename+'.gz'
shutil.copy(workout.csvfilename+'.gz', csvfilename)
except:
failed = True
if not failed:
uploadoptions['user'] = rower.user.id
uploadoptions['file'] = csvfilename
session = requests.session()
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
session.headers.update(newHeaders)
response = session.post(UPLOAD_SERVICE_URL, json=uploadoptions)
print(response.text)
if remove_workout:
workout.delete()
return 1
@app.task
def create_sessions_from_json_async(plansteps, rower, startdate, manager, planbyrscore, plan, plan_past_days, debug=False, **kwargs):
@@ -532,19 +489,6 @@ def create_sessions_from_json_async(plansteps, rower, startdate, manager, planby
return 1
@app.task
def handle_post_workout_api(uploadoptions, debug=False, **kwargs): # pragma: no cover
session = requests.session()
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
session.headers.update(newHeaders)
response = session.post(UPLOAD_SERVICE_URL, json=uploadoptions)
if response.status_code != 200:
return 0
return 1
@app.task
def handle_remove_workouts_team(ws, t, debug=False, **kwargs): # pragma: no cover
@@ -3744,128 +3688,6 @@ def handle_intervals_updateworkout(workout, debug=False, **kwargs):
return 0
@app.task
def handle_intervals_getworkout(rower, intervalstoken, workoutid, debug=False, **kwargs):
authorizationstring = str('Bearer '+intervalstoken)
headers = {
'authorization': authorizationstring,
}
url = "https://intervals.icu/api/v1/activity/{}".format(workoutid)
response = requests.get(url, headers=headers)
if response.status_code != 200:
return 0
data = response.json()
try:
title = data['name']
except KeyError:
title = 'Intervals workout'
try:
workouttype = intervalsmappinginv[data['type']]
except KeyError:
workouttype = 'water'
try:
rpe = data['icu_rpe']
except KeyError:
rpe = 0
try:
is_commute = data['commute']
if is_commute is None:
is_commute = False
except KeyError:
is_commute = False
try:
subtype = data['sub_type']
if subtype is not None:
subtype = subtype.capitalize()
except KeyError:
subtype = None
try:
is_race = data['race']
if is_race is None:
is_race = False
except KeyError:
is_race = False
url = "https://intervals.icu/api/v1/activity/{workoutid}/fit-file".format(workoutid=workoutid)
response = requests.get(url, headers=headers)
if response.status_code != 200:
return 0
try:
fit_data = response.content
fit_filename = 'media/'+f'{uuid4().hex[:16]}.fit'
with open(fit_filename, 'wb') as fit_file:
fit_file.write(fit_data)
except Exception as e:
return 0
try:
row = FP(fit_filename)
rowdata = rowingdata.rowingdata(df=row.df)
rowsummary = FitSummaryData(fit_filename)
duration = totaltime_sec_to_string(rowdata.duration)
distance = rowdata.df[" Horizontal (meters)"].iloc[-1]
except Exception as e:
return 0
uploadoptions = {
'secret': UPLOAD_SERVICE_SECRET,
'user': rower.user.id,
'boattype': '1x',
'workouttype': workouttype,
'file': fit_filename,
'intervalsid': workoutid,
'title': title,
'rpe': rpe,
'notes': '',
'offline': False,
}
url = UPLOAD_SERVICE_URL
handle_request_post(url, uploadoptions)
try:
paired_event_id = data['paired_event_id']
ws = Workout.objects.filter(uploadedtointervals=workoutid)
for w in ws:
w.sub_type = subtype
w.save()
if is_commute:
for w in ws:
w.is_commute = True
w.sub_type = "Commute"
w.save()
if is_race:
for w in ws:
w.is_race = True
w.save()
if ws.count() > 0:
pss = PlannedSession.objects.filter(rower=rower,intervals_icu_id=paired_event_id)
if pss.count() > 0:
for ps in pss:
for w in ws:
w.plannedsession = ps
w.save()
except KeyError:
pass
except Workout.DoesNotExist:
pass
except PlannedSession.DoesNotExist:
pass
return 1
@app.task
def handle_c2_getworkout(userid, c2token, c2id, defaulttimezone, debug=False, **kwargs):