Skip to content

Commit 24492fd

Browse files
committed
chore: add Search Console submission helper
1 parent 47a787d commit 24492fd

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

‎scripts/gsc_submit.py‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python3
2+
import json
3+
from pathlib import Path
4+
5+
from google.oauth2.credentials import Credentials
6+
from google.auth.transport.requests import Request
7+
from googleapiclient.discovery import build
8+
from googleapiclient.errors import HttpError
9+
10+
TOKEN = Path('/home/codex/.hermes/gsc_token.json')
11+
SITE_CANDIDATES = [
12+
'https://debate.pi-stack.org/',
13+
'sc-domain:pi-stack.org',
14+
]
15+
SITEMAP = 'https://debate.pi-stack.org/sitemap.xml'
16+
URLS = [
17+
'https://debate.pi-stack.org/',
18+
'https://debate.pi-stack.org/ai-debate-practice',
19+
'https://debate.pi-stack.org/debate-case-generator',
20+
'https://debate.pi-stack.org/debate-rebuttal-practice',
21+
'https://debate.pi-stack.org/ai-debate-coach',
22+
'https://debate.pi-stack.org/terms',
23+
'https://debate.pi-stack.org/privacy',
24+
]
25+
26+
def load_creds():
27+
data=json.loads(TOKEN.read_text())
28+
creds=Credentials(
29+
token=data.get('token'),
30+
refresh_token=data.get('refresh_token'),
31+
token_uri=data.get('token_uri'),
32+
client_id=data.get('client_id'),
33+
client_secret=data.get('client_secret'),
34+
scopes=data.get('scopes'),
35+
)
36+
if creds.expired and creds.refresh_token:
37+
creds.refresh(Request())
38+
data['token']=creds.token
39+
if creds.expiry:
40+
data['expiry']=creds.expiry.isoformat()
41+
TOKEN.write_text(json.dumps(data, indent=2))
42+
TOKEN.chmod(0o600)
43+
return creds
44+
45+
def main():
46+
service=build('searchconsole','v1',credentials=load_creds(),cache_discovery=False)
47+
sites=service.sites().list().execute().get('siteEntry', [])
48+
print('SITES')
49+
print(json.dumps(sites, indent=2))
50+
site_url=None
51+
for cand in SITE_CANDIDATES:
52+
if any(s.get('siteUrl') == cand for s in sites):
53+
site_url=cand
54+
break
55+
if not site_url:
56+
# Try any property that can cover target.
57+
for s in sites:
58+
su=s.get('siteUrl','')
59+
if 'pi-stack.org' in su or 'debate.pi-stack.org' in su:
60+
site_url=su
61+
break
62+
if not site_url:
63+
raise SystemExit('NO_MATCHING_PROPERTY')
64+
print('USING_SITE', site_url)
65+
66+
print('SUBMIT_SITEMAP', SITEMAP)
67+
try:
68+
service.sitemaps().submit(siteUrl=site_url, feedpath=SITEMAP).execute()
69+
print('SITEMAP_SUBMIT_OK')
70+
except HttpError as e:
71+
print('SITEMAP_SUBMIT_ERROR', e.status_code, e.reason, e.content.decode('utf-8','ignore')[:1000])
72+
73+
print('SITEMAPS')
74+
try:
75+
print(json.dumps(service.sitemaps().list(siteUrl=site_url).execute(), indent=2)[:5000])
76+
except HttpError as e:
77+
print('SITEMAPS_LIST_ERROR', e.status_code, e.reason, e.content.decode('utf-8','ignore')[:1000])
78+
79+
print('URL_INSPECTION')
80+
inspect=build('searchconsole','v1',credentials=load_creds(),cache_discovery=False).urlInspection().index().inspect
81+
for u in URLS:
82+
try:
83+
res=inspect(body={'inspectionUrl':u,'siteUrl':site_url}).execute()
84+
idx=res.get('inspectionResult',{}).get('indexStatusResult',{})
85+
print(json.dumps({'url':u,'verdict':idx.get('verdict'),'coverageState':idx.get('coverageState'),'robotsTxtState':idx.get('robotsTxtState'),'indexingState':idx.get('indexingState'),'lastCrawlTime':idx.get('lastCrawlTime'),'pageFetchState':idx.get('pageFetchState'),'googleCanonical':idx.get('googleCanonical'),'userCanonical':idx.get('userCanonical')}, indent=2))
86+
except HttpError as e:
87+
print(json.dumps({'url':u,'error_status':e.status_code,'reason':e.reason,'body':e.content.decode('utf-8','ignore')[:500]}, indent=2))
88+
89+
if __name__ == '__main__':
90+
main()

0 commit comments

Comments
 (0)