o
    i                     @   s~   d Z ddlZddlZddlZddlZddlZddlZddlm	Z	 e	e
 jjd ZdZdd Zdd	 Zed
kr=e  dS dS )u(  
Oura Ring API setup — one-time token configuration.

Steps:
  1. Go to https://cloud.ouraring.com/personal-access-tokens
  2. Log in with your Oura account
  3. Click "Create New Personal Access Token"
  4. Copy the token
  5. Run this script and paste it

Usage:
  python3 setup_oura_auth.py
    N)Pathz.oura-token.jsonzhttps://api.ouraring.comc                  C   s\   zddl } tj|  dW S  ty   Y nw dD ]}tj|r)tj|d  S qt S )z7Build SSL context that works on macOS (cert discovery).r   N)cafile)z/etc/ssl/cert.pemz/usr/local/etc/openssl/cert.pemz!/usr/local/etc/openssl@3/cert.pemz$/opt/homebrew/etc/openssl@3/cert.pemz"/opt/homebrew/etc/openssl/cert.pem)certifisslcreate_default_contextwhereImportErrorospathexists)r   	cert_path r   setup_oura_auth.py_get_ssl_context   s   r   c               
   C   s  t d t d t d t   t d t d t d t   td } | s/t d td t }t d	 zItjjt	 d
d|  ddd}tjj
|d|d}t| }W d    n1 scw   Y  t d|dd  t d|dd  W n tjjy } z%|jdkrt d t d nt d|j d|j  td W Y d }~njd }~w tjjy } z:dt|v rt d|  t   t d t d t d  t d! t   t d" nt d#|  td W Y d }~n#d }~w ty } zt d#|  td W Y d }~nd }~ww t d$ zwd%d&lm}m} | |dd'  }|  }	tjjt	 d(| d)|	 d|  ddd}tjj
|d|d}t| }
W d    n	1 shw   Y  |
d*g }|r|d+ }t|d,d%d- d}t d.| d/ nt d0 W n ty } zt d1| d2 W Y d }~nd }~ww d3| i}ttj|d4d5 t d6t  t d7 t d8 d S )9Nz2==================================================z  Oura Ring API Setupz;1. Go to: https://cloud.ouraring.com/personal-access-tokensz02. Log in and create a new Personal Access Tokenz$3. Copy the token and paste it belowzPaste your Oura access token: zNo token entered. Exiting.   z
Testing token...z /v2/usercollection/personal_infozBearer zapplication/json)AuthorizationAccept)headers
   )timeoutcontextz  Connected to Oura account: emailunknownz  Age: age?i  z%  ERROR: Token is invalid or expired.z3  Please generate a new token at cloud.ouraring.comz  ERROR: API returned z: CERTIFICATE_VERIFY_FAILEDz  SSL certificate error: z!  Fix: run one of these commands:z    pip3 install certifiz  or:z7    /Applications/Python*/Install\ Certificates.commandz  Then re-run this script.z(  ERROR: Could not connect to Oura API: z
Testing sleep data access...r   )date	timedelta)daysz$/v2/usercollection/sleep?start_date=z
&end_date=datatotal_sleep_durationi  z  Last night's sleep: hz;  No sleep data for last night (normal if ring wasn't worn)z  Sleep data test: z (non-fatal)access_token   )indentz
Token saved to: zH
Setup complete! Luke will now pull health data from Oura at 8 AM daily.z?You can also text Luke 'oura' anytime to trigger a manual pull.)printinputstripsysexitr   urllibrequestRequestBASE_URLurlopenjsonloadsreadgeterror	HTTPErrorcodereasonURLErrorstr	Exceptiondatetimer   r   today	isoformatround
TOKEN_PATH
write_textdumps)tokenctxreqrespinfoer   r   	yesterdayr<   
sleep_datasessionslatesthours
token_datar   r   r   main2   s   



rN   __main__)__doc__r0   r	   r   r)   urllib.requestr+   urllib.errorpathlibr   __file__resolveparentr?   r.   r   rN   __name__r   r   r   r   <module>   s   [
