Fix ship name captalizing when there is a space in teh name, also returning the error embed object

This commit is contained in:
Luke Robles 2023-04-28 13:28:10 -07:00
parent 0f172b7f43
commit 676e802d52

View File

@ -1,32 +1,16 @@
from bs4 import BeautifulSoup
import requests
import discord
import pprint
async def get_ship(ship_name):
base_url = "https://robertspaceindustries.com"
wiki_url = "https://starcitizen.tools/"
# try:
# # ship = await Ship(name=ship_name).get_ships_pages_async()
# # ship = ship[0]
# except IndexError:
# embed = discord.Embed(description="❌", color=discord.Color.red(), type="rich")
# embed.add_field(
# name="**Could not find that ship**",
# value="You gave me %s. Did you spell it right? (Its also possible my shitty code isnt working right)"
# % ship_name,
# inline=True,
# )
# return embed
try:
wiki_ship_name = " ".join(elem.capitalize() for elem in ship_name.split())
# prints for debug
pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(ship)
# wiki_ship_name = ship["url"].split("/")[-1].replace("-", "_")
wiki_ship_name = ship_name.replace(" ", "_").title()
# print(wiki_ship_name)
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0"
@ -71,7 +55,9 @@ async def get_ship(ship_name):
.text
)
embed.add_field(name="**Stowage Capacity**", value=stowage_capacity, inline=True)
embed.add_field(
name="**Stowage Capacity**", value=stowage_capacity, inline=True
)
cargo_capacity = (
soup.find("tr", {"class": "data-cargocapacity infobox-data infobox-col4"})
@ -114,6 +100,17 @@ async def get_ship(ship_name):
embed.add_field(name="**Expedite Fee**", value=expedited_fee, inline=True)
embed.add_field(
name="**Link**", value="%s%s" % (wiki_url, wiki_ship_name), inline=False
name="**Link**", value="%s/%s" % (wiki_url, wiki_ship_name), inline=False
)
except Exception:
embed = discord.Embed(description="", color=discord.Color.red(), type="rich")
embed.add_field(
name="**Could not find that ship**",
value="You gave me %s. Did you spell it right? (Its also possible my shitty code isnt working right)"
% ship_name,
inline=True,
)
return embed
return embed