
    J`i                        S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  S/r " S S	5      r\S
:X  a  \" S5        \" S5        \" 5       r \R#                  5       r\" S5        \" S5        \R'                  5       r\(       a4  \" S\" \5       S35        \" \S5       H  u  rr\" S\ S\ 35        M     g\" S5        gg! \ a  r\" \5         SrCgSrCf\ a  r\" S\ 35         SrCgSrCff = f)zm
Google Search Console Authentication Handler
Handles OAuth 2.0 authentication for Google Search Console API
    N)Request)Credentials)InstalledAppFlow)buildz3https://www.googleapis.com/auth/webmasters.readonlyc                   0    \ rS rSrSS jrS rS rS rSrg)	GSCAuthenticator   c                 8    Xl         X l        SU l        SU l        g)z
Initialize GSC authenticator

Args:
    credentials_file: Path to OAuth 2.0 client credentials JSON
    token_file: Path to store authentication token
N)credentials_file
token_filecredsservice)selfr   r   s      OC:\Users\lafte\Apps\Conservatory Transformations\seo_tools\gsc_authenticator.py__init__GSCAuthenticator.__init__   s     !1$
    c                 |   [         R                  R                  U R                  5      (       a:  [	        U R                  S5       n[
        R                  " U5      U l        SSS5        U R                  (       a  U R                  R                  (       Gd_  U R                  (       ae  U R                  R                  (       aJ  U R                  R                  (       a/  [        S5        U R                  R                  [        5       5        O[         R                  R                  U R                  5      (       d,  [        SS SU R                   SU R                   SS S3	5      e[        S	5        [         R"                  " U R                  [$        5      nUR'                  S
S9U l        [	        U R                  S5       n[
        R(                  " U R                  U5        SSS5        [        S5        [+        SSU R                  S9U l        U R,                  $ ! , (       d  f       GN= f! , (       d  f       NQ= f)z`
Authenticate with Google Search Console API

Returns:
    Google Search Console service object
rbNz!Refreshing expired credentials...
<============================================================z
ERROR: a   not found!

To set up Google Search Console API:
1. Go to https://console.cloud.google.com/
2. Create a new project or select existing
3. Enable 'Google Search Console API'
4. Create OAuth 2.0 credentials (Desktop app)
5. Download credentials and save as 'z'
zStarting OAuth flow...r   )portwbzAuthentication successful!searchconsolev1)credentials)ospathexistsr   openpickleloadr   validexpiredrefresh_tokenprintrefreshr   r   FileNotFoundErrorr   from_client_secrets_fileSCOPESrun_local_serverdumpr   r   )r   tokenflows      r   authenticateGSCAuthenticator.authenticate   s    77>>$//**doot,#[[/
 - zz!1!1!1zzdjj00TZZ5M5M9:

""79-ww~~d&;&;<<+VH %""&"7"7!8 9@ AE@U@U?VVY!("&
 
 ./'@@))63!222:
 doot,DJJ. -./ _d

K||E -,8 -,s   H "H-
H*-
H;c                 V   U R                   (       d  U R                  5          U R                   R                  5       R                  5       R	                  5       nUR                  S/ 5       Vs/ s H  o"S   PM	     nnU$ s  snf ! [         a  n[        SU 35        / s SnA$ SnAff = f)z_
List all Search Console properties the user has access to

Returns:
    List of property URLs
	siteEntrysiteUrlzError listing properties: N)r   r/   siteslistexecuteget	Exceptionr&   )r   	site_listsite
propertieses        r   list_properties GSCAuthenticator.list_propertiesL   s     ||	**,113;;=I6?mmKQS6TU6Tdy/6TJU V 	.qc23I	s0   A
B -A?;B ?B 
B(B#B(#B(c                 *    U R                  5       nX;   $ )z
Verify access to a specific property

Args:
    property_url: URL of the property to verify
    
Returns:
    Boolean indicating access
)r=   )r   property_urlr;   s      r   verify_property_access'GSCAuthenticator.verify_property_access^   s     ))+
))r   )r   r   r   r   N)zcredentials.jsonztoken.pickle)	__name__
__module____qualname____firstlineno__r   r/   r=   rA   __static_attributes__ r   r   r   r      s    +Z$*r   r   __main__z/Testing Google Search Console Authentication...r   u   
✓ Authentication successful!z+
Fetching your Search Console properties...z
Found z properties:   z  z. zB
No properties found. Make sure you have access to Search Console.u   
✗ Error: )__doc__r   jsonr!   google.auth.transport.requestsr   google.oauth2.credentialsr   google_auth_oauthlib.flowr   googleapiclient.discoveryr   r*   r   rC   r&   authr/   r   r=   r;   len	enumerateipropr(   r<   r8   rH   r   r   <module>rV      s  
 
   2 1 6 + @	@X* X*v z	
;<	&MD###%01<=))+
HS_-\:;$Z341#Rv&' 4 WX' *  a #aS!""#s+   A*C =C C8C	C8#C33C8