urlinfo: fix handler selection

This commit is contained in:
ducklet 2020-11-07 22:08:46 +01:00
parent 15c3cb0221
commit d84f82768d

View file

@ -215,6 +215,11 @@ def full_url(ref: str) -> str:
return f"http://{ref}" if ref.startswith("www") else ref return f"http://{ref}" if ref.startswith("www") else ref
class GenericHandler:
extractor = generic_extractor
handle = generic_handler
async def handle(message: Message): async def handle(message: Message):
if message.command and message.command not in {"u", "url"}: if message.command and message.command not in {"u", "url"}:
return return
@ -229,18 +234,15 @@ async def handle(message: Message):
for url in urls: for url in urls:
for handler in handlers: for handler in handlers:
if handler.can_handle(url): if handler.can_handle(url):
handler = handler.handle
extractor = handler.extractor
break break
else: else:
if not ( # We only want the generic handler if we were called explicitly
message.command handler = GenericHandler if message.command else None
): # We only want the generic handler if we were called explicitly
continue
handler = generic_handler
extractor = generic_extractor
info = await load_info(url, extractor, cachetoken()) if handler is None:
continue
info = await load_info(url, handler.extractor, cachetoken())
if not info: if not info:
continue continue
await handler(message, url, info) await handler.handle(message, url, info)