Normalize symbols (to upper case). Output symbols returned by coinmarketcap.

This commit is contained in:
Chris Berkhout 2021-06-17 13:42:06 +02:00
parent 0927e27939
commit 98355efbc3
3 changed files with 18 additions and 10 deletions

View file

@ -53,8 +53,8 @@ def cli(args=None, output_file=sys.stdout):
)
sys.exit(1)
series = Series(
base=args.pair[0],
quote=args.pair[1],
base=source.normalizesymbol(args.pair[0]),
quote=source.normalizesymbol(args.pair[1]),
type=args.type or (source.types() + ["(none)"])[0],
start=start,
end=args.end,

View file

@ -36,6 +36,9 @@ class BaseSource(ABC):
def notes(self) -> str:
pass
def normalizesymbol(self, str) -> str:
return str.upper()
@abstractmethod
def symbols(self) -> list[(str, str)]:
pass

View file

@ -55,7 +55,7 @@ class CoinMarketCap(BaseSource):
amount = self._amount(next(iter(item["quote"].values())), series.type)
prices.append(Price(d, amount))
output_base, output_quote = self._output_pair(series.base, series.quote)
output_base, output_quote = self._output_pair(series.base, series.quote, data)
return dataclasses.replace(
series, base=output_base, quote=output_quote, prices=prices
@ -66,12 +66,12 @@ class CoinMarketCap(BaseSource):
params = {}
if series.base.startswith("id="):
if series.base.startswith("ID="):
params["id"] = series.base[3:]
else:
params["symbol"] = series.base
if series.quote.startswith("id="):
if series.quote.startswith("ID="):
params["convert_id"] = series.quote[3:]
else:
params["convert"] = series.quote
@ -95,12 +95,17 @@ class CoinMarketCap(BaseSource):
else:
return Decimal(str(data[type]))
def _output_pair(self, base, quote):
if base.startswith("id=") or quote.startswith("id="):
symbols = {i["id"]: (i["symbol"] or i["code"]) for i in self._symbol_data()}
def _output_pair(self, base, quote, data):
data_base = data["data"]["symbol"]
data_quote = next(iter(data["data"]["quotes"][0]["quote"].keys()))
output_base = symbols[int(base[3:])] if base.startswith("id=") else base
output_quote = symbols[int(quote[3:])] if quote.startswith("id=") else quote
lookup_quote = False
if quote.startswith("ID="):
symbols = {i["id"]: (i["symbol"] or i["code"]) for i in self._symbol_data()}
lookup_quote = symbols[int(quote[3:])]
output_base = data_base
output_quote = lookup_quote or data_quote
return (output_base, output_quote)