set a bitbot-specific useragent for fediverse requests
This commit is contained in:
parent
612849d0c5
commit
9194925739
1 changed files with 10 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from src import ModuleManager, utils
|
from src import IRCBot, ModuleManager, utils
|
||||||
|
|
||||||
HOSTMETA = "https://%s/.well-known/host-meta"
|
HOSTMETA = "https://%s/.well-known/host-meta"
|
||||||
WEBFINGER_DEFAULT = "https://%s/.well-known/webfinger?resource={uri}"
|
WEBFINGER_DEFAULT = "https://%s/.well-known/webfinger?resource={uri}"
|
||||||
|
@ -9,6 +9,8 @@ ACTIVITY_TYPE = "application/activity+json"
|
||||||
ACTIVITY_HEADERS = {"Accept": ("application/ld+json; "
|
ACTIVITY_HEADERS = {"Accept": ("application/ld+json; "
|
||||||
'profile="https://www.w3.org/ns/activitystreams"')}
|
'profile="https://www.w3.org/ns/activitystreams"')}
|
||||||
|
|
||||||
|
USERAGENT = "BitBot (%s) Fediverse" % IRCBot.VERSION
|
||||||
|
|
||||||
def _parse_username(s):
|
def _parse_username(s):
|
||||||
username, _, instance = s.rpartition("@")
|
username, _, instance = s.rpartition("@")
|
||||||
if username.startswith("@"):
|
if username.startswith("@"):
|
||||||
|
@ -54,7 +56,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
raise utils.EventError("Please provide @<user>@<instance>")
|
raise utils.EventError("Please provide @<user>@<instance>")
|
||||||
|
|
||||||
hostmeta = utils.http.request(HOSTMETA % instance,
|
hostmeta = utils.http.request(HOSTMETA % instance,
|
||||||
parse=True, check_content_type=False)
|
parse=True, check_content_type=False, useragent=USERAGENT)
|
||||||
webfinger_url = None
|
webfinger_url = None
|
||||||
for item in hostmeta.data.find_all("link"):
|
for item in hostmeta.data.find_all("link"):
|
||||||
if item["rel"] and item["rel"][0] == "lrdd":
|
if item["rel"] and item["rel"][0] == "lrdd":
|
||||||
|
@ -68,7 +70,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
"acct:%s@%s" % (username, instance))
|
"acct:%s@%s" % (username, instance))
|
||||||
|
|
||||||
webfinger = utils.http.request(webfinger_url,
|
webfinger = utils.http.request(webfinger_url,
|
||||||
headers=WEBFINGER_HEADERS, json=True)
|
headers=WEBFINGER_HEADERS, json=True, useragent=USERAGENT)
|
||||||
|
|
||||||
activity_url = None
|
activity_url = None
|
||||||
for link in webfinger.data["links"]:
|
for link in webfinger.data["links"]:
|
||||||
|
@ -80,12 +82,12 @@ class Module(ModuleManager.BaseModule):
|
||||||
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, useragent=USERAGENT)
|
||||||
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, useragent=USERAGENT)
|
||||||
items = None
|
items = None
|
||||||
|
|
||||||
if "first" in outbox.data:
|
if "first" in outbox.data:
|
||||||
|
@ -95,7 +97,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
else:
|
else:
|
||||||
# mastodon
|
# mastodon
|
||||||
first = utils.http.request(outbox.data["first"],
|
first = utils.http.request(outbox.data["first"],
|
||||||
headers=ACTIVITY_HEADERS, json=True)
|
headers=ACTIVITY_HEADERS, json=True, useragent=USERAGENT)
|
||||||
items = first.data["orderedItems"]
|
items = first.data["orderedItems"]
|
||||||
else:
|
else:
|
||||||
items = outbox.data["orderedItems"]
|
items = outbox.data["orderedItems"]
|
||||||
|
@ -108,11 +110,11 @@ class Module(ModuleManager.BaseModule):
|
||||||
retoot_url = first_item["object"]
|
retoot_url = first_item["object"]
|
||||||
retoot_instance = urllib.parse.urlparse(retoot_url).hostname
|
retoot_instance = urllib.parse.urlparse(retoot_url).hostname
|
||||||
retoot = utils.http.request(retoot_url,
|
retoot = utils.http.request(retoot_url,
|
||||||
headers=ACTIVITY_HEADERS, json=True)
|
headers=ACTIVITY_HEADERS, json=True, useragent=USERAGENT)
|
||||||
|
|
||||||
original_tooter_url = retoot.data["attributedTo"]
|
original_tooter_url = retoot.data["attributedTo"]
|
||||||
original_tooter = utils.http.request(original_tooter_url,
|
original_tooter = utils.http.request(original_tooter_url,
|
||||||
headers=ACTIVITY_HEADERS, json=True)
|
headers=ACTIVITY_HEADERS, json=True, useragent=USERAGENT)
|
||||||
|
|
||||||
retooted_user = "@%s@%s" % (
|
retooted_user = "@%s@%s" % (
|
||||||
original_tooter.data["preferredUsername"],
|
original_tooter.data["preferredUsername"],
|
||||||
|
|
Loading…
Reference in a new issue