From 76dc9c12af9cece206f3c736a6c77ad110df22db Mon Sep 17 00:00:00 2001 From: ducklet Date: Sat, 14 Nov 2020 14:29:54 +0100 Subject: [PATCH] urlinfo: make youtube-info a submodule for urlinfo --- hotdog/command/urlinfo.py | 19 +++++++++---------- hotdog/command/urlinfo_/generic.py | 6 +++++- hotdog/command/urlinfo_/imdb.py | 6 +++++- hotdog/command/{ => urlinfo_}/youtube.py | 19 +++++-------------- 4 files changed, 24 insertions(+), 26 deletions(-) rename hotdog/command/{ => urlinfo_}/youtube.py (87%) diff --git a/hotdog/command/urlinfo.py b/hotdog/command/urlinfo.py index 82c2e62..25d7ccd 100644 --- a/hotdog/command/urlinfo.py +++ b/hotdog/command/urlinfo.py @@ -9,7 +9,11 @@ import requests from ..functions import react from ..html import find from ..models import Message -from .urlinfo_ import generic, imdb # XXX make this dynamic? (like we load plugins) +from .urlinfo_ import ( # XXX make this dynamic? (like we load plugins) + generic, + imdb, + youtube, +) HELP = """Return information about an online HTTP resource. !u[rl] @@ -98,11 +102,9 @@ class Info: _load_info_cache = {} -async def load_info( - url: str, extractor: Callable[[Info], None], cachetoken -) -> Optional[Info]: +async def load_info(url: str, extractor: Callable[[Info], None]) -> Optional[Info]: """The cachetoken is just there to bust the LRU cache after a while.""" - cachekey = (url, cachetoken) + cachekey = (url, cachetoken()) if cachekey in _load_info_cache: return _load_info_cache[cachekey] @@ -166,7 +168,7 @@ async def handle(message: Message): return limit = 3 - handlers = (imdb,) + handlers = (imdb, youtube) urls = {full_url(w) for w in message.words if is_url(w)} if message.html: @@ -188,10 +190,7 @@ async def handle(message: Message): await react(message, "⚡️") try: - info = await load_info(url, handler.extractor, cachetoken()) - if not info: - continue - await handler.handle(message, url, info) + await handler.handle(message, url, load_info) except: await react(message, "🐛") raise diff --git a/hotdog/command/urlinfo_/generic.py b/hotdog/command/urlinfo_/generic.py index 5989f91..62ceb27 100644 --- a/hotdog/command/urlinfo_/generic.py +++ b/hotdog/command/urlinfo_/generic.py @@ -66,7 +66,11 @@ def ld_details(ex, tz, lc): return html, plain -async def handle(message: Message, url, info): +async def handle(message: Message, url, load_info): + info = await load_info(url, extractor) + if not info: + return + roomconf = message.app.config.l6n[message.room.room_id] plain = html = None if info.extracted.ld: diff --git a/hotdog/command/urlinfo_/imdb.py b/hotdog/command/urlinfo_/imdb.py index 8578f38..8f521b7 100644 --- a/hotdog/command/urlinfo_/imdb.py +++ b/hotdog/command/urlinfo_/imdb.py @@ -130,7 +130,11 @@ async def extractor(info): info.extracted.rating_count = ld["aggregateRating"]["ratingCount"] -async def handle(message: Message, url, info): +async def handle(message: Message, url, load_info): + info = await load_info(url, extractor) + if not info: + return + ex = clone(info.extracted) image_title = f"Poster for {ex.title} ({ex.published:%Y})" hosted_image = await import_image( diff --git a/hotdog/command/youtube.py b/hotdog/command/urlinfo_/youtube.py similarity index 87% rename from hotdog/command/youtube.py rename to hotdog/command/urlinfo_/youtube.py index 869206d..2bac62e 100644 --- a/hotdog/command/youtube.py +++ b/hotdog/command/urlinfo_/youtube.py @@ -6,20 +6,14 @@ from typing import * import youtube_dl -from ..functions import escape_all, pretty_duration, reply -from ..models import Message - -HELP = """Gibt Informationen zu Youtube-Videos aus. -Some text containing a . -""" +from ...functions import escape_all, pretty_duration, reply +from ...models import Message youtube_re = re.compile( r"\byoutu(\.be/|be\.com/(embed/|v/|watch\?\w*v=))(?P[0-9A-Za-z_-]{10,11})\b" ) - -def init(bot): - bot.on_message(handle) +can_handle = youtube_re.search @lru_cache(maxsize=5) @@ -33,11 +27,8 @@ def cachetoken(quant_m=15): return int(now() / 60 / quant_m) -async def handle(message: Message): - if message.command in {"u", "url"}: - return - - match = youtube_re.search(message.text) +async def handle(message: Message, url, load_generic_info): + match = youtube_re.search(url) if not match: return