taxref: use shrlok to show images
This commit is contained in:
parent
659dae9ce6
commit
1b2303617b
|
@ -29,15 +29,22 @@ class ShrlokPlugin(Plugin):
|
||||||
self.bot.log_d("No socket path specified, shrlok plugin disabled.")
|
self.bot.log_d("No socket path specified, shrlok plugin disabled.")
|
||||||
self.is_ready = False
|
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:
|
try:
|
||||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
||||||
sock.connect(self.socket)
|
sock.connect(self.socket)
|
||||||
data = b'{"type":"txt"}\0' + content.encode()
|
data = header + content_data
|
||||||
sock.sendall(str(len(data)).encode() + b"\0" + data)
|
sock.sendall(str(len(data)).encode() + b"\0" + data)
|
||||||
response = sock.recv(4096)
|
response = sock.recv(4096)
|
||||||
except OSError as exc:
|
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
|
return None
|
||||||
url = response.decode().replace(self.file_root, self.url_root, 1)
|
url = response.decode().replace(self.file_root, self.url_root, 1)
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
def post_text(self, text):
|
||||||
|
self.post("txt", text.encode())
|
||||||
|
|
||||||
|
def post_html(self, html):
|
||||||
|
self.post("html", html.encode())
|
||||||
|
|
|
@ -6,6 +6,25 @@ import requests
|
||||||
from edmond.plugin import Plugin
|
from edmond.plugin import Plugin
|
||||||
|
|
||||||
BASE_URL = "https://taxref.mnhn.fr/api"
|
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):
|
class TaxrefPlugin(Plugin):
|
||||||
|
@ -108,13 +127,21 @@ class TaxrefPlugin(Plugin):
|
||||||
def get_images_reply(self, item):
|
def get_images_reply(self, item):
|
||||||
"""If there are media available, return one in a message.
|
"""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
|
If shrlok is available, return a link to an HTML page shared by shrlok.
|
||||||
None if no image could be found or we encountered an error.
|
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")
|
m_url = item.get("_links", {}).get("media", {}).get("href")
|
||||||
if not m_url:
|
if not m_url:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
response = requests.get(m_url)
|
response = requests.get(m_url)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return None
|
return None
|
||||||
|
@ -123,11 +150,21 @@ class TaxrefPlugin(Plugin):
|
||||||
if not items:
|
if not items:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
random_item = random.choice(items)
|
def get_img_url(item):
|
||||||
media_href = random_item.get("_links", {}).get("file", {}).get("href")
|
return item.get("_links", {}).get("file", {}).get("href")
|
||||||
if not media_href:
|
|
||||||
return None
|
if (shrlok := self.bot.get_plugin("shrlok")):
|
||||||
return "📷 " + media_href
|
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):
|
def find_scientific_name(self, name, target):
|
||||||
"""Find a corresponding scientific name for a vernacular name."""
|
"""Find a corresponding scientific name for a vernacular name."""
|
||||||
|
|
Loading…
Reference in a new issue