Fix httpx session being created on each message

This commit is contained in:
Nexus 2024-09-18 18:51:57 +01:00
parent 9f0ac830f2
commit faed38aafc
3 changed files with 59 additions and 49 deletions

View file

@ -35,53 +35,58 @@ class FediversePreviewModule(niobot.Module):
return
sent = []
async with httpx.AsyncClient(
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
}
) as client:
for item in event.body.split():
if not item.startswith(tuple(supported_prefixes)):
return
parsed = urlparse(item)
post_id = parsed.path.split("/")[-1]
if post_id in sent:
self.log.info("Already sent post %s", post_id)
continue
elif len(sent) >= 5:
self.log.info("Already sent 5 posts, stopping")
break
resp = await client.get(
"https://%s/api/v1/statuses/%s" % (parsed.netloc, post_id)
)
if resp.status_code != 200:
self.log.error("Got HTTP %d from %s", resp.status_code, resp.url)
continue
data = resp.json()
self.log.info("Got data: %r", data)
username = data["account"]["username"]
if not (text := data.get("content")):
self.log.warning("No text for post %s", post_id)
continue
self.log.info("Detected fediverse post %s: %r", post_id, text)
rendered = await self.bot._markdown_to_html(text)
text_body = "<blockquote>%s</blockquote>" % rendered
body = '<a href="%s">@%s:</a><br>%s' % (
"https://%s/@%s" % (parsed.netloc, username),
username,
text_body,
)
if data.get("media_attachments"):
body += "<p>{:,} attachments</p>".format(
len(data["media_attachments"])
to_get: set[str] = set()
for item in event.body.split():
if not item.startswith(tuple(supported_prefixes)):
return
parsed = urlparse(item)
post_id = parsed.path.split("/")[-1]
if post_id in sent:
self.log.info("Already sent post %s", post_id)
continue
elif len(to_get) >= 5:
self.log.info("Already sent 5 posts, stopping")
break
to_get.add("https://%s/api/v1/statuses/%s" % (parsed.netloc, post_id))
if to_get:
async with httpx.AsyncClient(
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
}
) as client:
for url in to_get:
resp = await client.get(
url
)
self.log.info("Sending fediverse post %s", post_id)
await self.bot.send_message(
room,
body,
reply_to=event,
content_type="html.raw",
override={"body": f"@{username}: {data['content']!r}"},
)
sent.append(post_id)
if resp.status_code != 200:
self.log.error("Got HTTP %d from %s", resp.status_code, resp.url)
continue
data = resp.json()
self.log.info("Got data: %r", data)
username = data["account"]["username"]
if not (text := data.get("content")):
self.log.warning("No text for post %s", url)
continue
self.log.info("Detected fediverse post %s: %r", url, text)
rendered = await self.bot._markdown_to_html(text)
parsed = urlparse(url)
text_body = "<blockquote>%s</blockquote>" % rendered
body = '<a href="%s">@%s:</a><br>%s' % (
"https://%s/@%s" % (parsed.netloc, username),
username,
text_body,
)
if data.get("media_attachments"):
body += "<p>{:,} attachments</p>".format(
len(data["media_attachments"])
)
self.log.info("Sending fediverse post %s", url)
await self.bot.send_message(
room,
body,
reply_to=event,
content_type="html.raw",
override={"body": f"@{username}: {data['content']!r}"},
)
sent.append(post_id)

View file

@ -1,4 +1,5 @@
import json
import logging
import re
import typing
@ -12,6 +13,7 @@ if typing.TYPE_CHECKING:
class MSCGetter(niobot.Module):
bot: "NonsenseBot"
log = logging.getLogger(__name__)
def __init__(self, bot):
super().__init__(bot)
@ -34,6 +36,7 @@ class MSCGetter(niobot.Module):
if content:
return content
self.log.debug("Requesting MSC: %d", number)
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.github.com/repos/matrix-org/matrix-spec-proposals/issues/%d"

View file

@ -43,6 +43,8 @@ class TruthSocialTranscode(niobot.Module):
async def on_message_internal(
self, room: niobot.MatrixRoom, event: niobot.RoomMessageVideo
) -> str:
if self.bot.is_old(event):
return "IGNORED"
self.log.info("Processing event: %r (%r)", event, room)
# if not isinstance(event, niobot.RoomMessageVideo):
# # We are not interested in non-videos.