adding rp3 auto import
This commit is contained in:
@@ -12,16 +12,24 @@ import gzip
|
||||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
import django_rq
|
||||
queue = django_rq.get_queue('default')
|
||||
queuelow = django_rq.get_queue('low')
|
||||
queuehigh = django_rq.get_queue('high')
|
||||
|
||||
from rowers.utils import myqueue
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
||||
STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET,
|
||||
RP3_CLIENT_ID, RP3_CLIENT_SECRET,
|
||||
RP3_REDIRECT_URI,RP3_CLIENT_KEY,
|
||||
RP3_CLIENT_ID, RP3_CLIENT_KEY, RP3_REDIRECT_URI, RP3_CLIENT_SECRET,
|
||||
RP3_CLIENT_ID, RP3_CLIENT_KEY, RP3_REDIRECT_URI, RP3_CLIENT_SECRET,
|
||||
UPLOAD_SERVICE_URL, UPLOAD_SERVICE_SECRET
|
||||
)
|
||||
|
||||
from rowers.tasks import handle_rp3_async_workout
|
||||
|
||||
|
||||
from celery import Celery,app
|
||||
from django_rq import job
|
||||
@@ -60,14 +68,14 @@ def do_refresh_token(refreshtoken):
|
||||
def get_token(code):
|
||||
client_auth = requests.auth.HTTPBasicAuth(RP3_CLIENT_KEY, RP3_CLIENT_SECRET)
|
||||
post_data = {
|
||||
"client_id":RP3_CLIENT_KEY,
|
||||
"client_id":RP3_CLIENT_KEY,
|
||||
"grant_type": "authorization_code",
|
||||
"code": code,
|
||||
"redirect_uri":RP3_REDIRECT_URI,
|
||||
"client_secret": RP3_CLIENT_SECRET,
|
||||
"client_secret": RP3_CLIENT_SECRET,
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
@@ -107,13 +115,49 @@ def get_rp3_workout_list(user):
|
||||
}
|
||||
}"""
|
||||
response = requests.post(
|
||||
url=graphql_url,
|
||||
headers=headers,
|
||||
json={'query': get_workouts_list}
|
||||
)
|
||||
url=graphql_url,
|
||||
headers=headers,
|
||||
json={'query': get_workouts_list}
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
def get_rp3_workouts(rower,do_async=True):
|
||||
try:
|
||||
auth_token = rp3_open(rower.user)
|
||||
except NoTokenError:
|
||||
return 0
|
||||
|
||||
res = get_rp3_workout_list(rower.user)
|
||||
|
||||
if (res.status_code != 200):
|
||||
return 0
|
||||
|
||||
workouts_list = pd.json_normalize(res.json()['data']['workouts'])
|
||||
rp3ids = workouts_list['id'].values
|
||||
workouts_list.set_index('id',inplace=True)
|
||||
|
||||
knownrp3ids = uniqify([
|
||||
w.uploadedtorp3 for w in Workout.objects.filter(user=rower)
|
||||
])
|
||||
|
||||
newids = [rp3id for rp3id in rp3ids if not rp3id in knownrp3ids]
|
||||
|
||||
for id in newids:
|
||||
startdatetime = workouts_list.loc[id,'executed_at']
|
||||
|
||||
job = myqueue(
|
||||
queuehigh,
|
||||
handle_rp3_async_workout,
|
||||
rower.user.id,
|
||||
auth_token,
|
||||
id,
|
||||
startdatetime,
|
||||
10,
|
||||
)
|
||||
|
||||
return 1
|
||||
|
||||
def download_rp3_file(url,auth_token,filename):
|
||||
headers = {'Authorization': 'Bearer ' + auth_token }
|
||||
|
||||
@@ -129,11 +173,11 @@ def get_rp3_workout_token(workout_id,auth_token,waittime=3,max_attempts=20):
|
||||
headers = {'Authorization': 'Bearer ' + auth_token }
|
||||
|
||||
get_download_link = """{
|
||||
download(workout_id: """ + str(workout_id) + """, type:csv){
|
||||
id
|
||||
status
|
||||
link
|
||||
}
|
||||
download(workout_id: """ + str(workout_id) + """, type:csv){
|
||||
id
|
||||
status
|
||||
link
|
||||
}
|
||||
}"""
|
||||
|
||||
have_link = False
|
||||
@@ -144,7 +188,7 @@ def get_rp3_workout_token(workout_id,auth_token,waittime=3,max_attempts=20):
|
||||
url=graphql_url,
|
||||
headers=headers,
|
||||
json={'query': get_download_link}
|
||||
)
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
have_link = True
|
||||
|
||||
Reference in New Issue
Block a user