Tweak lookup

This commit is contained in:
Thomas Sileo 2019-08-07 21:38:50 +02:00
parent b42f82acde
commit 5bc655dfd3

View file

@ -4,6 +4,7 @@ import little_boxes.activitypub as ap
import mf2py import mf2py
import requests import requests
from little_boxes.errors import NotAnActivityError from little_boxes.errors import NotAnActivityError
from little_boxes.errors import RemoteServerUnavailableError
from little_boxes.webfinger import get_actor_url from little_boxes.webfinger import get_actor_url
@ -20,14 +21,20 @@ def lookup(url: str) -> ap.BaseActivity:
# Some websites may returns 404, 503 or others when they don't support webfinger, and we're just taking a guess # Some websites may returns 404, 503 or others when they don't support webfinger, and we're just taking a guess
# when performing the lookup. # when performing the lookup.
pass pass
except requests.RequestException as err:
raise RemoteServerUnavailableError(f"failed to fetch {url}: {err!r}")
backend = ap.get_backend() backend = ap.get_backend()
resp = requests.get( try:
url, resp = requests.head(
timeout=15, url,
allow_redirects=False, timeout=10,
headers={"User-Agent": backend.user_agent()}, allow_redirects=True,
) headers={"User-Agent": backend.user_agent()},
)
except requests.RequestException as err:
raise RemoteServerUnavailableError(f"failed to GET {url}: {err!r}")
resp.raise_for_status() resp.raise_for_status()
# If the page is HTML, maybe it contains an alternate link pointing to an AP object # If the page is HTML, maybe it contains an alternate link pointing to an AP object