misskey2telegram/defs/check_node.py

32 lines
862 B
Python
Raw Normal View History

2023-08-07 05:55:43 +00:00
from httpx import URL, InvalidURL, AsyncClient
2023-07-20 14:21:37 +00:00
2023-08-07 05:55:43 +00:00
from init import headers
2023-07-20 14:21:37 +00:00
def get_host(url: str) -> str:
try:
url = URL(url)
except InvalidURL:
return ""
return url.host
async def check_host(host: str) -> bool:
if not host:
return False
try:
2023-08-07 05:55:43 +00:00
async with AsyncClient(timeout=60, headers=headers) as request:
req = await request.get(f"https://{host}/.well-known/nodeinfo")
req.raise_for_status()
node_url = req.json()["links"][0]["href"]
req = await request.get(node_url)
2023-07-20 14:21:37 +00:00
req.raise_for_status()
data = req.json()
if data["software"]["name"] != "misskey":
raise ValueError
if not data["software"]["version"].startswith("13."):
raise ValueError
return True
except Exception:
return False