from bs4 import BeautifulSoup import requests import discord import os wiki_url = "https://starcitizen.tools/" async def get_ship(ship_name): base_url = "https://robertspaceindustries.com" try: wiki_ship_name = "_".join(elem for elem in ship_name.split()) headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0" } response = requests.get(wiki_url + wiki_ship_name, headers=headers).text soup = BeautifulSoup(response, "html.parser") embed = discord.Embed( description="-------", color=discord.Color.blue(), type="rich" ) ship_image = (soup.find("a", {"class": "mw-file-description"})).img["src"] ship_name_on_page = soup.find("span", {"class": "mw-page-title-main"}).text try: manufacturer = ( soup.find("div", {"class": "infobox__subtitle infobox__data"}) .findNext("a") .text ) except: manufacturer = "" embed.set_thumbnail( url="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png" ) embed.set_author( name="%s %s" % (manufacturer, ship_name_on_page), url="%s%s" % (wiki_url, wiki_ship_name), ) try: ship_role = ( soup.find("div", {"class": "infobox__label"}, string="Role") .findNext("div") .text ) except: ship_role = "" embed.add_field(name="**Role**", value=ship_role) try: ingame_price = soup.find("td", {"class": "Price smwtype_qty"}).text price = "[%s](%s%s#Universe_availability)" % ( ingame_price, wiki_url, wiki_ship_name, ) if "Not available" in ingame_price: price = "N/A" except: price = "N/A" embed.add_field( name="**Ingame Price**", value=price, inline=True, ) try: pledge_price = ( soup.find("div", {"class": "infobox__label"}, string="Standalone") .findNext("span", {"class": "smwtext"}) .text ) except Exception: pledge_price = "N/A" try: links = soup.find_all("a", {"class": "external text"}) pledge_store_link = list(filter(lambda x: "Pledge" in x.text, links))[0][ "href" ] embed.add_field( name="**Pledge Price**", value="[%s](%s#buying-options)" % (pledge_price, pledge_store_link), inline=True, ) except Exception: embed.add_field( name="**Pledge Price**", value=pledge_price, inline=True, ) buy_location = soup.find_all("td", {"class": "Location smwtype_wpg"}) if len(buy_location): embed.add_field( name="**Buyable At**", value=", ".join(str(x.text) for x in buy_location), inline=True, ) embed.add_field(name="-------", value="", inline=False) try: cargo_capacity = ( soup.find("div", {"class": "infobox__label"}, string="Cargo") .findNext("span", {"class": "smwtext"}) .text ) except Exception: cargo_capacity = "N/A" embed.add_field(name="**Cargo Capacity**", value=cargo_capacity, inline=True) try: top_speed = ( soup.find("div", {"class": "infobox__label"}, string="Max speed") .findNext("span", {"class": "smwtext"}) .text ) except Exception: top_speed = "N/A" embed.add_field(name="**Top Speed**", value=top_speed, inline=True) try: crew_size = ( soup.find("div", {"class": "infobox__label"}, string="Crew") .findNext("div", {"class": "infobox__data"}) .text ) except Exception: crew_size = "N/A" embed.add_field(name="**Crew Size**", value=crew_size, inline=True) embed.add_field(name="-------", value="", inline=False) try: claim_time = ( soup.find("div", {"class": "infobox__label"}, string="Claim") .findNext("div", {"class": "infobox__data"}) .text ) except Exception: claim_time = "N/A" embed.add_field(name="**Insurance claim time**", value=claim_time, inline=True) try: expedited_claim_time = ( soup.find("div", {"class": "infobox__label"}, string="Expedite") .findNext("div", {"class": "infobox__data"}) .text ) except Exception: expedited_claim_time = "N/A" embed.add_field( name="**Expedite claim time**", value=expedited_claim_time, inline=True ) try: expedite_fee = ( soup.find("div", {"class": "infobox__label"}, string="Expedite fee") .findNext("div", {"class": "infobox__data"}) .text ) except Exception: expedite_fee = "N/A" embed.add_field(name="**Expedite Fee**", value=expedite_fee, inline=True) embed.add_field( name="**Link**", value="%s%s" % (wiki_url, wiki_ship_name), inline=False ) embed.set_image(url=ship_image) except Exception as e: print(e) 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?\n\n(Its also possible my shitty code isnt working)" % ship_name, inline=True, ) return embed return embed async def calculate_trade_profies(commodity, scu=None): url = "https://portal.uexcorp.space/api/all_prices/pretty_mode/1/" headers = {"api_key": os.getenv("uexcorp_key")} scu_string = "%s scu" % scu if not scu: scu = 696 scu_string = "a C2 Hercules" commodity = commodity.capitalize() embed = discord.Embed( description="-------", color=discord.Color.blue(), type="rich" ) try: response = requests.get(url, headers=headers).json()["data"] embed.set_author( name=commodity, url="%s%s" % (wiki_url, commodity), ) costs = {} for systems, stations in response.items(): for station, trades in stations.items(): if "buy" in trades and commodity in trades["buy"]: cost_to_fill = trades["buy"][commodity] * int(scu) costs[station] = cost_to_fill embed.add_field( name="**Cheapest place to fill %s**" % scu_string, value="%s: $%s" % ( min(costs, key=costs.get), "{:20,.2f}".format(costs[min(costs, key=costs.get)]).strip(), ), inline=True, ) profits = {} for systems, stations in response.items(): for station, trades in stations.items(): if "sell" in trades and commodity in trades["sell"]: net_profit = trades["sell"][commodity] * int(scu) profits[station] = net_profit embed.add_field( name="Most profitable place to sell %s" % scu_string, value="%s: $%s" % ( max(profits, key=profits.get), "{:20,.2f}".format(profits[max(profits, key=profits.get)]).strip(), ), inline=True, ) net_gain = float(profits[max(profits, key=profits.get)]) - float( costs[min(costs, key=costs.get)] ) embed.add_field( name="**Net Gain**", value="$%s" % "{:20,.2f}".format(net_gain).strip(), inline=True, ) return embed except Exception: print(commodity) embed.set_author( name="❌ Couldnt find that commodity", url="%s%s" % (wiki_url, commodity), ) return embed