getting intervals_import working
This commit is contained in:
231
rowers/upload_tasks.py
Normal file
231
rowers/upload_tasks.py
Normal file
@@ -0,0 +1,231 @@
|
||||
import os
|
||||
from uuid import uuid4
|
||||
import shutil
|
||||
import requests
|
||||
from rowingdata import FITParser as FP
|
||||
from rowingdata.otherparsers import FitSummaryData
|
||||
import rowingdata
|
||||
|
||||
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
|
||||
from YamJam import yamjam
|
||||
CFG = yamjam()['rowsandallapp']
|
||||
|
||||
try:
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE",CFG['settings_name'])
|
||||
except KeyError: # pragma: no cover
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE","rowsandall_app.settings")
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
application = get_wsgi_application()
|
||||
from rowers.models import (
|
||||
Workout, GeoPolygon, GeoPoint, GeoCourse,
|
||||
VirtualRaceResult, CourseTestResult, Rower,
|
||||
GraphImage, Team, PlannedSession
|
||||
)
|
||||
from rowers.session_utils import is_session_complete
|
||||
from rowers.nkimportutils import (
|
||||
get_nk_summary, get_nk_allstats, get_nk_intervalstats, getdict, strokeDataToDf,
|
||||
add_workout_from_data
|
||||
)
|
||||
from rowers.mytypes import intervalsmappinginv
|
||||
from rowers.dataroutines import (
|
||||
totaltime_sec_to_string,
|
||||
)
|
||||
|
||||
from rowers.celery import app
|
||||
from celery import shared_task
|
||||
|
||||
SITE_URL = CFG['site_url']
|
||||
SITE_URL_DEV = CFG['site_url']
|
||||
PROGRESS_CACHE_SECRET = CFG['progress_cache_secret']
|
||||
try:
|
||||
SETTINGS_NAME = CFG['settings_name']
|
||||
except KeyError: # pragma: no cover
|
||||
SETTINGS_NAME = 'rowsandall_ap.settings'
|
||||
|
||||
|
||||
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 rowers.dataflow import upload_handler
|
||||
|
||||
@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
|
||||
result = upload_handler(uploadoptions, csvfilename)
|
||||
if remove_workout:
|
||||
workout.delete()
|
||||
|
||||
return 1
|
||||
|
||||
@app.task
|
||||
def handle_post_workout_api(uploadoptions, debug=False, **kwargs): # pragma: no cover
|
||||
csvfilename = uploadoptions['file']
|
||||
return upload_handler(uploadoptions, csvfilename)
|
||||
|
||||
@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
|
||||
|
||||
w = Workout(
|
||||
user=rower,
|
||||
duration=duration,
|
||||
uploadedtointervals=workoutid,
|
||||
)
|
||||
w.save()
|
||||
|
||||
uploadoptions = {
|
||||
'user': rower.user.id,
|
||||
'boattype': '1x',
|
||||
'workouttype': workouttype,
|
||||
'file': fit_filename,
|
||||
'intervalsid': workoutid,
|
||||
'title': title,
|
||||
'rpe': rpe,
|
||||
'notes': '',
|
||||
'offline': False,
|
||||
'id': w.id,
|
||||
}
|
||||
|
||||
response = upload_handler(uploadoptions, fit_filename)
|
||||
if response['status'] != 'processing':
|
||||
return 0
|
||||
|
||||
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 w.id
|
||||
|
||||
Reference in New Issue
Block a user