Merge pull request #67 from ancho/feature/more-debug-apod

add more debugging output to apod module uploading a file
This commit is contained in:
Ville Ranki 2020-03-29 20:57:00 +03:00 committed by GitHub
commit 533bcd029a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 7 deletions

View File

@ -92,35 +92,43 @@ class MatrixModule(BotModule):
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):
url = apod.hdurl if apod.hdurl is not None else apod.url
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 self.upload_image(bot, url)
matrix_uri = await self.upload_image(bot, apod.hdurl)
if matrix_uri is None:
self.logger.warning("unable to upload hdurl. try url next.")
matrix_uri = await self.upload_image(bot, apod.url)
await bot.send_text(room, f"{apod.title} ({apod.date})")
if matrix_uri is not None:
self.matrix_uri_cache[apod.date] = matrix_uri
bot.save_settings()
await bot.send_text(room, f"{apod.title} ({apod.date})")
await bot.send_image(room, matrix_uri, f"{apod.title}")
await bot.send_text(room, f"original-url: {url}")
await bot.send_text(room, f"{apod.explanation}")
else:
await bot.send_text(room, "sorry. something went wrong uploading the image to matrix server :(")
if apod.hdurl is not None:
await bot.send_text(room, f"original-hdurl: {apod.hdurl}")
await bot.send_text(room, f"original-url: {apod.url}")
await bot.send_text(room, f"{apod.explanation}")
async def upload_image(self, bot, url):
self.client: AsyncClient
response: UploadResponse
self.logger.debug(f"start downloading image from url {url}")
url_response = requests.get(url)
self.logger.debug(f"response [status_code={url_response.status_code}, headers={url_response.headers}")
if url_response.status_code == 200:
content_type = url_response.headers.get("content-type")
self.logger.info(f"uploading content to matrix server [size={len(url_response.content)}, content-type: {content_type}]")
(response, alist) = await bot.client.upload(lambda a, b: url_response.content, content_type)
self.logger.debug("response: %s", response)
if isinstance(response, UploadResponse):
self.logger.debug("uploaded file to %s", response.content_uri)
self.logger.info("uploaded file to %s", response.content_uri)
return response.content_uri
else:
response: UploadError