X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=portfolio.py;h=1d8bfd5f122775df881bcbbdd3a76e8422740896;hb=f2da658998b6e6605c6ae27ff338ef23b96dce25;hp=507f79642c62aa64343251747706173d9bf31bb8;hpb=dd359bc0617a915909efb2ef37048192c0639836;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/portfolio.py b/portfolio.py index 507f796..1d8bfd5 100644 --- a/portfolio.py +++ b/portfolio.py @@ -32,8 +32,14 @@ class Portfolio: urllib3.disable_warnings() http = urllib3.PoolManager() - r = http.request("GET", cls.URL) - cls.data = json.loads(r.data) + try: + r = http.request("GET", cls.URL) + except Exception: + return + try: + cls.data = json.loads(r.data) + except json.JSONDecodeError: + cls.data = None @classmethod def parse_cryptoportfolio(cls): @@ -214,6 +220,10 @@ class Balance: self.free = Amount(currency, free_value) self.used = Amount(currency, used_value) + @classmethod + def from_hash(cls, currency, hash_): + return cls(currency, hash_["total"], hash_["free"], hash_["used"]) + @classmethod def in_currency(cls, other_currency, market, action="average", type="total"): amounts = {} @@ -228,10 +238,6 @@ class Balance: def currencies(cls): return cls.known_balances.keys() - @classmethod - def from_hash(cls, currency, hash_): - return cls(currency, hash_["total"], hash_["free"], hash_["used"]) - @classmethod def _fill_balances(cls, hash_): for key in hash_: @@ -264,9 +270,6 @@ class Balance: new_repartition = cls.dispatch_assets(total_base_value) Trade.compute_trades(values_in_base, new_repartition, market=market) - def __int__(self): - return int(self.total) - def __repr__(self): return "Balance({} [{}/{}/{}])".format(self.currency, str(self.free), str(self.used), str(self.total))