-
Notifications
You must be signed in to change notification settings - Fork 1
/
1fetch_v2.py
executable file
·69 lines (56 loc) · 2.29 KB
/
1fetch_v2.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
#!/usr/bin/env python3
# by Dr. Torben Menke https://entorb.net
# https://github.com/entorb/analyze-oura
"""
Fetch Oura day-summary data from Oura Cloud API.
requires a personal access token from https://cloud.ouraring.com/personal-access-tokens
provide your personal access token in file token.txt
set the start date in config.json
fetched data is stored in data/
"""
# standard modules
import json
from pathlib import Path
import requests
Path("data").mkdir(exist_ok=True)
with Path("config.json").open(encoding="utf-8") as fh:
config = json.load(fh)
try:
with Path("token.txt").open() as fh:
token = fh.read().strip() # trim spaces
except FileNotFoundError:
msg = "token.txt not found."
raise FileNotFoundError(msg) from None
def fetch_data_summaries() -> None:
"""
Fetch data from Oura API.
"""
for data_summary_set in ("sleep",): # , "activity", "readiness"
print(f"fetching {data_summary_set} data")
# url = "https://api.ouraring.com/v1/sleep"
# -> last week
url = f"https://api.ouraring.com/v2/usercollection/{data_summary_set}?start_date={config['date_start']}"
# start=YYYY-MM-DD
# end=YYYY-MM-DD
headers = {
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0 ", # noqa: E501
"Authorization": f"Bearer {token}",
}
try:
response = requests.get(url, headers=headers, timeout=15)
response.raise_for_status() # Raise an HTTPError for bad responses
cont = response.content.decode("utf-8")
except requests.RequestException as e:
print(f"Error fetching {data_summary_set} data: {e}")
continue
# Write raw data to file
raw_data_path = Path(f"data/data_raw_{data_summary_set}.json")
with raw_data_path.open(mode="w", encoding="utf-8", newline="\n") as fh:
fh.write(cont)
# Write formatted data to file
formatted_data_path = Path(f"data/data_formatted_{data_summary_set}.json")
with formatted_data_path.open(mode="w", encoding="utf-8", newline="\n") as fh:
d = json.loads(cont)
json.dump(d, fh, ensure_ascii=False, sort_keys=False, indent=True)
if __name__ == "__main__":
fetch_data_summaries()