
    i                         S r SSKrSSKrSSKrSSKJr  \R                  " S5      r\R                  " 5       S-  r	\	S-  r
\R                  " SS5      r\R                  " S	S5      rS
r/ SQrS rS rS rg)u  
Spotify OAuth2 Handler for Luke.

Manages authorization, token refresh, and service access for Spotify.
Uses Authorization Code flow with spotipy's built-in redirect handling.

Redirect URI: http://127.0.0.1:8091
  → Spotify allows http for 127.0.0.1 (but NOT "localhost")
  → Add exactly this in Spotify Developer Dashboard > Settings > Redirect URIs

Scopes:
  - user-read-currently-playing: what's playing now
  - user-read-recently-played: recent listening history
  - user-read-playback-state: playback device info
  - user-modify-playback-state: play/pause/skip/queue
  - user-top-read: top artists and tracks
  - playlist-read-private: read private playlists
  - playlist-modify-public: create/edit playlists
  - playlist-modify-private: create/edit private playlists

Setup:
  1. Add SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET to ~/clawd/.env
  2. In Spotify Dashboard > Settings > Redirect URIs, add: http://127.0.0.1:8091
  3. Run: python3 setup_spotify_auth.py
  4. Authorize in browser
  5. Token auto-refreshes from there
    N)Pathzspotify-authclawdz.spotify-token.jsonSPOTIFY_CLIENT_ID SPOTIFY_CLIENT_SECRETzhttp://127.0.0.1:8091)	zuser-read-currently-playingzuser-read-recently-playedzuser-read-playback-statezuser-modify-playback-statezuser-top-readzuser-follow-readzplaylist-read-privatezplaylist-modify-publiczplaylist-modify-privatec                  :    [        [        =(       a    [        5      $ )z%Check if Spotify credentials are set.)boolr   r        3/Users/bsyrros/clawd/whatsapp-agent/spotify_auth.pyis_configuredr   =   s    !;&;<<r   c            	         [        5       (       d  [        R                  S5        g SSKn SSKJn  U" [        [        [        SR                  [        5      [        [        5      SS9nUR                  5       nU(       d  [        R                  S5        gU R                  US	9nUR!                  5         [        R                  S
5        U$ ! ["         a"  n[        R%                  SU 35         SnAgSnAff = f)zZ
Get an authenticated Spotipy client.
Returns None if not configured or token is missing.
zOSpotify not configured. Set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in .envNr   SpotifyOAuth F	client_idclient_secretredirect_uriscope
cache_pathopen_browserz8No Spotify token found. Run setup_spotify_auth.py first.)auth_managerzSpotify client: ACTIVEzSpotify client init failed: )r   loggerinfospotipyspotipy.oauth2r   r   r   SPOTIFY_REDIRECT_URIjoinSCOPESstr
TOKEN_PATHget_cached_tokenwarningSpotifycurrent_user	Exceptionerror)r   r   r   
token_infoclientes         r   get_spotify_clientr,   B   s    
 ??ef/#'/-((6":
 "224
NNUVl;,- 3A378s   A-C 5C 
C7C22C7c            	          SSK n SSKJn  U" [        [        [
        SR                  [        5      [        [        5      SS9$ )z4Get the SpotifyOAuth manager (used by setup script).r   Nr   r   Tr   )
r   r   r   r   r   r   r   r    r!   r"   )r   r   s     r   get_auth_managerr.   i   s4    +#+)hhvz? r   )__doc__osjsonloggingpathlibr   	getLoggerr   home	CLAWD_DIRr"   getenvr   r   r   r    r   r,   r.   r
   r   r   <module>r8      s   8 
   			>	* IIK'!	..
 II126 		"92>  / 

=
$Nr   