Skip to content

Commit 9686e3b

Browse files
committed
OuraRing API integration_step1 - DB Sync
Add code to connect to Oura API, and download and insert to DB sleep, activity and readiness data for the selected timeframe.
1 parent 3cf9104 commit 9686e3b

File tree

8 files changed

+365
-50
lines changed

8 files changed

+365
-50
lines changed

src/athletedataapp_apache.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from mind_monitor_data_download_db_insert import mm_data_insert
2121
from weather import get_weather
2222
from send_email import send_email
23+
from oura_data_download import dwnld_insert_oura_data
2324
import psutil
2425
import urllib.request, urllib.error, urllib.parse
2526
from database_ini_parser import config
@@ -31,7 +32,7 @@
3132
from Crypto.Cipher import AES
3233
import base64
3334
from requests_oauthlib import OAuth2Session
34-
35+
3536
#----Crypto Variables----
3637
# salt size in bytes
3738
SALT_SIZE = 16
@@ -73,6 +74,10 @@ def encrypt(plaintext, password):
7374
OURA_CLIENT_SECRET = str(oura_params.get("oura_client_secret"))
7475
OURA_AUTH_URL = str(oura_params.get("oura_auth_url"))
7576
OURA_TOKEN_URL = str(oura_params.get("oura_token_url"))
77+
if OURA_CLIENT_ID == "":
78+
oura_enabled = False
79+
else:
80+
oura_enabled = True
7681

7782
anticaptcha_params = config(filename="encrypted_settings.ini", section="anticaptcha",encr_pass=encr_pass)
7883
anticaptcha_api_key = str(anticaptcha_params.get("api_key"))
@@ -127,6 +132,7 @@ def index():
127132
gc_fit_activ_progress = None
128133
gc_tcx_activ_progress = None
129134
gc_fit_well_progress = None
135+
oura_well_progress = None
130136
gc_json_well_progress = None
131137
gc_json_dailysum_progress = None
132138
progress_error = False
@@ -150,7 +156,7 @@ def index():
150156
elif gc_login_radio == 'getGCcredfromCSV':
151157
gc_username = str(request.form.get('gcUN'))
152158
gc_password = str(request.form.get('gcPW'))
153-
159+
154160
#----Destination DB variables-----
155161
db_name = str(str2md5(gc_username)) + '_Athlete_Data_DB'
156162

@@ -165,7 +171,7 @@ def index():
165171
connection = check_db_server_connectivity(gc_username,db_host,superuser_un,superuser_pw)
166172
if connection != 'SUCCESS':
167173
flash(' Could cot connect to the DB Host.The host returned an error: '+connection,'danger')
168-
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
174+
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
169175
else:
170176
#If user does not wish to download to his db, credentials to be retrieved from .ini.
171177
params = config(filename="encrypted_settings.ini", section="postgresql",encr_pass=encr_pass)
@@ -178,13 +184,13 @@ def index():
178184
host_record_exists = check_host_record_exists(gc_username,db_name,db_host,encr_pass)
179185
if host_record_exists == True:
180186
flash(' You can only download to one db host. Please correct the db_hostname and try again','warning')
181-
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
187+
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
182188

183189
#----Check if the provided GC credentials are valid-----
184190
gc_cred_valid = gc.check_gc_creds(gc_username,gc_password)
185191
if gc_cred_valid == False:
186192
flash(' The Garmin Connect login credentials are not valid. Please try again','warning')
187-
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
193+
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
188194

189195

190196
#----- Set Auto_Synch variables--------
@@ -275,7 +281,7 @@ def index():
275281
else:
276282
return redirect(url_for('oura_auth_request'))
277283

278-
# CLEANUP BEFORE DOWNLOAD -----------------
284+
# CLEANUP BEFORE DOWNLOAD -----------------
279285

280286
#----Delete Files and DB Data variables----
281287
try:
@@ -361,7 +367,7 @@ def index():
361367
time.sleep(1)
362368
with ProgressStdoutRedirection(gc_username):
363369
print(del_progress)
364-
return render_template("index.html",del_progress=del_progress,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
370+
return render_template("index.html",del_progress=del_progress,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
365371
except:
366372
del_progress = 'Error deleting data'
367373
progress_error = True
@@ -370,9 +376,9 @@ def index():
370376
time.sleep(1)
371377
with ErrorStdoutRedirection(gc_username):
372378
print((str(datetime.datetime.now()) + ' ' + del_progress))
373-
return render_template("index.html",del_progress=del_progress,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
379+
return render_template("index.html",del_progress=del_progress,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
374380

375-
381+
376382
if gc_username is None and gc_password is None and mfp_username is None and mfp_password is None:
377383
progress_error = True
378384
continue_btn = 'none'
@@ -402,7 +408,7 @@ def index():
402408
print((str(datetime.datetime.now()) + ' ' + str(e)))
403409
#PG:If start date not provided render index and flash warning
404410
flash(' Please provide a valid start date and try again!','danger')
405-
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
411+
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
406412

407413
# DATA DOWNLOAD --------------------------------------
408414

@@ -427,7 +433,7 @@ def index():
427433
with ErrorStdoutRedirection(gc_username):
428434
print((str(datetime.datetime.now()) + ' ' + gc_login_progress))
429435
flash(' There was a problem logging in to Garmin Connect. Please try again later','warning')
430-
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
436+
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
431437

432438
#---------------------------------- Activity ---------------------------------------
433439

@@ -482,10 +488,6 @@ def index():
482488
with ErrorStdoutRedirection(gc_username):
483489
print((str(datetime.datetime.now()) + ' ' + gc_fit_well_progress))
484490

485-
#TODO:
486-
#PG:Call to execute "Parse and insert Oura wellness data" script
487-
#TODO
488-
489491
#PG:Call to execute "Parse and insert JSON wellness data" script
490492
try:
491493
gc_json_well_progress = 'GC JSON wellness download started'
@@ -535,6 +537,31 @@ def index():
535537
with ErrorStdoutRedirection(gc_username):
536538
print((str(datetime.datetime.now()) + ' ' + gc_json_dailysum_progress))
537539

540+
#PG:Call to execute "Parse and insert Oura wellness data" script
541+
if request.form.get('ouraCheckbox') is not None:
542+
try:
543+
oura_well_progress = 'Oura wellness download started'
544+
with StdoutRedirection(gc_username):
545+
print(oura_well_progress)
546+
time.sleep(1)
547+
dwnld_insert_oura_data(gc_username,db_host,db_name,superuser_un,superuser_pw,oura_refresh_token,start_date,end_date,save_pwd,encr_pass)
548+
oura_well_progress = 'Oura wellness data downloaded successfully'
549+
with StdoutRedirection(gc_username):
550+
print(oura_well_progress)
551+
time.sleep(1)
552+
with ProgressStdoutRedirection(gc_username):
553+
print(oura_well_progress)
554+
except Exception as e:
555+
with ErrorStdoutRedirection(gc_username):
556+
print((str(datetime.datetime.now()) + ' ' + str(e)))
557+
oura_well_progress = 'Error downloading Oura wellness data'
558+
progress_error = True
559+
with StdoutRedirection(gc_username):
560+
print(oura_well_progress)
561+
time.sleep(1)
562+
with ErrorStdoutRedirection(gc_username):
563+
print((str(datetime.datetime.now()) + ' ' + oura_well_progress))
564+
538565
#----------------- Nutrition MFP ---------------------------
539566
#PG:Call to execute "parse and insert MFP data" script
540567
if mfp_username is not None:
@@ -678,9 +705,9 @@ def index():
678705
print(('--------------- ' + str(datetime.datetime.now()) + ' User ' + gc_username + ' Finished Data Download ' + error_log_entry +' -------------' ))
679706

680707
return render_template("index.html",del_progress = del_progress,mfp_progress = mfp_progress,diasend_progress = diasend_progress,glimp_progress = glimp_progress, mm_progress = mm_progress, gc_login_progress = gc_login_progress,
681-
gc_fit_activ_progress = gc_fit_activ_progress,gc_tcx_activ_progress = gc_tcx_activ_progress,
682-
gc_fit_well_progress = gc_fit_well_progress, gc_json_well_progress = gc_json_well_progress,
683-
gc_json_dailysum_progress = gc_json_dailysum_progress, progress_error = progress_error, continue_btn = continue_btn,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
708+
gc_fit_activ_progress = gc_fit_activ_progress,gc_tcx_activ_progress = gc_tcx_activ_progress,gc_fit_well_progress = gc_fit_well_progress, gc_json_well_progress = gc_json_well_progress,
709+
gc_json_dailysum_progress = gc_json_dailysum_progress, oura_well_progress = oura_well_progress, progress_error = progress_error, continue_btn = continue_btn,admin_email=admin_email,
710+
integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
684711

685712
except Exception as e:
686713
with ErrorStdoutRedirection(gc_username):
@@ -693,7 +720,7 @@ def index():
693720

694721
else: # Request method is GET
695722
continue_btn = request.args.get('continue_btn')
696-
return render_template("index.html",continue_btn = continue_btn,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
723+
return render_template("index.html",continue_btn = continue_btn,admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
697724

698725
@app.route("/datamodel_preview")
699726
def datamodel_preview():
@@ -783,7 +810,7 @@ def db_info():
783810
db_username = None
784811
password_info = None
785812
flash(' The DB name, DB role and DB permissions are generated based on your Garmin Connect username. Please fill in your GC credentials and try again. This information is not recorded anywhere until you proceed with the download and the AutoSynch option is enabled.','warning')
786-
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled)
813+
return render_template("index.html",admin_email=admin_email,integrated_with_dropbox=integrated_with_dropbox,diasend_enabled=diasend_enabled,oura_enabled=oura_enabled)
787814

788815
@app.route("/dropbox_auth_request")
789816
def dropbox_auth_request():

0 commit comments

Comments
 (0)