Added reporting for a failed OCR match

This commit is contained in:
Riley Housden 2022-08-22 14:35:13 -04:00
parent cf73726614
commit b16ff88a79
2 changed files with 13 additions and 3 deletions

View file

@ -10,7 +10,7 @@ from typing import Optional
from lib.config import load_config
from lib.gcal import GoogleCalendarAPI
from lib.room import Room
from lib.ocr import get_room_data, NoMatchException
from lib.ocr import get_room_data, NoMatchException, get_image_string
plugin = lightbulb.Plugin("LibCal")
config = load_config()
@ -82,8 +82,15 @@ async def book_error_handler(event: lightbulb.CommandErrorEvent):
print("ERROR TYPE:", type(event.exception))
if isinstance(event.exception.__cause__, NoMatchException):
embed = hikari.Embed(title="Huh, I can't read that",
description="Could you try another picture?")
description="Could you try another picture?\nI'll send a notice to the bot's maintainer. :)")
await event.context.respond(embed=embed)
embed.title = "Image match failed!"
embed.description = f"I failed to find any bookings in the attached image."
embed.add_field("Image text", get_image_string(event.context.options.img))
embed.set_image(event.context.options.img)
owners = event.context.bot.fetch_owner_ids()
for user_id in owners:
await event.context.bot.rest.fetch_user(user_id).send(embed)
else:
embed = hikari.Embed(
title="Booking Error", description="Whelp, better luck next time I guess... the images used are attached.")

View file

@ -67,7 +67,7 @@ def get_room_data(img: bytes) -> list[Room]:
rooms: list[Room] = []
start_time: datetime | None = None
end_time: datetime | None = None
img_string = image_to_string(Image.open(BytesIO(img)))
img_string = get_image_string(img)
img_string = correct_newlines(img_string)
img_string = correct_commas(img_string)
matches = re.finditer(RE_STRING, img_string)
@ -85,3 +85,6 @@ def get_room_data(img: bytes) -> list[Room]:
if len(rooms) == 0:
raise NoMatchException
return rooms
def get_image_string(img: bytes) -> str:
return image_to_string(Image.open(BytesIO(img)))