Compare commits

..

No commits in common. "3be1fddd93f8a4b01f3f7634bfedb64f0c1a522c" and "0d49fb145715c429bd6155037a55ca181a04908c" have entirely different histories.

3 changed files with 21 additions and 44 deletions

View file

@ -165,8 +165,7 @@
"commands": ["taxref"], "commands": ["taxref"],
"not_found_reply": "Not found!", "not_found_reply": "Not found!",
"reply": "{sci_name}, {fr_name}, {family}, {cd_nom}, {cd_ref}", "reply": "{sci_name}, {fr_name}, {family}, {cd_nom}, {cd_ref}",
"ambiguous_reply": "Ambiguous! It can be: ", "ambiguous_reply": "Ambiguous! It can be: "
"unnamed_species": "(no name)"
}, },
"translate": { "translate": {
"commands": ["translate"], "commands": ["translate"],

View file

@ -30,7 +30,7 @@ class CaptureGivePlugin(Plugin):
# "give" command. # "give" command.
if self.command.ident == self.config["commands"][0]: if self.command.ident == self.config["commands"][0]:
content = self.command.raw[len(self.command.matched):].strip() content = self.command.raw[len(self.command.matched):].strip()
matched = self.content_re.match(content) matched = self.content_re.matched(content)
if not matched: if not matched:
return False return False
groups = matched.groupdict() groups = matched.groupdict()

View file

@ -11,8 +11,7 @@ BASE_URL = "https://taxref.mnhn.fr/api"
class TaxrefPlugin(Plugin): class TaxrefPlugin(Plugin):
REQUIRED_CONFIGS = [ REQUIRED_CONFIGS = [
"commands", "not_found_reply", "reply", "ambiguous_reply", "commands", "not_found_reply", "reply", "ambiguous_reply"
"unnamed_species"
] ]
def __init__(self, bot): def __init__(self, bot):
@ -27,12 +26,8 @@ class TaxrefPlugin(Plugin):
return True return True
def search_by_name(self, name, target): def search_by_name(self, name, target):
name = name.lower() name = urllib.parse.quote(name.lower())
enc_name = urllib.parse.quote(name) url = f"{BASE_URL}/taxa/search?scientificNames={name}&page=1&size=100"
url = (
f"{BASE_URL}/taxa/search?scientificNames={enc_name}"
"&page=1&size=100"
)
response = requests.get(url) response = requests.get(url)
if response.status_code != 200: if response.status_code != 200:
self.signal_failure(target) self.signal_failure(target)
@ -54,49 +49,32 @@ class TaxrefPlugin(Plugin):
for item in items: for item in items:
if item["rankId"] == "ES": if item["rankId"] == "ES":
species_items.append(item) species_items.append(item)
num_species = len(species_items) if len(species_items) == 1:
self.bot.log_d(f"{num_species} species.")
if num_species == 1:
item_to_use = species_items[0] item_to_use = species_items[0]
else: else:
# If there are several species, check if one of them has the reply = self.config["ambiguous_reply"]
# exact same name; else show an ambiguous reply. append = ""
species_with_same_name = [ if len(items) > 5:
item for item in species_items append = f"… (+{len(items)})"
if item["scientificName"].lower() == name items = items[:5]
] reply += ", ".join(
if len(species_with_same_name) != 1: item["scientificName"] for item in items
self.show_ambiguous_reply(species_items, target) )
if append:
reply += append
self.bot.say(target, reply)
return return
item_to_use = species_with_same_name[0]
unnamed = self.config["unnamed_species"]
reply = self.config["reply"].format( reply = self.config["reply"].format(
sci_name=item_to_use["scientificName"], sci_name=item_to_use["scientificName"],
fr_name=item_to_use["frenchVernacularName"] or unnamed, fr_name=item_to_use["frenchVernacularName"],
family=item_to_use["familyName"], family=item_to_use["familyName"],
cd_nom=item_to_use["id"], cd_nom=item_to_use["id"],
cd_ref=item_to_use["referenceId"], cd_ref=item_to_use["referenceId"],
) )
self.bot.say(target, reply) self.bot.say(target, reply)
self.show_images(item_to_use, target) # If there are media available, show one!
m_url = item_to_use.get("_links", {}) .get("media", {}) .get("href")
def show_ambiguous_reply(self, items, target):
"""Show a reply with potential species."""
reply = self.config["ambiguous_reply"]
append = ""
if len(items) > 5:
append = f"… (+{len(items)})"
items = items[:5]
reply += ", ".join(item["scientificName"] for item in items)
if append:
reply += append
self.bot.say(target, reply)
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: if not m_url:
return return