2022-07-10 04:04:28 -05:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any
|
2022-07-17 13:43:03 -05:00
|
|
|
from urllib.parse import urlparse
|
2022-07-10 04:04:28 -05:00
|
|
|
|
2022-07-10 12:19:55 -05:00
|
|
|
from app.utils import microformats
|
2022-07-10 04:04:28 -05:00
|
|
|
from app.utils.url import make_abs
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class IndieAuthClient:
|
|
|
|
logo: str | None
|
|
|
|
name: str
|
|
|
|
url: str
|
|
|
|
|
|
|
|
|
|
|
|
def _get_prop(props: dict[str, Any], name: str, default=None) -> Any:
|
|
|
|
if name in props:
|
|
|
|
items = props.get(name)
|
|
|
|
if isinstance(items, list):
|
|
|
|
return items[0]
|
|
|
|
return items
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
async def get_client_id_data(url: str) -> IndieAuthClient | None:
|
2022-07-17 13:43:03 -05:00
|
|
|
# Don't fetch localhost URL
|
2022-08-15 03:27:58 -05:00
|
|
|
if urlparse(url).hostname == "localhost":
|
2022-07-17 13:43:03 -05:00
|
|
|
return IndieAuthClient(
|
|
|
|
logo=None,
|
|
|
|
name=url,
|
|
|
|
url=url,
|
|
|
|
)
|
|
|
|
|
2022-07-10 12:19:55 -05:00
|
|
|
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]
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
2022-07-10 04:04:28 -05:00
|
|
|
|
|
|
|
return IndieAuthClient(
|
|
|
|
logo=None,
|
|
|
|
name=url,
|
|
|
|
url=url,
|
|
|
|
)
|