Bootstrap webmention endpoint

This commit is contained in:
Thomas Sileo 2022-07-10 19:19:55 +02:00
parent 1b3c76ee2f
commit c6bc53ce54
5 changed files with 104 additions and 29 deletions

View file

@ -1,11 +1,7 @@
from dataclasses import dataclass
from typing import Any
import httpx
import mf2py # type: ignore
from loguru import logger
from app import config
from app.utils import microformats
from app.utils.url import make_abs
@ -26,31 +22,20 @@ def _get_prop(props: dict[str, Any], name: str, default=None) -> Any:
async def get_client_id_data(url: str) -> IndieAuthClient | None:
async with httpx.AsyncClient() as client:
try:
resp = await client.get(
url,
headers={
"User-Agent": config.USER_AGENT,
},
follow_redirects=True,
)
resp.raise_for_status()
except (httpx.HTTPError, httpx.HTTPStatusError):
logger.exception(f"Failed to discover webmention endpoint for {url}")
return None
maybe_data_and_html = await microformats.fetch_and_parse(url)
if maybe_data_and_html is not None:
data: dict[str, Any] = maybe_data_and_html[0]
data = mf2py.parse(doc=resp.text)
for item in data["items"]:
if "h-x-app" in item["type"] or "h-app" in item["type"]:
props = item.get("properties", {})
print(props)
logo = _get_prop(props, "logo")
return IndieAuthClient(
logo=make_abs(logo, url) if logo else None,
name=_get_prop(props, "name"),
url=_get_prop(props, "url", url),
)
for item in data["items"]:
if "h-x-app" in item["type"] or "h-app" in item["type"]:
props = item.get("properties", {})
print(props)
logo = _get_prop(props, "logo")
return IndieAuthClient(
logo=make_abs(logo, url) if logo else None,
name=_get_prop(props, "name"),
url=_get_prop(props, "url", url),
)
return IndieAuthClient(
logo=None,