-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_oura.py
76 lines (56 loc) · 2.05 KB
/
dump_oura.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
import argparse
import configparser
import datetime
import os
import json
import time
from oura import OuraClient
from etlutils.date import mkdate
DUMP_DIR = 'oura-dumps'
CONFIG_FILE = 'config.ini'
def logmsg(msg):
time = datetime.datetime.now()
print("[%04i/%02i/%02i %02i:%02i:%02i]: %s" % (time.year, time.month, time.day, time.hour, time.minute, time.second, msg))
def update_config(token_dict):
global config_parser
config_parser['Login Parameters']['refresh_token'] = token_dict['refresh_token']
with open(CONFIG_FILE, 'w') as configfile:
config_parser.write(configfile)
def dump_to_json_file(data_type, date, data):
directory = "%s/%i/%s" % (DUMP_DIR, date.year, date)
if not os.path.isdir(directory):
os.makedirs(directory)
with open("%s/%s.json" % (directory, data_type), "w") as f:
f.write(json.dumps(data, indent=2))
time.sleep(1)
def previously_dumped(date):
return os.path.isdir("%s/%i/%s" % (DUMP_DIR, date.year, date))
def dump_day(c, date):
date_str = date.isoformat()
sleep_data = c.sleep_summary(date_str)
dump_to_json_file("sleep", date, sleep_data)
activity_data = c.activity_summary(date_str)
dump_to_json_file("activity", date, activity_data)
readiness_data = c.readiness_summary(date_str)
dump_to_json_file("readiness", date, readiness_data)
return True
config_parser = configparser.ConfigParser()
config_parser.read(CONFIG_FILE)
personal_access_token = config_parser.get('Login Parameters', 'personal_access_token')
auth_client = OuraClient(
personal_access_token=personal_access_token
)
date = datetime.date.today()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Dump all oura data.')
parser.add_argument('-d', '--date', type=mkdate)
ns = parser.parse_args()
if not ns.date is None:
date = ns.date
while not previously_dumped(date):
logmsg('dumping {}'.format(date))
r = dump_day(auth_client, date)
date -= datetime.timedelta(days=1)
if not r:
break