Fix httpx session being created on each message
This commit is contained in:
parent
9f0ac830f2
commit
faed38aafc
3 changed files with 59 additions and 49 deletions
|
@ -35,11 +35,7 @@ class FediversePreviewModule(niobot.Module):
|
||||||
return
|
return
|
||||||
|
|
||||||
sent = []
|
sent = []
|
||||||
async with httpx.AsyncClient(
|
to_get: set[str] = set()
|
||||||
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():
|
for item in event.body.split():
|
||||||
if not item.startswith(tuple(supported_prefixes)):
|
if not item.startswith(tuple(supported_prefixes)):
|
||||||
return
|
return
|
||||||
|
@ -48,11 +44,19 @@ class FediversePreviewModule(niobot.Module):
|
||||||
if post_id in sent:
|
if post_id in sent:
|
||||||
self.log.info("Already sent post %s", post_id)
|
self.log.info("Already sent post %s", post_id)
|
||||||
continue
|
continue
|
||||||
elif len(sent) >= 5:
|
elif len(to_get) >= 5:
|
||||||
self.log.info("Already sent 5 posts, stopping")
|
self.log.info("Already sent 5 posts, stopping")
|
||||||
break
|
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(
|
resp = await client.get(
|
||||||
"https://%s/api/v1/statuses/%s" % (parsed.netloc, post_id)
|
url
|
||||||
)
|
)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
self.log.error("Got HTTP %d from %s", resp.status_code, resp.url)
|
self.log.error("Got HTTP %d from %s", resp.status_code, resp.url)
|
||||||
|
@ -61,11 +65,12 @@ class FediversePreviewModule(niobot.Module):
|
||||||
self.log.info("Got data: %r", data)
|
self.log.info("Got data: %r", data)
|
||||||
username = data["account"]["username"]
|
username = data["account"]["username"]
|
||||||
if not (text := data.get("content")):
|
if not (text := data.get("content")):
|
||||||
self.log.warning("No text for post %s", post_id)
|
self.log.warning("No text for post %s", url)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.log.info("Detected fediverse post %s: %r", post_id, text)
|
self.log.info("Detected fediverse post %s: %r", url, text)
|
||||||
rendered = await self.bot._markdown_to_html(text)
|
rendered = await self.bot._markdown_to_html(text)
|
||||||
|
parsed = urlparse(url)
|
||||||
text_body = "<blockquote>%s</blockquote>" % rendered
|
text_body = "<blockquote>%s</blockquote>" % rendered
|
||||||
body = '<a href="%s">@%s:</a><br>%s' % (
|
body = '<a href="%s">@%s:</a><br>%s' % (
|
||||||
"https://%s/@%s" % (parsed.netloc, username),
|
"https://%s/@%s" % (parsed.netloc, username),
|
||||||
|
@ -76,7 +81,7 @@ class FediversePreviewModule(niobot.Module):
|
||||||
body += "<p>{:,} attachments</p>".format(
|
body += "<p>{:,} attachments</p>".format(
|
||||||
len(data["media_attachments"])
|
len(data["media_attachments"])
|
||||||
)
|
)
|
||||||
self.log.info("Sending fediverse post %s", post_id)
|
self.log.info("Sending fediverse post %s", url)
|
||||||
await self.bot.send_message(
|
await self.bot.send_message(
|
||||||
room,
|
room,
|
||||||
body,
|
body,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ if typing.TYPE_CHECKING:
|
||||||
|
|
||||||
class MSCGetter(niobot.Module):
|
class MSCGetter(niobot.Module):
|
||||||
bot: "NonsenseBot"
|
bot: "NonsenseBot"
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
super().__init__(bot)
|
super().__init__(bot)
|
||||||
|
@ -34,6 +36,7 @@ class MSCGetter(niobot.Module):
|
||||||
if content:
|
if content:
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
self.log.debug("Requesting MSC: %d", number)
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
"https://api.github.com/repos/matrix-org/matrix-spec-proposals/issues/%d"
|
"https://api.github.com/repos/matrix-org/matrix-spec-proposals/issues/%d"
|
||||||
|
|
|
@ -43,6 +43,8 @@ class TruthSocialTranscode(niobot.Module):
|
||||||
async def on_message_internal(
|
async def on_message_internal(
|
||||||
self, room: niobot.MatrixRoom, event: niobot.RoomMessageVideo
|
self, room: niobot.MatrixRoom, event: niobot.RoomMessageVideo
|
||||||
) -> str:
|
) -> str:
|
||||||
|
if self.bot.is_old(event):
|
||||||
|
return "IGNORED"
|
||||||
self.log.info("Processing event: %r (%r)", event, room)
|
self.log.info("Processing event: %r (%r)", event, room)
|
||||||
# if not isinstance(event, niobot.RoomMessageVideo):
|
# if not isinstance(event, niobot.RoomMessageVideo):
|
||||||
# # We are not interested in non-videos.
|
# # We are not interested in non-videos.
|
||||||
|
|
Loading…
Reference in a new issue