|
|
|
@ -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."""
|
|
|
|
|