diff --git a/app/star_citizen.py b/app/star_citizen.py index e044505b..d5f5c5d4 100755 --- a/app/star_citizen.py +++ b/app/star_citizen.py @@ -464,7 +464,7 @@ async def get_ship(ship_name): 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")} @@ -476,64 +476,56 @@ async def calculate_trade_profits(commodity, scu=None): embed = discord.Embed( 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_thumbnail( 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 - costs = {} - for systems, stations in response.items(): - for station, trades in stations.items(): - if "buy" in trades and commodity in trades["buy"]: - costs[station] = trades["buy"][commodity] * int(scu) - break - - 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 + price_origin = response["price_origin"] + price_destination = response["price_destination"] + margin = response["price_margin"] + investment = response["investment"] + profit = response["profit"] + best_buy_place = response["origin_terminal_name"] + best_sell_place = response["destination_terminal_name"] embed.add_field( - name="Best place to sell %s of %s" % (scu_string, commodity), - value="%s: $%s" - % ( - max(profits, key=profits.get), - "{:20,.2f}".format(profits[max(profits, key=profits.get)]).strip(), - ), + name="Best place to buy %s of %s" % (scu_string, commodity), + value=best_buy_place, 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): - net_profit = float(profits[max(profits, key=profits.get)]) - float( - costs[min(costs, key=costs.get)] - ) - embed.add_field( - name="**Net Profit**", - value="$%s" % "{:20,.2f}".format(net_profit).strip(), - inline=True, - ) - + embed.add_field( + name="Initial Investment", + value="${:20,.2f} aUEC".format(investment), + inline=False, + ) + embed.add_field( + name="Total Profit", value="${:20,.2f} aUEC".format(profit), inline=True + ) + embed.add_field( + name="Margin", value="${:20,.2f} aUEC/scu".format(margin), inline=True + ) 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): - 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")} @@ -551,11 +543,10 @@ async def get_price(commodity, scu): # 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 + for items in response: + if commodity == items["commodity_name"]: + profits[items["terminal_name"]] = items["price_sell_avg"] * int(scu) + break embed.add_field( name="Best place to sell %s of %s" % (scu_string, commodity),