Skip to content

Commit

Permalink
API v2 migration start
Browse files Browse the repository at this point in the history
  • Loading branch information
entorb committed Sep 13, 2023
1 parent 4bc69ee commit 24555a6
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 0 deletions.
File renamed without changes.
66 changes: 66 additions & 0 deletions 1fetch_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/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
import os

import requests

# external modules

os.makedirs("data", exist_ok=True)

with open(file="config.json", encoding="utf-8") as fh:
config = json.load(fh)

with open(file="token.txt") as fh:
token = fh.read()
token = token.strip() # trim spaces


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']}" # noqa: E501
# 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}",
}
cont = requests.get(url, headers=headers, timeout=3).content
cont = cont.decode("utf-8")

with open(
file=f"data/data_raw_{data_summary_set}.json",
mode="w",
encoding="utf-8",
newline="\n",
) as fh:
fh.write(cont)
with open(
f"data/data_formatted_{data_summary_set}.json",
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()
File renamed without changes.
Loading

0 comments on commit 24555a6

Please sign in to comment.