taxref: use shrlok to show images

This commit is contained in:
dece 2022-07-10 18:25:40 +02:00
parent 659dae9ce6
commit 1b2303617b
2 changed files with 55 additions and 11 deletions

View file

@ -29,15 +29,22 @@ class ShrlokPlugin(Plugin):
self.bot.log_d("No socket path specified, shrlok plugin disabled.")
self.is_ready = False
def post_text(self, content):
def post(self, content_type, content_data):
header = '{{"type":"{}"}}'.format(content_type).encode() + b"\0"
try:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.connect(self.socket)
data = b'{"type":"txt"}\0' + content.encode()
data = header + content_data
sock.sendall(str(len(data)).encode() + b"\0" + data)
response = sock.recv(4096)
except OSError as exc:
self.bot.log_e(f"Can't post text file: {exc}")
self.bot.log_e(f"Can't post data: {exc}")
return None
url = response.decode().replace(self.file_root, self.url_root, 1)
return url
def post_text(self, text):
self.post("txt", text.encode())
def post_html(self, html):
self.post("html", html.encode())

View file

@ -6,6 +6,25 @@ import requests
from edmond.plugin import Plugin
BASE_URL = "https://taxref.mnhn.fr/api"
IMG_FETCH_HTML = """\
<!doctype html>
<html>
<head><meta charset="UTF-8"/></head>
<body></body>
<script>
const urls = [{}];
urls.forEach(url => {{
fetch(url)
.then(r => r.blob())
.then(blob => {{
let img = document.createElement("img");
img.src = window.URL.createObjectURL(blob);
document.body.appendChild(img);
}});
}});
</script>
</html>
"""
class TaxrefPlugin(Plugin):
@ -108,13 +127,21 @@ class TaxrefPlugin(Plugin):
def get_images_reply(self, item):
"""If there are media available, return one in a message.
Return a string with an URL to an image if one is available, or an
None if no image could be found or we encountered an error.
If shrlok is available, return a link to an HTML page shared by shrlok.
The HTML page, whose source code is generated from the template
IMG_FETCH_HTML, fetches a random sample of 1 to 10 images from the
results and embed the images directly into the page so it is not
necessary to download the images before seeing them.
If shrlok is not available, return a string with an URL to an image if
one is available, or None if no image could be found or we encountered
an error. The image is selected randomly. Yes, media links on TAXREF
are downloaded by the browser and not shown directly, thus the benefits
of having shrlok available.
"""
m_url = item.get("_links", {}).get("media", {}).get("href")
if not m_url:
return None
response = requests.get(m_url)
if response.status_code != 200:
return None
@ -123,11 +150,21 @@ class TaxrefPlugin(Plugin):
if not items:
return None
random_item = random.choice(items)
media_href = random_item.get("_links", {}).get("file", {}).get("href")
if not media_href:
return None
return "📷 " + media_href
def get_img_url(item):
return item.get("_links", {}).get("file", {}).get("href")
if (shrlok := 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)
else:
link = get_img_url(random.choice(items))
if link:
return "📷 " + link
def find_scientific_name(self, name, target):
"""Find a corresponding scientific name for a vernacular name."""