sources and symbols listing has a formatting helper.
This commit is contained in:
parent
a609a2b8c3
commit
96970f736c
5 changed files with 15 additions and 17 deletions
|
@ -41,13 +41,14 @@ def cli(args=None, output_file=sys.stdout):
|
||||||
logging.debug(f"Finished pricehist run at {datetime.now()}.")
|
logging.debug(f"Finished pricehist run at {datetime.now()}.")
|
||||||
|
|
||||||
|
|
||||||
|
def _format_pairs(pairs, gap=4):
|
||||||
|
width = max([len(a) for a, b in pairs])
|
||||||
|
lines = [a.ljust(width + gap) + b for a, b in pairs]
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def cmd_sources(args):
|
def cmd_sources(args):
|
||||||
width = max([len(identifier) for identifier in sources.by_id.keys()])
|
return _format_pairs([(s.id(), s.name()) for k, s in sorted(sources.by_id.items())])
|
||||||
source_lines = [
|
|
||||||
f"{identifier.ljust(width)} {source.name()}"
|
|
||||||
for identifier, source in sources.by_id.items()
|
|
||||||
]
|
|
||||||
return "\n".join(source_lines)
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_source(args):
|
def cmd_source(args):
|
||||||
|
@ -75,7 +76,7 @@ def cmd_source(args):
|
||||||
source = sources.by_id[args.identifier]
|
source = sources.by_id[args.identifier]
|
||||||
|
|
||||||
if args.symbols:
|
if args.symbols:
|
||||||
return "\n".join(source.symbols())
|
return _format_pairs(source.symbols())
|
||||||
else:
|
else:
|
||||||
k_width = 11
|
k_width = 11
|
||||||
total_width = shutil.get_terminal_size().columns
|
total_width = shutil.get_terminal_size().columns
|
||||||
|
|
|
@ -36,7 +36,7 @@ class BaseSource(ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def symbols(self) -> list[str]:
|
def symbols(self) -> list[(str, str)]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|
|
@ -39,10 +39,10 @@ class CoinDesk(BaseSource):
|
||||||
response = self.log_curl(requests.get(url))
|
response = self.log_curl(requests.get(url))
|
||||||
data = json.loads(response.content)
|
data = json.loads(response.content)
|
||||||
relevant = [i for i in data if i["currency"] not in ["XBT", "BTC"]]
|
relevant = [i for i in data if i["currency"] not in ["XBT", "BTC"]]
|
||||||
symbols = sorted(
|
return [
|
||||||
[f"BTC/{i['currency']} Bitcoin against {i['country']}" for i in relevant]
|
(f"BTC/{i['currency']}", f"Bitcoin against {i['country']}")
|
||||||
)
|
for i in sorted(relevant, key=lambda i: i["currency"])
|
||||||
return symbols
|
]
|
||||||
|
|
||||||
def fetch(self, series):
|
def fetch(self, series):
|
||||||
data = self._data(series)
|
data = self._data(series)
|
||||||
|
|
|
@ -43,10 +43,8 @@ class CoinMarketCap(BaseSource):
|
||||||
def symbols(self):
|
def symbols(self):
|
||||||
data = self._symbol_data()
|
data = self._symbol_data()
|
||||||
ids = [f"id={i['id']}" for i in data]
|
ids = [f"id={i['id']}" for i in data]
|
||||||
id_width = max([len(id) for id in ids])
|
|
||||||
descriptions = [f"{i['symbol'] or i['code']} {i['name']}".strip() for i in data]
|
descriptions = [f"{i['symbol'] or i['code']} {i['name']}".strip() for i in data]
|
||||||
rows = [i.ljust(id_width + 4) + d for i, d in zip(ids, descriptions)]
|
return list(zip(ids, descriptions))
|
||||||
return rows
|
|
||||||
|
|
||||||
def fetch(self, series):
|
def fetch(self, series):
|
||||||
data = self._data(series)
|
data = self._data(series)
|
||||||
|
|
|
@ -38,8 +38,7 @@ class ECB(BaseSource):
|
||||||
nodes = root.cssselect("[currency]")
|
nodes = root.cssselect("[currency]")
|
||||||
currencies = sorted(set([n.attrib["currency"] for n in nodes]))
|
currencies = sorted(set([n.attrib["currency"] for n in nodes]))
|
||||||
iso = isocurrencies.by_code()
|
iso = isocurrencies.by_code()
|
||||||
pairs = [f"EUR/{c} Euro against {iso[c].name}" for c in currencies]
|
return [(f"EUR/{c}", f"Euro against {iso[c].name}") for c in currencies]
|
||||||
return pairs
|
|
||||||
|
|
||||||
def fetch(self, series):
|
def fetch(self, series):
|
||||||
almost_90_days_ago = str(datetime.now().date() - timedelta(days=85))
|
almost_90_days_ago = str(datetime.now().date() - timedelta(days=85))
|
||||||
|
|
Loading…
Add table
Reference in a new issue