Compare commits

..

No commits in common. "a8b1b811c54dc27b1ba21858b3d1cbd1c91f6fd1" and "9c78abd7f365fdf342a6459abaa4f1061ef722c2" have entirely different histories.

3 changed files with 15 additions and 65 deletions

View file

@ -67,7 +67,7 @@
},
"horoscope": {
"commands": ["horoscope"],
"meditation": "/me looks at the stars",
"meditation": "/me looks at the starts",
"url": "http://zwergf.elynx.fr/bots/horobot/",
"delay": 2
},
@ -162,7 +162,7 @@
"snore_rate": 1.0
},
"taxref": {
"commands": ["taxref", "scientifize"],
"commands": ["taxref"],
"not_found_reply": "Not found!",
"reply": "{sci_name}, {fr_name}, {family}, {cd_nom}, {cd_ref}",
"ambiguous_reply": "Ambiguous! It can be: ",

View file

@ -39,5 +39,5 @@ class ShrlokPlugin(Plugin):
except OSError as exc:
self.bot.log_e(f"Can't post text file: {exc}")
return None
url = response.decode().replace(self.file_root, self.url_root, 1)
url = response.decode().replace(self.file_root, self.url_root, count=1)
return url

View file

@ -22,20 +22,11 @@ class TaxrefPlugin(Plugin):
if not self.should_handle_command(event.arguments[0]):
return False
# "taxref"
if self.command.ident == self.config["commands"][0]:
self.search_by_name(self.command.content, event.target)
# "scientifize"
if self.command.ident == self.config["commands"][1]:
self.find_scientific_name(self.command.content, event.target)
return True
def search_by_name(self, name, target):
"""Get species data from a scientific name.
Try to disambiguate the results by focusing on species only and their
scientific name.
"""
name = name.lower()
enc_name = urllib.parse.quote(name)
url = (
@ -75,8 +66,7 @@ class TaxrefPlugin(Plugin):
if item["scientificName"].lower() == name
]
if len(species_with_same_name) != 1:
reply = self.get_ambiguous_reply(species_items)
self.bot.say(target, reply)
self.show_ambiguous_reply(species_items, target)
return
item_to_use = species_with_same_name[0]
@ -90,10 +80,9 @@ class TaxrefPlugin(Plugin):
)
self.bot.say(target, reply)
if (images_reply := self.get_images_reply(item_to_use)):
self.bot.say(target, images_reply)
self.show_images(item_to_use, target)
def get_ambiguous_reply(self, items):
def show_ambiguous_reply(self, items, target):
"""Show a reply with potential species."""
reply = self.config["ambiguous_reply"]
append = ""
@ -103,64 +92,25 @@ class TaxrefPlugin(Plugin):
reply += ", ".join(item["scientificName"] for item in items)
if append:
reply += append
return reply
self.bot.say(target, reply)
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.
"""
m_url = item.get("_links", {}).get("media", {}).get("href")
def show_images(self, item, target):
"""If there are media available, show one!"""
m_url = item.get("_links", {}) .get("media", {}) .get("href")
if not m_url:
return None
return
response = requests.get(m_url)
if response.status_code != 200:
return None
self.signal_failure(target)
return
media_data = response.json()
items = media_data.get("_embedded", {}).get("media", [])
if not items:
return None
return
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 find_scientific_name(self, name, target):
"""Find a corresponding scientific name for a vernacular name."""
name = name.lower()
enc_name = urllib.parse.quote(name)
url = (
f"{BASE_URL}/taxa/search?frenchVernacularNames={enc_name}"
"&page=1&size=100"
)
response = requests.get(url)
if response.status_code != 200:
self.signal_failure(target)
return
data = response.json()
items = data.get("_embedded", {}).get("taxa", [])
if not items:
self.bot.say(target, self.config["not_found_reply"])
return
if len(items) == 1:
# Only one result: use it.
reply = items[0].get("scientificName")
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")):
text = "\n".join(
f"{i['frenchVernacularName']}{i['scientificName']}"
for i in items
) + "\n"
reply = shrlok.post_text(text)
else:
reply = self.get_ambiguous_reply(items)
self.bot.say(target, reply)
self.bot.say(target, "📷 " + media_href)