Normalize symbols (to upper case). Output symbols returned by coinmarketcap.
This commit is contained in:
parent
0927e27939
commit
98355efbc3
3 changed files with 18 additions and 10 deletions
|
@ -53,8 +53,8 @@ def cli(args=None, output_file=sys.stdout):
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
series = Series(
|
series = Series(
|
||||||
base=args.pair[0],
|
base=source.normalizesymbol(args.pair[0]),
|
||||||
quote=args.pair[1],
|
quote=source.normalizesymbol(args.pair[1]),
|
||||||
type=args.type or (source.types() + ["(none)"])[0],
|
type=args.type or (source.types() + ["(none)"])[0],
|
||||||
start=start,
|
start=start,
|
||||||
end=args.end,
|
end=args.end,
|
||||||
|
|
|
@ -36,6 +36,9 @@ class BaseSource(ABC):
|
||||||
def notes(self) -> str:
|
def notes(self) -> str:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def normalizesymbol(self, str) -> str:
|
||||||
|
return str.upper()
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def symbols(self) -> list[(str, str)]:
|
def symbols(self) -> list[(str, str)]:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -55,7 +55,7 @@ class CoinMarketCap(BaseSource):
|
||||||
amount = self._amount(next(iter(item["quote"].values())), series.type)
|
amount = self._amount(next(iter(item["quote"].values())), series.type)
|
||||||
prices.append(Price(d, amount))
|
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(
|
return dataclasses.replace(
|
||||||
series, base=output_base, quote=output_quote, prices=prices
|
series, base=output_base, quote=output_quote, prices=prices
|
||||||
|
@ -66,12 +66,12 @@ class CoinMarketCap(BaseSource):
|
||||||
|
|
||||||
params = {}
|
params = {}
|
||||||
|
|
||||||
if series.base.startswith("id="):
|
if series.base.startswith("ID="):
|
||||||
params["id"] = series.base[3:]
|
params["id"] = series.base[3:]
|
||||||
else:
|
else:
|
||||||
params["symbol"] = series.base
|
params["symbol"] = series.base
|
||||||
|
|
||||||
if series.quote.startswith("id="):
|
if series.quote.startswith("ID="):
|
||||||
params["convert_id"] = series.quote[3:]
|
params["convert_id"] = series.quote[3:]
|
||||||
else:
|
else:
|
||||||
params["convert"] = series.quote
|
params["convert"] = series.quote
|
||||||
|
@ -95,12 +95,17 @@ class CoinMarketCap(BaseSource):
|
||||||
else:
|
else:
|
||||||
return Decimal(str(data[type]))
|
return Decimal(str(data[type]))
|
||||||
|
|
||||||
def _output_pair(self, base, quote):
|
def _output_pair(self, base, quote, data):
|
||||||
if base.startswith("id=") or quote.startswith("id="):
|
data_base = data["data"]["symbol"]
|
||||||
symbols = {i["id"]: (i["symbol"] or i["code"]) for i in self._symbol_data()}
|
data_quote = next(iter(data["data"]["quotes"][0]["quote"].keys()))
|
||||||
|
|
||||||
output_base = symbols[int(base[3:])] if base.startswith("id=") else base
|
lookup_quote = False
|
||||||
output_quote = symbols[int(quote[3:])] if quote.startswith("id=") else quote
|
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)
|
return (output_base, output_quote)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue