1212`--all` submits every URL in the live sitemap and every redirect stub under `static/`, which suits
1313only a relaunch or migration; after one, submit only the pages that changed. `--dry-run` prints the
1414list and whether the live key file matches, without sending anything. `--from` reads the sitemap
15- and the key file from another origin, such as `npm run serve`, and `--endpoint` sends to another
16- IndexNow endpoint.
15+ from another origin, such as `npm run serve`, and `--endpoint` sends to another IndexNow endpoint.
1716
18- It refuses a URL on another host, and refuses to send while the live key file is missing or differs
19- from the one in `static/`. It exits 0 when the endpoint answers 200 or 202, and 1 otherwise.
17+ It refuses a URL on another host, and refuses to send while the key file on the live site is
18+ missing or differs from the one in `static/`, since the engines check the live site. It exits 0
19+ when the endpoint answers 200 or 202, and 1 otherwise.
2020"""
2121import argparse
2222import json
@@ -62,10 +62,16 @@ def fetch(url):
6262 try :
6363 with urllib .request .urlopen (url , timeout = 30 ) as response :
6464 return response .read ().decode ()
65- except (urllib . error . URLError , TimeoutError ):
65+ except (OSError , UnicodeDecodeError ):
6666 return None
6767
6868
69+ def live_key_matches (key ):
70+ """Return whether the live site serves the key file the engines will check."""
71+ live = fetch (f"{ SITE } /{ key } .txt" )
72+ return live is not None and live .strip () == key
73+
74+
6975def stub_urls ():
7076 """Return the site URL of each redirect stub under `static/`."""
7177 urls = []
@@ -90,7 +96,7 @@ def main():
9096 parser .add_argument ("urls" , nargs = "*" , help = "the changed pages' URLs" )
9197 parser .add_argument ("--all" , action = "store_true" , help = "every sitemap URL and redirect stub" )
9298 parser .add_argument ("--dry-run" , action = "store_true" , help = "print the list without sending" )
93- parser .add_argument ("--from" , dest = "origin" , default = SITE , help = "where to read the sitemap and key " )
99+ parser .add_argument ("--from" , dest = "origin" , default = SITE , help = "where to read the sitemap" )
94100 parser .add_argument ("--endpoint" , default = ENDPOINT , help = "the IndexNow endpoint" )
95101 args = parser .parse_args ()
96102
@@ -99,14 +105,13 @@ def main():
99105 key = find_key ()
100106 urls = all_urls (args .origin .rstrip ("/" )) if args .all else args .urls
101107
102- foreign = [url for url in urls if urlsplit (url ).scheme != "https" or urlsplit (url ).netloc != HOST ]
108+ foreign = [u for u in urls if urlsplit (u ).scheme != "https" or urlsplit (u ).netloc != HOST ]
103109 if foreign :
104110 sys .exit ("not on https://" + HOST + ":\n " + "\n " .join (foreign ))
105111 if len (urls ) > MAX_URLS :
106112 sys .exit (f"{ len (urls )} URLs exceed IndexNow's limit of { MAX_URLS } per request" )
107113
108- live = fetch (f"{ args .origin .rstrip ('/' )} /{ key } .txt" )
109- key_ok = live is not None and live .strip () == key
114+ key_ok = live_key_matches (key )
110115 key_status = "matches" if key_ok else "is missing or differs from static/"
111116 count = f"{ len (urls )} URL" if len (urls ) == 1 else f"{ len (urls )} URLs"
112117
@@ -131,8 +136,8 @@ def main():
131136 status = response .status
132137 except urllib .error .HTTPError as error :
133138 status = error .code
134- except urllib . error . URLError as error :
135- sys .exit (f"not sent: { error . reason } " )
139+ except OSError as error :
140+ sys .exit (f"not sent: { getattr ( error , ' reason' , error ) } " )
136141 print (f"{ count } sent to { args .endpoint } : { status } { MEANINGS .get (status , 'unexpected' )} " )
137142 return 0 if status in (200 , 202 ) else 1
138143
0 commit comments