"""Reproduce the known-url example with Python 3.9+. All inputs are synthetic. Runs offline."""
import argparse
from html.parser import HTMLParser
import json
from urllib.parse import urljoin, urlsplit
def result(key, method, columns, rows, limits):
return dict(key=key, method=method, columns=columns, rows=rows, limits=limits)
class Links(HTMLParser):
def __init__(self):
super().__init__()
self.anchors, self.canonicals = [], []
self.in_head = False
def handle_starttag(self, tag, attrs):
attr = dict(attrs)
if tag == 'head':
self.in_head = True
if tag == 'a' and attr.get('href'):
self.anchors.append(attr['href'])
if self.in_head and tag == 'link' and 'canonical' in attr.get('rel','').lower().split() and attr.get('href'):
self.canonicals.append(attr['href'])
def handle_endtag(self, tag):
if tag == 'head':
self.in_head = False
def known_url():
rows = []
target = '/p/42'
for linked, status, body in [(True,200,'SKU-42'),(False,200,'SKU-42'),(True,403,'Denied'),(True,200,'Sign in')]:
root = '42' if linked else '
42
'
pages = {'/':(200,root), target:(status,f'{body}')}
links = Links()
links.feed(pages['/'][1])
paths = {urlsplit(urljoin('https://shop.example/',ref)).path for ref in links.anchors}
code,html = pages[target]
marker = 'SKU-42' if 'SKU-42' in html else '∅'
rows.append([target if target in paths else '∅',str(code),marker])
return result('known-url',
'Compare two operations on four miniature in-memory sites. First extract one hop of '
'ordinary HTML links from the home page and look for /p/42. Then read /p/42 directly '
'from the simulated response map, even if no link led there. Record the status and the '
'literal SKU-42 marker. The third response is a refusal. The fourth is a sign-in page '
'served with status 200. ∅ means no matching link or no matching marker in that column.',
['Link from /', 'Direct status for /p/42', 'Marker in response'], rows,
'No Perplexity API, search index, live site or crawler is queried. This checks one '
'HTML path and a literal string only. A missing link from the home page does not rule '
'out other discovery paths. Reading bytes at a known URL does not prove discovery, '
'model extraction, ranking, citation or referral. Access permissions must be respected.')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('case', nargs='?', choices=['known-url'])
parser.parse_args()
print(json.dumps(known_url(), indent=2, ensure_ascii=False))