From 19d9c94ae3f01d8082ec3c0f563534a3e2446991 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Thu, 15 Jun 2023 12:10:18 -0700 Subject: [PATCH] Fix edge case of a commodity not actually being sold anywhere, but it still being able to be bought (Bexalite) --- app/star_citizen.py | 67 +++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/app/star_citizen.py b/app/star_citizen.py index d35b25f3..14a85731 100644 --- a/app/star_citizen.py +++ b/app/star_citizen.py @@ -3,6 +3,11 @@ import requests import discord import os +# prints for debug +import pprint + +pp = pprint.PrettyPrinter(indent=4) + wiki_url = "https://starcitizen.tools/" @@ -206,7 +211,7 @@ async def calculate_trade_profies(commodity, scu=None): scu_string = "%s scu" % scu if not scu: scu = 696 - scu_string = "a C2 Hercules" + scu_string = "a C2 Hercules full" commodity = commodity.capitalize() @@ -215,13 +220,20 @@ async def calculate_trade_profies(commodity, scu=None): ) try: response = requests.get(url, headers=headers).json()["data"] - + except Exception: + print(commodity) embed.set_author( - name=commodity, + name="❌ Couldnt find that commodity", url="%s%s" % (wiki_url, commodity), ) - # Loop through every system/outpost and find the cheapest place selling the commodity + embed.set_author( + name=commodity, + url="%s%s" % (wiki_url, commodity), + ) + + # Loop through every system/outpost and find the cheapest place selling the commodity + try: costs = {} for systems, stations in response.items(): for station, trades in stations.items(): @@ -230,7 +242,7 @@ async def calculate_trade_profies(commodity, scu=None): break embed.add_field( - name="**Cheapest place to fill %s**" % scu_string, + name="**Cheapest place to fill %s of %s**" % (scu_string, commodity), value="%s: $%s" % ( min(costs, key=costs.get), @@ -238,40 +250,35 @@ async def calculate_trade_profies(commodity, scu=None): ), inline=True, ) + except Exception: + pass - # 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 + # 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( - 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, - ) + embed.add_field( + name="Most profitable place to sell %s of " % (scu_string, commodity), + value="%s: $%s" + % ( + max(profits, key=profits.get), + "{:20,.2f}".format(profits[max(profits, key=profits.get)]).strip(), + ), + inline=True, + ) + 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, ) - return embed - except Exception: - print(commodity) - embed.set_author( - name="❌ Couldnt find that commodity", - url="%s%s" % (wiki_url, commodity), - ) - return embed + return embed