Re-doing the price and trade route code for star citizen to use the new API

This commit is contained in:
Luke Robles 2024-06-15 11:13:20 -07:00
parent 8dc67337aa
commit 75eb2fee26

View File

@ -464,7 +464,7 @@ async def get_ship(ship_name):
async def calculate_trade_profits(commodity, scu=None): async def calculate_trade_profits(commodity, scu=None):
url = "https://portal.uexcorp.space/api/all_prices/pretty_mode/1/" url = f"https://uexcorp.space/api/2.0/commodities_routes?id_commodity={await get_commodity_id(commodity)}"
headers = {"api_key": os.getenv("uexcorp_key")} headers = {"api_key": os.getenv("uexcorp_key")}
@ -476,64 +476,56 @@ async def calculate_trade_profits(commodity, scu=None):
embed = discord.Embed( embed = discord.Embed(
description="-------", color=discord.Color.blue(), type="rich" description="-------", color=discord.Color.blue(), type="rich"
) )
response = requests.get(url, headers=headers).json()["data"] response = requests.get(url, headers=headers).json()["data"][0]
embed.set_author(name=commodity) embed.set_author(name=commodity)
embed.set_thumbnail( embed.set_thumbnail(
url="https://media.starcitizen.tools/thumb/b/bb/Shubin_logo_circle.png/120px-Shubin_logo_circle.png" url="https://media.starcitizen.tools/thumb/b/bb/Shubin_logo_circle.png/120px-Shubin_logo_circle.png"
) )
# Loop through every system/outpost and find the cheapest place selling the commodity price_origin = response["price_origin"]
costs = {} price_destination = response["price_destination"]
for systems, stations in response.items(): margin = response["price_margin"]
for station, trades in stations.items(): investment = response["investment"]
if "buy" in trades and commodity in trades["buy"]: profit = response["profit"]
costs[station] = trades["buy"][commodity] * int(scu) best_buy_place = response["origin_terminal_name"]
break best_sell_place = response["destination_terminal_name"]
if len(costs):
embed.add_field(
name="**Best place to buy %s of %s**" % (scu_string, commodity),
value="%s: $%s"
% (
min(costs, key=costs.get),
"{:20,.2f}".format(costs[min(costs, key=costs.get)]).strip(),
),
inline=True,
)
# Same logic, but finding the place buying the commodity for the most
profits = {}
for systems, stations in response.items():
for station, trades in stations.items():
if "sell" in trades and commodity in trades["sell"]:
profits[station] = trades["sell"][commodity] * int(scu)
break
embed.add_field( embed.add_field(
name="Best place to sell %s of %s" % (scu_string, commodity), name="Best place to buy %s of %s" % (scu_string, commodity),
value="%s: $%s" value=best_buy_place,
% (
max(profits, key=profits.get),
"{:20,.2f}".format(profits[max(profits, key=profits.get)]).strip(),
),
inline=True, inline=True,
) )
embed.add_field(
name="Best place to sell %s of %s" % (scu_string, commodity),
value=best_sell_place,
inline=False,
)
if len(costs) and len(profits): embed.add_field(
net_profit = float(profits[max(profits, key=profits.get)]) - float( name="Initial Investment",
costs[min(costs, key=costs.get)] value="${:20,.2f} aUEC".format(investment),
) inline=False,
embed.add_field( )
name="**Net Profit**", embed.add_field(
value="$%s" % "{:20,.2f}".format(net_profit).strip(), name="Total Profit", value="${:20,.2f} aUEC".format(profit), inline=True
inline=True, )
) embed.add_field(
name="Margin", value="${:20,.2f} aUEC/scu".format(margin), inline=True
)
return embed return embed
async def get_commodity_id(commodity):
headers = {"api_key": os.getenv("uexcorp_key")}
url = "https://uexcorp.space/api/2.0/commodities"
response = requests.get(url, headers=headers).json()["data"]
for item in response:
if commodity == item["name"]:
return item["id"]
async def get_price(commodity, scu): async def get_price(commodity, scu):
url = "https://portal.uexcorp.space/api/all_prices/pretty_mode/1/" url = " https://uexcorp.space/api/2.0/commodities_prices_all"
headers = {"api_key": os.getenv("uexcorp_key")} headers = {"api_key": os.getenv("uexcorp_key")}
@ -551,11 +543,10 @@ async def get_price(commodity, scu):
# Same logic, but finding the place buying the commodity for the most # Same logic, but finding the place buying the commodity for the most
profits = {} profits = {}
for systems, stations in response.items(): for items in response:
for station, trades in stations.items(): if commodity == items["commodity_name"]:
if "sell" in trades and commodity in trades["sell"]: profits[items["terminal_name"]] = items["price_sell_avg"] * int(scu)
profits[station] = trades["sell"][commodity] * int(scu) break
break
embed.add_field( embed.add_field(
name="Best place to sell %s of %s" % (scu_string, commodity), name="Best place to sell %s of %s" % (scu_string, commodity),