forked from turing-complet/python-ouraring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
68 lines (53 loc) · 1.92 KB
/
sample.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
import json
import os
from datetime import datetime
from oura import OuraClient
def get_self():
pat = os.getenv("OURA_PAT")
client = OuraClient(personal_access_token=pat)
user_info = client.user_info()
print(user_info)
def setEnvironment(envFile):
basePath = os.path.dirname(os.path.abspath(__file__))
fullPath = os.path.join(basePath, envFile)
with open(fullPath) as file:
env = json.load(file)
os.environ["OURA_CLIENT_ID"] = env["client_id"]
os.environ["OURA_CLIENT_SECRET"] = env["client_secret"]
os.environ["OURA_ACCESS_TOKEN"] = env["access_token"]
os.environ["OURA_REFRESH_TOKEN"] = env["refresh_token"]
def appendFile(filename, token_dict):
basePath = os.path.dirname(os.path.abspath(__file__))
fullPath = os.path.join(basePath, filename)
with open(fullPath, "r+") as file:
prev = json.load(file)
curr = {
"client_id": prev.pop("client_id"),
"client_secret": prev.pop("client_secret"),
"access_token": token_dict["access_token"],
"refresh_token": token_dict["refresh_token"],
"previous": json.dumps(prev),
}
file.seek(0)
json.dump(curr, file)
def getOuraClient(envFile):
client_id = os.getenv("OURA_CLIENT_ID")
client_secret = os.getenv("OURA_CLIENT_SECRET")
access_token = os.getenv("OURA_ACCESS_TOKEN")
refresh_token = os.getenv("OURA_REFRESH_TOKEN")
refresh_callback = lambda x: appendFile(envFile, x)
auth_client = OuraClient(
client_id=client_id,
client_secret=client_secret,
access_token=access_token,
refresh_token=refresh_token,
refresh_callback=refresh_callback,
)
return auth_client
if __name__ == "__main__":
envFile = "token.json"
setEnvironment(envFile)
client = getOuraClient(envFile)
today = datetime.today()
sleep = client.sleep_summary(today)
print(sleep)