2018-07-20 22:15:47 +00:00
|
|
|
import json
|
|
|
|
|
2018-07-20 23:05:51 +00:00
|
|
|
import little_boxes.activitypub as ap
|
2018-07-20 22:15:47 +00:00
|
|
|
import mf2py
|
2018-07-20 23:05:51 +00:00
|
|
|
import requests
|
2018-07-22 09:44:42 +00:00
|
|
|
from little_boxes.errors import NotAnActivityError
|
2018-07-22 10:04:18 +00:00
|
|
|
from little_boxes.webfinger import get_actor_url
|
2018-07-20 22:15:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def lookup(url: str) -> ap.BaseActivity:
|
|
|
|
"""Try to find an AP object related to the given URL."""
|
2018-07-21 09:41:49 +00:00
|
|
|
try:
|
2019-04-13 08:00:56 +00:00
|
|
|
if url.startswith("@"):
|
2018-07-30 07:41:04 +00:00
|
|
|
actor_url = get_actor_url(url)
|
|
|
|
if actor_url:
|
|
|
|
return ap.fetch_remote_activity(actor_url)
|
2018-07-22 09:44:42 +00:00
|
|
|
except NotAnActivityError:
|
2018-07-21 09:41:49 +00:00
|
|
|
pass
|
2018-07-26 20:39:12 +00:00
|
|
|
except requests.HTTPError:
|
|
|
|
# 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.
|
|
|
|
pass
|
2018-07-21 09:41:49 +00:00
|
|
|
|
2018-07-20 22:15:47 +00:00
|
|
|
backend = ap.get_backend()
|
|
|
|
resp = requests.get(
|
|
|
|
url,
|
|
|
|
timeout=15,
|
|
|
|
allow_redirects=False,
|
|
|
|
headers={"User-Agent": backend.user_agent()},
|
|
|
|
)
|
|
|
|
resp.raise_for_status()
|
|
|
|
|
|
|
|
# If the page is HTML, maybe it contains an alternate link pointing to an AP object
|
|
|
|
for alternate in mf2py.parse(resp.text).get("alternates", []):
|
|
|
|
if alternate.get("type") == "application/activity+json":
|
|
|
|
return ap.fetch_remote_activity(alternate["url"])
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Maybe the page was JSON-LD?
|
|
|
|
data = resp.json()
|
|
|
|
return ap.parse_activity(data)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Try content negotiation (retry with the AP Accept header)
|
|
|
|
return ap.fetch_remote_activity(url)
|