|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
import random
|
|
|
|
|
import urllib.parse
|
|
|
|
|
from typing import Optional
|
|
|
|
|
from typing import cast, Optional
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
from edmond.plugin import Plugin
|
|
|
|
|
from edmond.plugins.shrlok import ShrlokPlugin
|
|
|
|
|
|
|
|
|
|
BASE_URL = "https://taxref.mnhn.fr/api"
|
|
|
|
|
IMG_FETCH_HTML = """\
|
|
|
|
@ -164,13 +165,13 @@ class TaxrefPlugin(Plugin):
|
|
|
|
|
def get_img_url(item) -> Optional[str]:
|
|
|
|
|
return item.get("_links", {}).get("file", {}).get("href")
|
|
|
|
|
|
|
|
|
|
if shrlok := self.bot.get_plugin("shrlok"):
|
|
|
|
|
if shrlok := cast(ShrlokPlugin, self.bot.get_plugin("shrlok")):
|
|
|
|
|
if len(items) > 10:
|
|
|
|
|
items = random.sample(items, 10)
|
|
|
|
|
urls = map(get_img_url, items)
|
|
|
|
|
urls_text = ",".join(map(lambda url: f'"{url}"', urls))
|
|
|
|
|
html = IMG_FETCH_HTML.format(urls_text)
|
|
|
|
|
link = shrlok.post_html(html) # type: ignore
|
|
|
|
|
html = IMG_FETCH_HTML.format(urls_text).encode()
|
|
|
|
|
link = shrlok.post({"type": "raw", "ext": "html"}, html)
|
|
|
|
|
if not link:
|
|
|
|
|
self.bot.log_d("shrlok plugin returned an empty string.")
|
|
|
|
|
else:
|
|
|
|
@ -207,7 +208,7 @@ class TaxrefPlugin(Plugin):
|
|
|
|
|
else:
|
|
|
|
|
# More than one result? For simplicity sake, use the shrlok plugin
|
|
|
|
|
# if available or just show an ambiguous response.
|
|
|
|
|
if shrlok := self.bot.get_plugin("shrlok"):
|
|
|
|
|
if shrlok := cast(ShrlokPlugin, self.bot.get_plugin("shrlok")):
|
|
|
|
|
text = (
|
|
|
|
|
"\n".join(
|
|
|
|
|
(
|
|
|
|
@ -219,7 +220,7 @@ class TaxrefPlugin(Plugin):
|
|
|
|
|
)
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
|
|
|
|
reply = shrlok.post_text(text) # type: ignore
|
|
|
|
|
reply = shrlok.post({"type": "txt"}, text.encode())
|
|
|
|
|
else:
|
|
|
|
|
reply = self.get_ambiguous_reply(items)
|
|
|
|
|
|
|
|
|
|