Repository navigation
S339 round 4: performance pass, Chere' share cards, cache-proof card URLs #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9203cf8
S339 round 4: performance pass, and /chere gets the join page's share…
claude 638fc5c
chore: recapture the build golden for the S339 home
claude 8427654
S339 round 4b: Chere' pages wear Chere' on the plate cards, and every…
claude 97b7a51
S339 round 4b: the join card sets "your phone." in mint, not "on your…
claude 8c1d55f
chore: recapture the build golden for the S339 home
claude ddcd2eb
S339: the share-card check and the page-repeats check live in the repo
claude b324ad7
chore: recapture the build golden for the S339 home
claude f4d74e8
S339: og-check.py exits 1 when a card check fails (Codex P2 on #60)
claude 5c53229
chore: recapture the build golden for the S339 home
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env python3 | ||
| # page-repeats.py (S339): usage: python3 page-repeats.py DIST_CLIENT_DIR | ||
| # Flags statements a page says twice inside <main> (closer, header, footer, | ||
| # menu skipped). It catches word-level repeats; read each page for the same | ||
| # idea said in new words, which it cannot see. | ||
| # Per page: find repeated or near-repeated statements inside <main>, ignoring | ||
| # the sitewide closer, header, footer and menu. Prints pairs to review by eye. | ||
| import re, sys, glob, html, difflib, os | ||
| from html.parser import HTMLParser | ||
| class P(HTMLParser): | ||
| def __init__(s): super().__init__(); s.stack=[]; s.blocks=[]; s.cur=None; s.inmain=0; s.skip=0 | ||
| def handle_starttag(s,t,a): | ||
| a=dict(a); c=a.get('class') or '' | ||
| if t=='main': s.inmain+=1 | ||
| if t in('script','style','svg','noscript') or any(k in c for k in ('wc-closer','closer','textbar','menu-panel','wc-foot','sr-only')): s.skip+=1; s.stack.append(('skip',t)); return | ||
| s.stack.append(('n',t)) | ||
| if s.inmain and not s.skip and t in('p','li','h1','h2','h3','h4','dt','dd','figcaption','summary','span','a') and s.cur is None and t not in('span','a'): s.cur=[t,''] | ||
| def handle_endtag(s,t): | ||
| while s.stack: | ||
| k,tt=s.stack.pop() | ||
| if k=='skip': s.skip-=1 | ||
| if tt==t: break | ||
| if t=='main': s.inmain-=1 | ||
| if s.cur and s.cur[0]==t: | ||
| txt=re.sub(r'\s+',' ',s.cur[1]).strip() | ||
| if len(txt)>25: s.blocks.append((t,txt)) | ||
| s.cur=None | ||
| def handle_data(s,d): | ||
| if s.cur is not None and not s.skip: s.cur[1]+=d | ||
| root=sys.argv[1] | ||
| for f in sorted(glob.glob(root+'/**/*.html',recursive=True)): | ||
| rel=os.path.relpath(f,root) | ||
| if any(x in rel for x in ('lamp','404','onboard','wall')): continue | ||
| p=P(); p.feed(open(f).read()); b=p.blocks; hits=[] | ||
| for i in range(len(b)): | ||
| for j in range(i+1,len(b)): | ||
| r=difflib.SequenceMatcher(None,b[i][1].lower(),b[j][1].lower()).ratio() | ||
| # shared long phrase | ||
| m=difflib.SequenceMatcher(None,b[i][1].lower(),b[j][1].lower()).find_longest_match(0,len(b[i][1]),0,len(b[j][1])) | ||
| if r>0.6 or m.size>=40: hits.append((round(r,2),m.size,b[i][1][:110],b[j][1][:110])) | ||
| if hits: | ||
| print('##',rel,len(b),'blocks') | ||
| for h in hits[:8]: print(' ',h[0],h[1],'|',h[2],'||',h[3]) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env python3 | ||
| """og-check.py (S339 round 4b): strict share-card check over every built page, then over HTTP. | ||
| Usage: python3 scripts/cards/og-check.py dist/client BASE_URL (after a build; BASE_URL is the host the build points its cards at) | ||
| Per page: og:title, og:description, og:url == canonical, og:image absolute https, | ||
| og:image:width/height match the real file, og:image:type matches the bytes, | ||
| og:image:alt, twitter:card summary_large_image, twitter:image == og:image, | ||
| title length, description length. Per card over HTTP: 200, content-type image/*, | ||
| under 300 KB (WhatsApp drops larger previews), 1200x630, RGB baseline-readable. | ||
| Exits 1 when anything is BAD, so a build or deploy gate can stop on it.""" | ||
| import sys, re, os, io, glob, html, urllib.request | ||
| from PIL import Image | ||
| dist, base = sys.argv[1].rstrip('/'), sys.argv[2].rstrip('/') | ||
|
|
||
| def meta(doc, key, attr='property'): | ||
| m = re.search(r'<meta %s="%s" content="([^"]*)"' % (attr, re.escape(key)), doc) | ||
| return html.unescape(m.group(1)) if m else None | ||
|
|
||
| def get(u): | ||
| req = urllib.request.Request(u, headers={'User-Agent': 'facebookexternalhit/1.1'}) | ||
| with urllib.request.urlopen(req, timeout=20) as r: | ||
| return r.status, r.headers.get('content-type', ''), r.read() | ||
|
|
||
| bad, cards = 0, {} | ||
| # Same skip as og-cards.mjs: /lamp is Preston's own page outside the business site. | ||
| SKIP = {'/lamp'} | ||
| for f in sorted(glob.glob(dist + '/**/*.html', recursive=True)): | ||
| page = f[len(dist):].replace('/index.html', '/').replace('.html', '') or '/' | ||
| if page in SKIP: | ||
| print(f'skip {page}'); continue | ||
| doc = open(f, encoding='utf-8').read() | ||
| probs = [] | ||
| noindex = 'name="robots" content="noindex' in doc | ||
| canon = re.search(r'<link rel="canonical" href="([^"]+)"', doc) | ||
| canon = canon.group(1) if canon else None | ||
| t, d = meta(doc, 'og:title'), meta(doc, 'og:description') | ||
| img, alt = meta(doc, 'og:image'), meta(doc, 'og:image:alt') | ||
| w, h, typ = meta(doc, 'og:image:width'), meta(doc, 'og:image:height'), meta(doc, 'og:image:type') | ||
| tw, twi = meta(doc, 'twitter:card', 'name'), meta(doc, 'twitter:image', 'name') | ||
| if not t: probs.append('no og:title') | ||
| elif len(t) > 70: probs.append(f'og:title {len(t)} chars') | ||
| if not d: probs.append('no og:description') | ||
| elif not 50 <= len(d) <= 200: probs.append(f'og:description {len(d)} chars') | ||
| if meta(doc, 'og:url') != canon: probs.append('og:url != canonical') | ||
| if not img: probs.append('NO og:image') | ||
| else: | ||
| if not img.startswith('https://'): probs.append('og:image not absolute https') | ||
| if not alt: probs.append('no og:image:alt') | ||
| if tw != 'summary_large_image': probs.append(f'twitter:card {tw}') | ||
| if twi != img: probs.append('twitter:image != og:image') | ||
| local = dist + re.sub(r'^https?://[^/]+', '', img).split('?')[0] | ||
| if not os.path.exists(local): probs.append('card file missing in build') | ||
| else: | ||
| data = open(local, 'rb').read() | ||
| im = Image.open(io.BytesIO(data)) | ||
| if (str(im.size[0]), str(im.size[1])) != (w, h): probs.append(f'declared {w}x{h}, real {im.size}') | ||
| if im.size != (1200, 630): probs.append(f'size {im.size}') | ||
| real = 'image/jpeg' if im.format == 'JPEG' else 'image/' + im.format.lower() | ||
| if typ != real: probs.append(f'og:image:type {typ}, bytes {real}') | ||
| if im.mode != 'RGB': probs.append(f'mode {im.mode}') | ||
| if im.format == 'JPEG' and im.info.get('progression'): probs.append('progressive jpeg') | ||
| if len(data) > 300_000: probs.append(f'{len(data)//1024} KB > 300') | ||
| cards[img] = page | ||
| bad += bool(probs) | ||
| print(f"{'BAD ' if probs else 'ok '}{page:42s} {'noindex ' if noindex else ''}{(img or '-').split('/', 3)[-1]:40s} {'; '.join(probs)}") | ||
|
|
||
| print('\nHTTP pass on', base) | ||
| for img, page in sorted(cards.items()): | ||
| path = re.sub(r'^https?://[^/]+', '', img) | ||
| try: | ||
| st, ct, data = get(base + path) | ||
| ok = st == 200 and ct.startswith('image/') and Image.open(io.BytesIO(data)).size == (1200, 630) | ||
| if not ok: bad += 1 | ||
| print(f"{'ok ' if ok else 'BAD '}{path:45s} {st} {ct} {len(data)//1024}KB") | ||
| except Exception as e: | ||
| bad += 1; print('BAD ', path, e) | ||
| print('PAGES', len(glob.glob(dist + '/**/*.html', recursive=True)), 'CARDS', len(cards), 'BAD', bad) | ||
| sys.exit(1 if bad else 0) | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a built page has invalid metadata or a card fails the HTTP checks, the script increments
badbut still exits successfully. A metadata-only fixture with no OpenGraph tags producesBAD 1and exit status 0, so invoking this strict checker from a build or deployment gate will silently allow broken share cards through. Exit with a nonzero status whenbadis nonzero.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f4d74e8: exits 1 when anything is BAD. A page with no og tags exits 1; the preview build exits 0.