2019-09-23 10:28:14 +00:00
|
|
|
import urllib.parse
|
2019-10-09 15:44:54 +00:00
|
|
|
import bs4
|
2019-09-15 09:43:46 +00:00
|
|
|
from src import IRCBot, utils
|
|
|
|
|
|
|
|
LD_TYPE = ("application/ld+json; "
|
|
|
|
"profile=\"https://www.w3.org/ns/activitystreams\"")
|
|
|
|
JRD_TYPE = "application/jrd+json"
|
|
|
|
ACTIVITY_TYPE = "application/activity+json"
|
|
|
|
USERAGENT = "BitBot (%s) Fediverse" % IRCBot.VERSION
|
|
|
|
|
|
|
|
def split_username(s):
|
|
|
|
if s[0] == "@":
|
|
|
|
s = s[1:]
|
|
|
|
username, _, instance = s.partition("@")
|
|
|
|
if username and instance:
|
|
|
|
return username, instance
|
|
|
|
return None, None
|
|
|
|
|
2019-09-15 13:27:34 +00:00
|
|
|
def activity_request(url, data=None, method="GET", type=ACTIVITY_TYPE,
|
|
|
|
headers={}):
|
2019-09-15 09:43:46 +00:00
|
|
|
content_type = None
|
|
|
|
|
|
|
|
if method == "POST":
|
|
|
|
content_type = type
|
|
|
|
else:
|
|
|
|
headers = {"Accept": type}
|
|
|
|
|
|
|
|
request = utils.http.Request(url, headers=headers, useragent=USERAGENT,
|
2019-09-16 09:57:46 +00:00
|
|
|
content_type=content_type, post_data=data, method=method, json=True,
|
|
|
|
json_body=True)
|
2019-09-17 16:41:15 +00:00
|
|
|
return utils.http.request(request)
|
2019-09-15 09:43:46 +00:00
|
|
|
|
|
|
|
HOSTMETA_TEMPLATE = "https://%s/.well-known/host-meta"
|
|
|
|
WEBFINGER_TEMPLATE = "https://%s/.well-known/webfinger?resource={uri}"
|
|
|
|
|
|
|
|
def find_actor(username, instance):
|
|
|
|
hostmeta = HOSTMETA_TEMPLATE % instance
|
|
|
|
hostmeta_request = utils.http.Request(HOSTMETA_TEMPLATE % instance,
|
|
|
|
useragent=USERAGENT, parse=True, check_content_type=False)
|
|
|
|
hostmeta = utils.http.request(hostmeta_request)
|
|
|
|
|
|
|
|
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 not webfinger_url:
|
|
|
|
webfinger_url = WEBFINGER_TEMPLATE % instance
|
|
|
|
webfinger_url = webfinger_url.replace("{uri}",
|
|
|
|
"acct:%s@%s" % (username, instance), 1)
|
|
|
|
|
|
|
|
webfinger = activity_request(webfinger_url, type=JRD_TYPE)
|
|
|
|
|
|
|
|
actor_url = None
|
2019-09-17 16:41:15 +00:00
|
|
|
for link in webfinger.data["links"]:
|
2019-09-15 09:43:46 +00:00
|
|
|
if link["type"] == ACTIVITY_TYPE:
|
|
|
|
return link["href"]
|
|
|
|
|
2019-10-09 15:44:54 +00:00
|
|
|
def _normalise_note(content):
|
|
|
|
soup = bs4.BeautifulSoup(content, "html.parser")
|
|
|
|
lines = []
|
|
|
|
for element in soup.find_all():
|
|
|
|
out = ""
|
|
|
|
if element.text.strip() == "":
|
|
|
|
continue
|
|
|
|
elif element.name == "p":
|
|
|
|
for subitem in element.contents:
|
|
|
|
if type(subitem) == bs4.element.Tag:
|
|
|
|
if subitem.name == "br":
|
|
|
|
lines.append(out)
|
|
|
|
out = ""
|
|
|
|
else:
|
|
|
|
out += subitem
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
lines.append(out.replace(" ", " "))
|
|
|
|
return " ".join(lines)
|
|
|
|
|
2019-10-04 12:06:29 +00:00
|
|
|
def format_note(actor, note, type="Create"):
|
|
|
|
if type == "Announce":
|
|
|
|
retoot_url = note
|
2019-09-16 09:51:59 +00:00
|
|
|
retoot_instance = urllib.parse.urlparse(retoot_url).hostname
|
|
|
|
retoot = activity_request(retoot_url)
|
2019-10-08 15:07:35 +00:00
|
|
|
retoot_url = retoot.data.get("url", retoot.data["id"])
|
2019-09-16 09:51:59 +00:00
|
|
|
|
|
|
|
original_tooter = ap_actor.Actor(retoot.data["attributedTo"])
|
|
|
|
original_tooter.load()
|
|
|
|
retooted_user = "@%s@%s" % (original_tooter.username, retoot_instance)
|
2019-10-09 15:44:54 +00:00
|
|
|
retoot_content = _normalise_note(retoot.data["content"])
|
2019-09-16 09:51:59 +00:00
|
|
|
|
2019-09-16 10:10:26 +00:00
|
|
|
return (retoot.data.get("summary", None), "%s (boost %s): %s - %s" % (
|
|
|
|
actor.username, retooted_user, retoot_content), retoot_url)
|
2019-09-16 09:51:59 +00:00
|
|
|
|
2019-10-04 12:06:29 +00:00
|
|
|
elif type == "Create":
|
2019-10-09 15:44:54 +00:00
|
|
|
content = _normalise_note(note["content"])
|
2019-10-08 15:07:35 +00:00
|
|
|
url = note.get("url", note["id"])
|
2019-09-16 09:51:59 +00:00
|
|
|
|
2019-10-04 12:06:29 +00:00
|
|
|
return (note.get("summary", None),
|
2019-09-16 10:10:26 +00:00
|
|
|
"%s: %s" % (actor.username, content), url)
|
2019-09-16 09:51:59 +00:00
|
|
|
|
2019-09-16 10:10:26 +00:00
|
|
|
return None, None, None
|