PulseCanvas / website / scripts / check-seo.py
| 1 | """Validate built metadata, structured data, assets, sitemap, and internal links.""" |
| 2 | from html.parser import HTMLParser |
| 3 | from pathlib import Path |
| 4 | from urllib.parse import urlsplit, unquote, urljoin |
| 5 | import json |
| 6 | import struct |
| 7 | import sys |
| 8 | from datetime import datetime |
| 9 | import xml.etree.ElementTree as ET |
| 10 | |
| 11 | root = Path(sys.argv[1] if len(sys.argv) > 1 else 'dist') |
| 12 | class Page(HTMLParser): |
| 13 | def __init__(self): |
| 14 | super().__init__(); self.meta = []; self.links = []; self.ids = set(); self.h1 = 0; self.title = 0; self.schema = []; self.in_schema = False; self.buffer = ''; self.title_text = ''; self.in_title = False; self.lang = None |
| 15 | def handle_starttag(self, tag, attrs): |
| 16 | a = dict(attrs) |
| 17 | if 'id' in a: |
| 18 | assert a['id'] not in self.ids, ('duplicate ID', a['id']) |
| 19 | self.ids.add(a['id']) |
| 20 | if tag == 'html': self.lang = a.get('lang') |
| 21 | if tag == 'img': assert 'alt' in a, ('missing image alt', a.get('src')) |
| 22 | if tag == 'h1': self.h1 += 1 |
| 23 | if tag == 'title': self.title += 1; self.in_title = True |
| 24 | if tag == 'meta': self.meta.append(a) |
| 25 | if tag == 'link': self.links.append((tag, a)) |
| 26 | if tag in ('a', 'img', 'source', 'track', 'script', 'video'): self.links.append((tag, a)) |
| 27 | if tag == 'script' and a.get('type') == 'application/ld+json': self.in_schema = True; self.buffer = '' |
| 28 | def handle_data(self, text): |
| 29 | if self.in_schema: self.buffer += text |
| 30 | if self.in_title: self.title_text += text |
| 31 | def handle_endtag(self, tag): |
| 32 | if tag == 'title': self.in_title = False |
| 33 | if tag == 'script' and self.in_schema: |
| 34 | self.schema.append(json.loads(self.buffer)); self.in_schema = False |
| 35 | |
| 36 | pages = {} |
| 37 | for path in root.rglob('index.html'): |
| 38 | route = '/' + str(path.parent.relative_to(root)).replace('\\','/') + '/' |
| 39 | route = '/' if route == '/./' else route |
| 40 | page = Page(); page.feed(path.read_text()); pages[route] = page |
| 41 | |
| 42 | descriptions = set() |
| 43 | titles = set() |
| 44 | origin = 'https://pulsecanvas.ai' |
| 45 | def meta_value(page, key, value): |
| 46 | matches = [m.get('content', '') for m in page.meta if m.get(key) == value] |
| 47 | assert len(matches) == 1 and matches[0].strip(), (value, 'missing or duplicate metadata') |
| 48 | return matches[0] |
| 49 | def local_asset(url): |
| 50 | parsed = urlsplit(url) |
| 51 | assert parsed.scheme == 'https' and parsed.netloc == 'pulsecanvas.ai', ('asset must be canonical HTTPS', url) |
| 52 | asset = root / unquote(parsed.path).lstrip('/') |
| 53 | assert asset.is_file(), ('missing metadata/schema asset', url) |
| 54 | return asset |
| 55 | def schema_assets(value): |
| 56 | if isinstance(value, dict): |
| 57 | for key, child in value.items(): |
| 58 | if key in ['image', 'screenshot', 'logo', 'thumbnailUrl', 'contentUrl']: |
| 59 | for url in child if isinstance(child, list) else [child]: |
| 60 | if isinstance(url, str) and url.startswith(origin): local_asset(url) |
| 61 | schema_assets(child) |
| 62 | elif isinstance(value, list): |
| 63 | for child in value: schema_assets(child) |
| 64 | for route, page in pages.items(): |
| 65 | assert page.h1 == 1 and page.title == 1, (route, 'heading/title count') |
| 66 | assert page.lang == 'en', (route, 'document language') |
| 67 | assert page.title_text.strip() and page.title_text not in titles, (route, 'empty/duplicate title') |
| 68 | titles.add(page.title_text) |
| 69 | robots = meta_value(page, 'name', 'robots') |
| 70 | assert 'noindex' not in robots and 'nofollow' not in robots, (route, 'indexing blocked') |
| 71 | assert meta_value(page, 'name', 'viewport').startswith('width=device-width') |
| 72 | description = [m['content'] for m in page.meta if m.get('name') == 'description'] |
| 73 | assert len(description) == 1 and description[0] not in descriptions, (route, 'description') |
| 74 | descriptions.add(description[0]) |
| 75 | canonical = [a['href'] for tag, a in page.links if a.get('rel') == 'canonical'] |
| 76 | assert canonical == ['https://pulsecanvas.ai' + route], (route, canonical) |
| 77 | for property in ['og:title','og:description','og:type','og:url','og:site_name','og:locale','og:image','og:image:secure_url','og:image:type','og:image:width','og:image:height','og:image:alt']: |
| 78 | meta_value(page, 'property', property) |
| 79 | assert meta_value(page, 'property', 'og:url') == canonical[0] |
| 80 | assert meta_value(page, 'property', 'og:description') == description[0] |
| 81 | assert meta_value(page, 'name', 'twitter:card') == 'summary_large_image' |
| 82 | for name in ['twitter:title','twitter:description','twitter:image','twitter:image:alt']: |
| 83 | meta_value(page, 'name', name) |
| 84 | og_image = meta_value(page, 'property', 'og:image') |
| 85 | assert meta_value(page, 'property', 'og:image:secure_url') == og_image |
| 86 | assert meta_value(page, 'name', 'twitter:image') == og_image |
| 87 | assert meta_value(page, 'name', 'twitter:title') == meta_value(page, 'property', 'og:title') |
| 88 | assert meta_value(page, 'name', 'twitter:description') == description[0] |
| 89 | image = local_asset(og_image) |
| 90 | with image.open('rb') as png: |
| 91 | header = png.read(24) |
| 92 | assert header[:8] == b'\x89PNG\r\n\x1a\n', ('invalid PNG', image) |
| 93 | width, height = struct.unpack('>II', header[16:24]) |
| 94 | assert (width, height) == (1200, 630), (route, 'social image dimensions') |
| 95 | assert meta_value(page, 'property', 'og:image:width') == str(width) |
| 96 | assert meta_value(page, 'property', 'og:image:height') == str(height) |
| 97 | assert meta_value(page, 'property', 'og:image:type') == 'image/png' |
| 98 | assert len(page.schema) == 1 |
| 99 | graph = page.schema[0]['@graph'] |
| 100 | schema_assets(graph) |
| 101 | if route.startswith('/guides/') and route != '/guides/': |
| 102 | article = next(node for node in graph if node['@type'] == 'TechArticle') |
| 103 | assert article['headline'] == page.title_text and article['author'] and article['image'] |
| 104 | if route == '/': assert any(node['@type'] == 'SoftwareApplication' and node['offers']['price'] == 0 for node in graph) |
| 105 | for node in graph: |
| 106 | if node['@type'] == 'VideoObject': |
| 107 | published = datetime.fromisoformat(node['uploadDate'].replace('Z', '+00:00')) |
| 108 | assert published.tzinfo is not None, (route, 'video publication timezone') |
| 109 | assert node['contentUrl'].startswith('https://media.protomota.com/video/'), (route, 'video plays from R2') |
| 110 | assert not any('aggregateRating' in node or 'review' in node for node in graph), 'No fabricated reviews' |
| 111 | for tag, attrs in page.links: |
| 112 | for attribute in ['href','src','poster','data-poster']: |
| 113 | url = attrs.get(attribute, '') |
| 114 | if not url or urlsplit(url).scheme or url.startswith('//'): continue |
| 115 | parsed = urlsplit(url); path = unquote(parsed.path) |
| 116 | if tag == 'use': continue |
| 117 | if not path: |
| 118 | if parsed.fragment: assert parsed.fragment in page.ids, (route,url) |
| 119 | continue |
| 120 | if not path.startswith('/'): |
| 121 | path = urlsplit(urljoin(origin + route, url)).path |
| 122 | target = root / path.lstrip('/') |
| 123 | if path == '/': target = root/'index.html' |
| 124 | if path.endswith('/') and path != '/': target /= 'index.html' |
| 125 | assert target.exists(), (route, 'missing asset/link',url) |
| 126 | if parsed.fragment and path in pages: |
| 127 | assert parsed.fragment in pages[path].ids, (route,url) |
| 128 | |
| 129 | urls = {node.text for node in ET.parse(root/'sitemap.xml').getroot().iter('{http://www.sitemaps.org/schemas/sitemap/0.9}loc')} |
| 130 | assert urls == {'https://pulsecanvas.ai'+route for route in pages} |
| 131 | robots = (root/'robots.txt').read_text() |
| 132 | assert 'Disallow' not in robots and robots.count('Allow: /') == 2 |
| 133 | for crawler in ['OAI-SearchBot', 'GPTBot', 'ClaudeBot', 'Claude-SearchBot', 'PerplexityBot', 'Google-Extended']: |
| 134 | assert f'User-agent: {crawler}\n' in robots, crawler |
| 135 | llms = (root/'llms.txt').read_text() |
| 136 | assert llms.startswith('# PulseCanvas Studio\n') and 'https://pulsecanvas.ai/download/' in llms |
| 137 | support = next(node for node in pages['/help/'].schema[0]['@graph'] if node['@type'] == 'FAQPage') |
| 138 | assert len(support['mainEntity']) >= 3 and all(q['name'] and q['acceptedAnswer']['text'] for q in support['mainEntity']) |
| 139 | assert 'Sitemap: https://pulsecanvas.ai/sitemap.xml' in robots |
| 140 | assert {'/download/', '/help/'} <= pages.keys() |
| 141 | print(f'{len(pages)} pages: unique metadata, social-image files/dimensions, JSON-LD assets, sitemap, crawler directives, image alt text, and internal links passed.') |