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 import urllib.parse
from src import ModuleManager, utils from src import ModuleManager, utils
WEBFINGER = "https://%s/.well-known/webfinger" HOSTMETA = "https://%s/.well-known/host-meta"
WEBFINGER_HEADERS = {"Accept": "application/jrd+json"} WEBFINGER_HEADERS = {"Accept": "application/jrd+json"}
ACTIVITY_TYPE = "application/activity+json" ACTIVITY_TYPE = "application/activity+json"
@ -22,72 +22,88 @@ class Module(ModuleManager.BaseModule):
if not username or not instance: if not username or not instance:
raise utils.EventError("Please provide @<user>@<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, headers=WEBFINGER_HEADERS,
get_params={"resource": "acct:%s" % full_username}, get_params={"resource": "acct:%s" % full_username},
json=True) json=True)
print(webfinger.data)
if webfinger.data: activity_url = None
activity_url = None for link in webfinger.data["links"]:
for link in webfinger.data["links"]: if link["type"] == ACTIVITY_TYPE:
if link["type"] == ACTIVITY_TYPE: activity_url = link["href"]
activity_url = link["href"] break
break
if not activity_url: if not activity_url:
raise utils.EventError("Failed to find user activity feed") raise utils.EventError("Failed to find user activity feed")
activity = utils.http.request(activity_url, activity = utils.http.request(activity_url,
headers=ACTIVITY_HEADERS, json=True) headers=ACTIVITY_HEADERS, json=True)
preferred_username = activity.data["preferredUsername"] preferred_username = activity.data["preferredUsername"]
outbox_url = activity.data["outbox"] outbox_url = activity.data["outbox"]
outbox = utils.http.request(outbox_url, headers=ACTIVITY_HEADERS, outbox = utils.http.request(outbox_url, headers=ACTIVITY_HEADERS,
json=True) json=True)
items = None items = None
if "first" in outbox.data:
if type(outbox.data["first"]) == dict: if "first" in outbox.data:
items = outbox.data["first"]["orderedItems"] if type(outbox.data["first"]) == dict:
else: items = outbox.data["first"]["orderedItems"]
first = utils.http.request(outbox.data["first"],
headers=ACTIVITY_HEADERS, json=True)
items = first.data["orderedItems"]
else: else:
items = outbox.data["orderedItems"] first = utils.http.request(outbox.data["first"],
headers=ACTIVITY_HEADERS, json=True)
if items: items = first.data["orderedItems"]
first_item = items[0]
if first_item["type"] == "Announce":
retoot_url = first_item["object"]
retoot_instance = urllib.parse.urlparse(retoot_url).hostname
retoot = utils.http.request(retoot_url,
headers=ACTIVITY_HEADERS, json=True)
original_tooter_url = retoot.data["attributedTo"]
original_tooter = utils.http.request(original_tooter_url,
headers=ACTIVITY_HEADERS, json=True)
retooted_user = "@%s@%s" % (
original_tooter.data["preferredUsername"],
retoot_instance)
shorturl = self.exports.get_one("shorturl")(
event["server"], retoot_url)
retoot_content = utils.http.strip_html(
retoot.data["content"])
event["stdout"].write("%s (boost %s): %s - %s" % (
preferred_username, retooted_user, retoot_content,
shorturl))
elif first_item["type"] == "Create":
content = utils.http.strip_html(
first_item["object"]["content"])
url = first_item["object"]["id"]
shorturl = self.exports.get_one("shorturl")(
event["server"], url)
event["stdout"].write("%s: %s - %s" % (preferred_username,
content, shorturl))
else: else:
raise utils.EventError("User not found") items = outbox.data["orderedItems"]
if not items:
raise utils.EventError("No toots found")
first_item = items[0]
if first_item["type"] == "Announce":
retoot_url = first_item["object"]
retoot_instance = urllib.parse.urlparse(retoot_url).hostname
retoot = utils.http.request(retoot_url,
headers=ACTIVITY_HEADERS, json=True)
original_tooter_url = retoot.data["attributedTo"]
original_tooter = utils.http.request(original_tooter_url,
headers=ACTIVITY_HEADERS, json=True)
retooted_user = "@%s@%s" % (
original_tooter.data["preferredUsername"],
retoot_instance)
shorturl = self.exports.get_one("shorturl")(
event["server"], retoot_url)
retoot_content = utils.http.strip_html(
retoot.data["content"])
event["stdout"].write("%s (boost %s): %s - %s" % (
preferred_username, retooted_user, retoot_content,
shorturl))
elif first_item["type"] == "Create":
content = utils.http.strip_html(
first_item["object"]["content"])
url = first_item["object"]["id"]
shorturl = self.exports.get_one("shorturl")(
event["server"], url)
event["stdout"].write("%s: %s - %s" % (preferred_username,
content, shorturl))