Fix bot.upload_and_send()
- Move matrix_uri_cache idea from apod into global bot, persist on restart - TODO: add "!bot uricache [ view | clean ]" command
This commit is contained in:
parent
6b9bd1620d
commit
44bd02d3b4
24
bot.py
24
bot.py
|
@ -44,6 +44,7 @@ class Bot:
|
|||
self.modules = dict()
|
||||
self.module_aliases = dict()
|
||||
self.leave_empty_rooms = True
|
||||
self.uri_cache = dict()
|
||||
self.pollcount = 0
|
||||
self.poll_task = None
|
||||
self.owners = []
|
||||
|
@ -83,15 +84,20 @@ class Bot:
|
|||
:param blob_content_type: Content type of the image in case of binary content
|
||||
:return:
|
||||
"""
|
||||
matrix_uri, mimetype, w, h, size = await self.upload_image(url, blob, blob_content_type)
|
||||
try:
|
||||
matrix_uri, mimetype, w, h, size = self.uri_cache[url]
|
||||
except KeyError:
|
||||
res = await self.upload_image(url, blob, blob_content_type)
|
||||
matrix_uri, mimetype, w, h, size = res
|
||||
if matrix_uri:
|
||||
self.uri_cache[url] = list(res)
|
||||
self.save_settings()
|
||||
else:
|
||||
return await self.send_text(room, "sorry. something went wrong uploading the image to matrix server :(")
|
||||
|
||||
if not text and not blob:
|
||||
text = f"{url}"
|
||||
|
||||
if matrix_uri is not None:
|
||||
await self.send_image(room, matrix_uri, text, mimetype, w, h, size)
|
||||
else:
|
||||
await self.send_text(room, "sorry. something went wrong uploading the image to matrix server :(")
|
||||
return await self.send_image(room, matrix_uri, text, mimetype, w, h, size)
|
||||
|
||||
# Helper function to upload a image from URL to homeserver. Use send_image() to actually send it to room.
|
||||
async def upload_image(self, url, blob=False, blob_content_type="image/png"):
|
||||
|
@ -225,7 +231,7 @@ class Bot:
|
|||
if size:
|
||||
msg["info"]["size"] = size
|
||||
|
||||
await self.client.room_send(room.room_id, 'm.room.message', msg)
|
||||
return await self.client.room_send(room.room_id, 'm.room.message', msg)
|
||||
|
||||
async def send_msg(self, mxid, roomname, message):
|
||||
"""
|
||||
|
@ -318,7 +324,7 @@ class Bot:
|
|||
module_settings[modulename] = moduleobject.get_settings()
|
||||
except Exception:
|
||||
self.logger.exception(f'unhandled exception {modulename}.get_settings')
|
||||
data = {self.appid: self.version, 'module_settings': module_settings}
|
||||
data = {self.appid: self.version, 'module_settings': module_settings, 'uri_cache': self.uri_cache}
|
||||
self.set_account_data(data)
|
||||
|
||||
def load_settings(self, data):
|
||||
|
@ -326,6 +332,8 @@ class Bot:
|
|||
return
|
||||
if not data.get('module_settings'):
|
||||
return
|
||||
if data.get('uri_cache'):
|
||||
self.uri_cache = data['uri_cache']
|
||||
for modulename, moduleobject in self.modules.items():
|
||||
if data['module_settings'].get(modulename):
|
||||
try:
|
||||
|
|
|
@ -76,7 +76,9 @@ class MatrixModule(BotModule):
|
|||
|
||||
self.logger.debug(apod)
|
||||
if apod.media_type == "image":
|
||||
await self.upload_and_send_image(room, bot, apod)
|
||||
await bot.send_text(room, f"{apod.title} ({apod.date})")
|
||||
await bot.upload_and_send_image(room, apod.hdurl, f"{apod.title}")
|
||||
await bot.send_text(room, f"{apod.explanation}")
|
||||
else:
|
||||
await self.send_unknown_mediatype(room, bot, apod)
|
||||
elif response.status_code == 400:
|
||||
|
@ -91,25 +93,6 @@ class MatrixModule(BotModule):
|
|||
await bot.send_text(room, f"{apod.title}")
|
||||
await bot.send_text(room, f"{apod.explanation} || date: {apod.date} || original-url: {apod.url}")
|
||||
|
||||
async def upload_and_send_image(self, room, bot, apod):
|
||||
send_again = True
|
||||
await bot.send_text(room, f"{apod.title} ({apod.date})")
|
||||
if apod.date in self.matrix_uri_cache:
|
||||
matrix_uri = self.matrix_uri_cache.get(apod.date)
|
||||
self.logger.debug(f"already uploaded picture {matrix_uri} for date {apod.date}")
|
||||
else:
|
||||
matrix_uri = await bot.upload_and_send_image(room, apod.hdurl, f"{apod.title}")
|
||||
send_again = False
|
||||
|
||||
if matrix_uri is not None:
|
||||
self.matrix_uri_cache[apod.date] = matrix_uri
|
||||
bot.save_settings()
|
||||
if send_again:
|
||||
await bot.send_image(room, matrix_uri, f"{apod.title}")
|
||||
else:
|
||||
await bot.send_text(room, "Sorry. Something went wrong uploading the image to Matrix server :(")
|
||||
await bot.send_text(room, f"{apod.explanation}")
|
||||
|
||||
def get_settings(self):
|
||||
data = super().get_settings()
|
||||
data["matrix_uri_cache"] = self.matrix_uri_cache
|
||||
|
|
|
@ -46,10 +46,7 @@ async def send_entry(bot, room, entry):
|
|||
pms_image = pms.get_image(entry["art"], 600, 300)
|
||||
if pms_image:
|
||||
(blob, content_type) = pms_image
|
||||
matrix_uri = await bot.upload_and_send_image(room, blob, "", True, content_type)
|
||||
|
||||
if matrix_uri is not None:
|
||||
await bot.send_image(room, matrix_uri, "")
|
||||
await bot.upload_and_send_image(room, blob, "", True, content_type)
|
||||
|
||||
fmt_params = {
|
||||
"title": entry["title"],
|
||||
|
|
Loading…
Reference in New Issue