Use host-meta to find webfinger url, indent-flatten by raise calls

This commit is contained in:
jesopo 2019-08-05 15:41:24 +01:00
parent 20042edfd9
commit 357abba00d

View file

@ -1,7 +1,7 @@
import urllib.parse
from src import ModuleManager, utils
WEBFINGER = "https://%s/.well-known/webfinger"
HOSTMETA = "https://%s/.well-known/host-meta"
WEBFINGER_HEADERS = {"Accept": "application/jrd+json"}
ACTIVITY_TYPE = "application/activity+json"
@ -22,12 +22,27 @@ class Module(ModuleManager.BaseModule):
if not username or not instance:
raise utils.EventError("Please provide @<user>@<instance>")
webfinger = utils.http.request(WEBFINGER % instance,
hostmeta = utils.http.request(HOSTMETA % instance,
soup=True, check_content_type=False)
print(hostmeta.data)
webfinger_url = None
for item in hostmeta.data.find_all("link"):
if item["rel"] and item["rel"][0] == "lrdd":
webfinger_url = item["template"]
break
if webfinger_url == None:
raise utils.EventError("host-meta lookup failed for %s" %
instance)
webfinger_url = webfinger_url.replace("{uri}",
"acct:%s" % full_username)
webfinger = utils.http.request(webfinger_url,
headers=WEBFINGER_HEADERS,
get_params={"resource": "acct:%s" % full_username},
json=True)
print(webfinger.data)
if webfinger.data:
activity_url = None
for link in webfinger.data["links"]:
if link["type"] == ACTIVITY_TYPE:
@ -45,6 +60,7 @@ class Module(ModuleManager.BaseModule):
outbox = utils.http.request(outbox_url, headers=ACTIVITY_HEADERS,
json=True)
items = None
if "first" in outbox.data:
if type(outbox.data["first"]) == dict:
items = outbox.data["first"]["orderedItems"]
@ -55,7 +71,9 @@ class Module(ModuleManager.BaseModule):
else:
items = outbox.data["orderedItems"]
if items:
if not items:
raise utils.EventError("No toots found")
first_item = items[0]
if first_item["type"] == "Announce":
retoot_url = first_item["object"]
@ -89,5 +107,3 @@ class Module(ModuleManager.BaseModule):
event["stdout"].write("%s: %s - %s" % (preferred_username,
content, shorturl))
else:
raise utils.EventError("User not found")