""" Microformats2 consuming helpers (v2.8.0) Shared by Webmention processing and author discovery: - representative h-card of a page - the h-entry on a page that responds to a given URL - the author of an h-entry (authorship algorithm) References: - https://microformats.org/wiki/representative-h-card-parsing - https://indieweb.org/authorship-spec """ from typing import Iterator, Optional from urllib.parse import urldefrag, urlparse RESPONSE_PROPERTIES = ('in-reply-to', 'like-of', 'repost-of', 'bookmark-of') def normalize_url(url: str) -> str: """URL for comparison: no fragment, no trailing slash, lowercase scheme/host""" if not url: return '' url = urldefrag(url.strip())[0] parsed = urlparse(url) url = parsed._replace(scheme=parsed.scheme.lower(), netloc=parsed.netloc.lower()).geturl() return url.rstrip('/') def value_of(value) -> Optional[str]: """Plain value of an mf2 property value (strings, {'value':...}, {'html':...})""" if isinstance(value, dict): value = value.get('value') return value if isinstance(value, str) and value else None def first(item: dict, prop: str) -> Optional[str]: """First plain value of an item's property""" for value in item.get('properties', {}).get(prop, []): result = value_of(value) if result: return result return None def values(item: dict, prop: str) -> list[str]: """All plain values of an item's property""" return [v for v in (value_of(x) for x in item.get('properties', {}).get(prop, [])) if v] def walk(items: list) -> Iterator[dict]: """Every microformat in items, depth first: top level, children, nested properties""" for item in items: if not isinstance(item, dict) or 'type' not in item: continue yield item yield from walk(item.get('children', [])) for prop_values in item.get('properties', {}).values(): yield from walk([v for v in prop_values if isinstance(v, dict)]) def is_type(item: dict, mf_type: str) -> bool: return mf_type in item.get('type', []) def representative_hcard(parsed: dict, page_url: str) -> Optional[dict]: """ The page's representative h-card, per the parsing spec, in order: 1. An h-card whose uid and url both match the page URL 2. An h-card whose url matches one of the page's rel=me URLs 3. The only h-card on the page, if its url matches the page URL Otherwise the page has none. """ page = normalize_url(page_url) cards = [item for item in walk(parsed.get('items', [])) if is_type(item, 'h-card')] for card in cards: if page in map(normalize_url, values(card, 'uid')) and \ page in map(normalize_url, values(card, 'url')): return card rel_me = {normalize_url(u) for u in parsed.get('rels', {}).get('me', [])} for card in cards: if rel_me & set(map(normalize_url, values(card, 'url'))): return card if len(cards) == 1 and page in map(normalize_url, values(cards[0], 'url')): return cards[0] return None def responding_entry(parsed: dict, target_url: str) -> Optional[dict]: """ The h-entry on a page that refers to target_url Prefers an entry whose in-reply-to / like-of / repost-of / bookmark-of is the target, then one whose content links to it, then the first top-level h-entry. """ target = normalize_url(target_url) entries = [item for item in walk(parsed.get('items', [])) if is_type(item, 'h-entry')] top = [item for item in parsed.get('items', []) if is_type(item, 'h-entry')] if not target: return top[0] if top else (entries[0] if entries else None) for entry in entries: for prop in RESPONSE_PROPERTIES: if target in map(normalize_url, values(entry, prop)): return entry for entry in entries: for content in entry.get('properties', {}).get('content', []): html = content.get('html', '') if isinstance(content, dict) else str(content) if target_url in html or target in html: return entry return top[0] if top else (entries[0] if entries else None) def response_type(entry: dict, target_url: str) -> str: """reply / like / repost / bookmark if the entry is one to target_url, else mention""" target = normalize_url(target_url) for prop, kind in zip(RESPONSE_PROPERTIES, ('reply', 'like', 'repost', 'bookmark')): if target in map(normalize_url, values(entry, prop)): return kind return 'mention' def entry_author(parsed: dict, entry: Optional[dict], page_url: str) -> dict: """ Author of an h-entry, following the authorship algorithm without fetching 1. The entry's author property (an h-card, a URL, or a name) 2. The author of an h-feed that contains the entry 3. The page's rel=author link 4. The page's representative h-card Returns: {'name', 'url', 'photo'}; values may be None """ author = None if entry: author = (entry.get('properties', {}).get('author') or [None])[0] if author is None: for feed in walk(parsed.get('items', [])): if is_type(feed, 'h-feed') and entry in feed.get('children', []): author = (feed.get('properties', {}).get('author') or [None])[0] break if author is None: rel_author = parsed.get('rels', {}).get('author', []) author = rel_author[0] if rel_author else representative_hcard(parsed, page_url) if isinstance(author, dict) and 'type' in author: return { 'name': first(author, 'name'), 'url': first(author, 'url'), 'photo': first(author, 'photo'), } text = value_of(author) if text and urlparse(text).scheme in ('http', 'https'): return {'name': None, 'url': text, 'photo': None} return {'name': text, 'url': None, 'photo': None}