Private
Public Access
1
0

rp3stff functions to support downloading file

This commit is contained in:
Sander Roosendaal
2021-01-27 11:25:52 +01:00
parent 2d7d07a914
commit 4070dd26ca

View File

@@ -112,3 +112,60 @@ def get_rp3_workout_list(user):
)
return response
def download_rp3_file(url,auth_token,filename):
headers = {'Authorization': 'Bearer ' + auth_token }
res = requests.get(url,headers=headers)
if res.status_code == 200:
with open(filename,'wb') as f:
f.write(res.content)
return res.status_code
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:tcx){
id
status
link
}
}"""
have_link = False
download_url = ''
counter = 0
while not have_link:
response = requests.post(
url=graphql_url,
headers=headers,
json={'query': get_download_link}
)
if response.status_code != 200:
have_link = True
workout_download_details = pd.json_normalize(response.json()['data']['download'])
if workout_download_details.iat[0,1] == 'ready':
download_url = workout_download_details.iat[0,2]
have_link = True
counter += 1
if counter>max_attempts:
have_link = True
time.sleep(waittime)
return download_url
def get_rp3_workout_link(user,workout_id,waittime=3,max_attempts=20):
r = Rower.objects.get(user=user)
auth_token = rp3_open(user)
return get_rp3_workout_token(workout_id,auth_token,waittime=waittime,max_attempts=max_attempts)