urlinfo: make youtube-info a submodule for urlinfo

This commit is contained in:
ducklet 2020-11-14 14:29:54 +01:00
parent c49083da6a
commit 76dc9c12af
4 changed files with 24 additions and 26 deletions

View file

@ -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] <url>
@ -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

View file

@ -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:

View file

@ -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(

View file

@ -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 <Youtube URL>.
"""
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<id>[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