Compare commits
No commits in common. "a8b1b811c54dc27b1ba21858b3d1cbd1c91f6fd1" and "9c78abd7f365fdf342a6459abaa4f1061ef722c2" have entirely different histories.
a8b1b811c5
...
9c78abd7f3
|
@ -67,7 +67,7 @@
|
||||||
},
|
},
|
||||||
"horoscope": {
|
"horoscope": {
|
||||||
"commands": ["horoscope"],
|
"commands": ["horoscope"],
|
||||||
"meditation": "/me looks at the stars",
|
"meditation": "/me looks at the starts",
|
||||||
"url": "http://zwergf.elynx.fr/bots/horobot/",
|
"url": "http://zwergf.elynx.fr/bots/horobot/",
|
||||||
"delay": 2
|
"delay": 2
|
||||||
},
|
},
|
||||||
|
@ -162,7 +162,7 @@
|
||||||
"snore_rate": 1.0
|
"snore_rate": 1.0
|
||||||
},
|
},
|
||||||
"taxref": {
|
"taxref": {
|
||||||
"commands": ["taxref", "scientifize"],
|
"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: ",
|
||||||
|
|
|
@ -39,5 +39,5 @@ class ShrlokPlugin(Plugin):
|
||||||
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 text file: {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, count=1)
|
||||||
return url
|
return url
|
||||||
|
|
|
@ -22,20 +22,11 @@ class TaxrefPlugin(Plugin):
|
||||||
if not self.should_handle_command(event.arguments[0]):
|
if not self.should_handle_command(event.arguments[0]):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# "taxref"
|
|
||||||
if self.command.ident == self.config["commands"][0]:
|
if self.command.ident == self.config["commands"][0]:
|
||||||
self.search_by_name(self.command.content, event.target)
|
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
|
return True
|
||||||
|
|
||||||
def search_by_name(self, name, target):
|
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()
|
name = name.lower()
|
||||||
enc_name = urllib.parse.quote(name)
|
enc_name = urllib.parse.quote(name)
|
||||||
url = (
|
url = (
|
||||||
|
@ -75,8 +66,7 @@ class TaxrefPlugin(Plugin):
|
||||||
if item["scientificName"].lower() == name
|
if item["scientificName"].lower() == name
|
||||||
]
|
]
|
||||||
if len(species_with_same_name) != 1:
|
if len(species_with_same_name) != 1:
|
||||||
reply = self.get_ambiguous_reply(species_items)
|
self.show_ambiguous_reply(species_items, target)
|
||||||
self.bot.say(target, reply)
|
|
||||||
return
|
return
|
||||||
item_to_use = species_with_same_name[0]
|
item_to_use = species_with_same_name[0]
|
||||||
|
|
||||||
|
@ -90,10 +80,9 @@ class TaxrefPlugin(Plugin):
|
||||||
)
|
)
|
||||||
self.bot.say(target, reply)
|
self.bot.say(target, reply)
|
||||||
|
|
||||||
if (images_reply := self.get_images_reply(item_to_use)):
|
self.show_images(item_to_use, target)
|
||||||
self.bot.say(target, images_reply)
|
|
||||||
|
|
||||||
def get_ambiguous_reply(self, items):
|
def show_ambiguous_reply(self, items, target):
|
||||||
"""Show a reply with potential species."""
|
"""Show a reply with potential species."""
|
||||||
reply = self.config["ambiguous_reply"]
|
reply = self.config["ambiguous_reply"]
|
||||||
append = ""
|
append = ""
|
||||||
|
@ -103,64 +92,25 @@ class TaxrefPlugin(Plugin):
|
||||||
reply += ", ".join(item["scientificName"] for item in items)
|
reply += ", ".join(item["scientificName"] for item in items)
|
||||||
if append:
|
if append:
|
||||||
reply += append
|
reply += append
|
||||||
return reply
|
self.bot.say(target, reply)
|
||||||
|
|
||||||
def get_images_reply(self, item):
|
def show_images(self, item, target):
|
||||||
"""If there are media available, return one in a message.
|
"""If there are media available, show one!"""
|
||||||
|
|
||||||
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")
|
m_url = item.get("_links", {}) .get("media", {}) .get("href")
|
||||||
if not m_url:
|
if not m_url:
|
||||||
return None
|
return
|
||||||
|
|
||||||
response = requests.get(m_url)
|
response = requests.get(m_url)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return None
|
self.signal_failure(target)
|
||||||
|
return
|
||||||
media_data = response.json()
|
media_data = response.json()
|
||||||
items = media_data.get("_embedded", {}).get("media", [])
|
items = media_data.get("_embedded", {}).get("media", [])
|
||||||
if not items:
|
if not items:
|
||||||
return None
|
return
|
||||||
|
|
||||||
random_item = random.choice(items)
|
random_item = random.choice(items)
|
||||||
media_href = random_item.get("_links", {}).get("file", {}).get("href")
|
media_href = random_item.get("_links", {}).get("file", {}).get("href")
|
||||||
if not media_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
|
return
|
||||||
data = response.json()
|
self.bot.say(target, "📷 " + media_href)
|
||||||
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)
|
|
||||||
|
|
Loading…
Reference in a new issue