Skip to content

Commit 3b35344

Browse files
committed
Add wikiphyto for plants' informations in game
1 parent 86a12b1 commit 3b35344

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

libs/commands.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,24 @@ def get_syntax(cmnd):
8484
embed.add_field(name="Description", value=cmnd.help, inline=True)
8585
else:
8686
embed.add_field(name="Erreur : commande inconnue", value=f"Entrez `{self.PREFIX}aide` pour avoir la liste des commandes.")
87-
87+
88+
await ctx.send(embed=embed)
89+
8890
else:
89-
embed = discord.Embed(title="Rubrique d'aide", description=f"Entrez : `{self.PREFIX}aide <commande>` pour plus d'informations.", color=8421504)
91+
fields = []
9092
for cmnd in self.get_commands():
91-
embed.add_field(name=cmnd.brief, value=get_syntax(cmnd), inline=False)
92-
await ctx.send(embed=embed)
93+
fields.append((cmnd.brief, get_syntax(cmnd)))
94+
95+
nb = len(fields) // 25 + 1
96+
index = 1
97+
while fields:
98+
embed = discord.Embed(title=f"Rubrique d'aide ({index}/{nb})", description=f"Entrez : `{self.PREFIX}aide <commande>` pour plus d'informations.", color=8421504)
99+
for field in fields[: 25]:
100+
embed.add_field(name=field[0], value=field[1], inline=False)
101+
fields = fields[25: ]
102+
index += 1
103+
await ctx.send(embed=embed)
104+
93105

94106

95107
@commands.command(help="Votre espèce est à préciser impérativement. Si aucun pseudonyme n'est précisé, Odyssée prendra votre nom d'utilisateur.", brief="Créer un nouveau joueur")
@@ -585,7 +597,6 @@ async def achat(self, ctx, nom: str, nombre: int=1):
585597
if shop_id == -1: await send_error(ctx, f"__{player.name}__ n'est pas dans un magasin"); return
586598

587599
obj = get_official_name(nom.lower())
588-
print(obj)
589600
obj = get_object(obj, shop_id)
590601
if obj.shop_id == -1: await send_error(ctx, f"cet objet : '{nom}' n'est pas vendu ici"); return
591602

@@ -868,4 +879,30 @@ async def etat(self, ctx, nouvel_etat: str):
868879
else:
869880
send_error(ctx, f"{nouvel_etat} n'est pas un état connu")
870881

871-
self.save_game()
882+
self.save_game()
883+
884+
@commands.command(name="plante", help="Donne des informations sur la plante demandée (source : wikiphyto.org)", brief="Informations sur une plante")
885+
async def plante(self, ctx, nom: str):
886+
result, check_code = wikiphyto_api(nom)
887+
# homonymie
888+
if check_code == 0:
889+
embed = discord.Embed(title=nom, description=f"Plusieurs plantes correspondent à la recherche : '{nom}'", color=8421504)
890+
embed.add_field(name="Essayez un nom de plante suivant", value=" ❖ " + " ❖ ".join(result))
891+
# pas de résultat
892+
elif check_code == 1:
893+
embed = discord.Embed(title=nom, description="Erreur", color=8421504)
894+
embed.add_field(name="Plante non trouvée", value=f"Aucune plante ne correspond à la recherche : '{nom}'")
895+
# succès
896+
else:
897+
latin_name, description, used_parts, properties, img, url = result
898+
embed = discord.Embed(title=nom, description=latin_name, color=8421504)
899+
if img: embed.set_image(url=img)
900+
embed.set_footer(text=url)
901+
embed.add_field(name="Description et habitat", value="\n".join(description), inline=True)
902+
embed.add_field(name="Parties utilisées", value="\n".join(used_parts), inline=True)
903+
embed.add_field(name="Propriétés de la plante", value="\n".join(properties[0]), inline=False)
904+
embed.add_field(name="Propriétés du bourgeon", value="\n".join(properties[1]), inline=True)
905+
embed.add_field(name="Propriétés de l'huile essentielle", value="\n".join(properties[2]), inline=True)
906+
907+
await ctx.send(embed=embed)
908+

libs/lib_odyssee.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from libs.players import *
66
from libs.travel import *
77
from libs.states import *
8+
from libs.wikiphyto import wikiphyto_api
89

910

1011
# --- Fonctions de sauvegarde --- #

libs/wikiphyto.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
def check_search(page):
5+
if "Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom." in page.text:
6+
urls = []
7+
for i in page.find("ul").find_all("li"):
8+
url = i.find("a")
9+
if not "redlink=1" in url.get("href"): urls.append(f"{url.get('title')}\n")
10+
return 0, urls
11+
elif "Il n'y a pour l'instant aucun texte sur cette page." in page.text:
12+
return 1, None
13+
else:
14+
return 2, None
15+
16+
17+
def clean(string):
18+
while "[" in string and "]" in string:
19+
start = string.find("[")
20+
end = string.find("]", start) + 2
21+
string = string[: start] + string[end: ]
22+
23+
return string
24+
25+
26+
def wikiphyto_api(plant_name):
27+
url = f"http://www.wikiphyto.org/wiki/{plant_name}"
28+
page = requests.get(url)
29+
status_code = page.status_code
30+
page = BeautifulSoup(page.text, features="html5lib")
31+
32+
check_code, suggestion = check_search(page)
33+
if check_code == 0: return suggestion, 0
34+
elif check_code == 1 or status_code != 200: return None, 1
35+
36+
try:
37+
img = f"http://www.wikiphyto.org{page.find('img', {'class': 'thumbimage'}).get('src')}"
38+
except:
39+
img = None
40+
41+
latin_name = page.find("span", {"id": "D.C3.A9nomination_latine_internationale"}).find_next().text
42+
43+
description = page.find("span", {"id": "Description_et_habitat"}).find_next().text
44+
if len(description) > 1000: description = description[: 1000] + "…"
45+
description = [f" ❖ {clean(i)}" for i in description.split("\n")]
46+
47+
used_parts = page.find("span", {"id": "Parties_utilis.C3.A9es"}).find_next().text
48+
if len(used_parts) > 1000: used_parts = used_parts[: 1000] + "…"
49+
used_parts = [f" ❖ {clean(i)}" for i in used_parts.split("\n")]
50+
51+
52+
plant_properties = page.find("span", {"id": "Propri.C3.A9t.C3.A9s_de_la_plante"}).find_next().text
53+
plant_properties = [f" ❖ {clean(i)}" for i in plant_properties.split("\n")[: 5] if i != "Propriétés du bourgeon"]
54+
if not plant_properties: plant_properties = ["< aucune >"]
55+
56+
bud_properties = page.find("span", {"id": "Propri.C3.A9t.C3.A9s_du_bourgeon"}).find_next().text
57+
bud_properties = [f" ❖ {clean(i)}" for i in bud_properties.split("\n")[: 5] if i != "Propriétés de l'huile essentielle"]
58+
if not bud_properties: bud_properties = ["< aucune >"]
59+
60+
oil_properties = page.find("span", {"id": "Propri.C3.A9t.C3.A9s_de_l.27huile_essentielle"}).find_next().text
61+
oil_properties = [f" ❖ {clean(i)}" for i in oil_properties.split("\n")[: 5] if i != "Indications"]
62+
if not oil_properties: oil_properties = ["< aucune >"]
63+
64+
return (clean(latin_name), description, used_parts, (plant_properties, bud_properties, oil_properties), img, url), 2

0 commit comments

Comments
 (0)