Webmention improvements

- Tweak design for IndieAuth login flow
 - Webmentions notifications support
 - Refactor webmentions processing
This commit is contained in:
Thomas Sileo 2022-07-19 20:38:32 +02:00
parent 9882fc555c
commit 0f6915fdbb
9 changed files with 204 additions and 90 deletions

View file

@ -7,19 +7,28 @@ from loguru import logger
from app import config
async def fetch_and_parse(url: str) -> tuple[dict[str, Any], str] | None:
class URLNotFoundOrGone(Exception):
pass
async def fetch_and_parse(url: str) -> tuple[dict[str, Any], str]:
async with httpx.AsyncClient() as client:
resp = await client.get(
url,
headers={
"User-Agent": config.USER_AGENT,
},
follow_redirects=True,
)
if resp.status_code in [404, 410]:
raise URLNotFoundOrGone
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
except httpx.HTTPStatusError:
logger.error(
f"Failed to parse microformats for {url}: " f"got {resp.status_code}"
)
raise
return mf2py.parse(doc=resp.text), resp.text