Add --symbols option, implement for ECB.
This commit is contained in:
parent
89cf36c357
commit
0b2b076d61
2 changed files with 40 additions and 65 deletions
|
@ -50,10 +50,15 @@ def cmd_source(args):
|
||||||
first_output = wrapper.wrap(first)
|
first_output = wrapper.wrap(first)
|
||||||
wrapper.initial_indent = subsequent_indent
|
wrapper.initial_indent = subsequent_indent
|
||||||
rest_output = sum([wrapper.wrap(line) for line in rest], [])
|
rest_output = sum([wrapper.wrap(line) for line in rest], [])
|
||||||
# TODO use str.splitlines()?
|
output = "\n".join(first_output + rest_output)
|
||||||
print("\n".join(first_output + rest_output))
|
if output != "":
|
||||||
|
print(output)
|
||||||
|
|
||||||
source = sources.by_id[args.identifier]
|
source = sources.by_id[args.identifier]()
|
||||||
|
|
||||||
|
if args.symbols:
|
||||||
|
print("\n".join(source.symbols()))
|
||||||
|
else:
|
||||||
key_width = 11
|
key_width = 11
|
||||||
output_width = shutil.get_terminal_size().columns
|
output_width = shutil.get_terminal_size().columns
|
||||||
|
|
||||||
|
@ -62,8 +67,6 @@ def cmd_source(args):
|
||||||
print_field("Description", source.description(), key_width, output_width)
|
print_field("Description", source.description(), key_width, output_width)
|
||||||
print_field("URL", source.source_url(), key_width, output_width, force=False)
|
print_field("URL", source.source_url(), key_width, output_width, force=False)
|
||||||
print_field("Notes", source.notes(), key_width, output_width)
|
print_field("Notes", source.notes(), key_width, output_width)
|
||||||
print_field("Bases", ", ".join(source.bases()), key_width, output_width)
|
|
||||||
print_field("Quotes", ", ".join(source.quotes()), key_width, output_width)
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_fetch(args):
|
def cmd_fetch(args):
|
||||||
|
@ -157,6 +160,7 @@ def build_parser():
|
||||||
source_parser = subparsers.add_parser(
|
source_parser = subparsers.add_parser(
|
||||||
"source",
|
"source",
|
||||||
help="show source details",
|
help="show source details",
|
||||||
|
usage="pricehist source SOURCE [-h] [-s]",
|
||||||
formatter_class=formatter,
|
formatter_class=formatter,
|
||||||
)
|
)
|
||||||
source_parser.add_argument(
|
source_parser.add_argument(
|
||||||
|
@ -166,6 +170,12 @@ def build_parser():
|
||||||
choices=sources.by_id.keys(),
|
choices=sources.by_id.keys(),
|
||||||
help="the source identifier",
|
help="the source identifier",
|
||||||
)
|
)
|
||||||
|
source_parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--symbols",
|
||||||
|
action="store_true",
|
||||||
|
help="list available symbols",
|
||||||
|
)
|
||||||
|
|
||||||
fetch_parser = subparsers.add_parser(
|
fetch_parser = subparsers.add_parser(
|
||||||
"fetch",
|
"fetch",
|
||||||
|
|
|
@ -28,68 +28,23 @@ class ECB:
|
||||||
def notes():
|
def notes():
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@staticmethod
|
def symbols(self):
|
||||||
def bases():
|
data = self._raw_data(more_than_90_days=True)
|
||||||
return ["EUR"]
|
root = etree.fromstring(data)
|
||||||
|
nodes = root.cssselect("[currency]")
|
||||||
@staticmethod
|
currencies = sorted(set([n.attrib["currency"] for n in nodes]))
|
||||||
def quotes():
|
pairs = [f"EUR/{c}" for c in currencies]
|
||||||
return [
|
return pairs
|
||||||
"AUD",
|
|
||||||
"BGN",
|
|
||||||
"BRL",
|
|
||||||
"CAD",
|
|
||||||
"CHF",
|
|
||||||
"CNY",
|
|
||||||
"CZK",
|
|
||||||
"DKK",
|
|
||||||
"GBP",
|
|
||||||
"HKD",
|
|
||||||
"HRK",
|
|
||||||
"HUF",
|
|
||||||
"IDR",
|
|
||||||
"ILS",
|
|
||||||
"INR",
|
|
||||||
"ISK",
|
|
||||||
"JPY",
|
|
||||||
"KRW",
|
|
||||||
"MXN",
|
|
||||||
"MYR",
|
|
||||||
"NOK",
|
|
||||||
"NZD",
|
|
||||||
"PHP",
|
|
||||||
"PLN",
|
|
||||||
"RON",
|
|
||||||
"RUB",
|
|
||||||
"SEK",
|
|
||||||
"SGD",
|
|
||||||
"THB",
|
|
||||||
"TRY",
|
|
||||||
"USD",
|
|
||||||
"ZAR",
|
|
||||||
]
|
|
||||||
|
|
||||||
def fetch(self, pair, type, start, end):
|
def fetch(self, pair, type, start, end):
|
||||||
base, quote = pair.split("/")
|
base, quote = pair.split("/")
|
||||||
if base not in self.bases():
|
|
||||||
exit(f"Invalid base {base}")
|
|
||||||
if quote not in self.quotes():
|
|
||||||
exit(f"Invalid quote {quote}")
|
|
||||||
|
|
||||||
min_start = "1999-01-04"
|
min_start = "1999-01-04"
|
||||||
if start < min_start:
|
if start < min_start:
|
||||||
exit(f"start {start} too early. Minimum is {min_start}")
|
exit(f"start {start} too early. Minimum is {min_start}")
|
||||||
|
|
||||||
almost_90_days_ago = str(datetime.now().date() - timedelta(days=85))
|
almost_90_days_ago = str(datetime.now().date() - timedelta(days=85))
|
||||||
url_base = "https://www.ecb.europa.eu/stats/eurofxref"
|
data = self._raw_data(start > almost_90_days_ago)
|
||||||
if start > almost_90_days_ago:
|
|
||||||
source_url = f"{url_base}/eurofxref-hist-90d.xml" # last 90 days
|
|
||||||
else:
|
|
||||||
source_url = f"{url_base}/eurofxref-hist.xml" # since 1999
|
|
||||||
|
|
||||||
response = requests.get(source_url)
|
|
||||||
data = response.content
|
|
||||||
|
|
||||||
root = etree.fromstring(data)
|
root = etree.fromstring(data)
|
||||||
|
|
||||||
all_rows = []
|
all_rows = []
|
||||||
|
@ -105,3 +60,13 @@ class ECB:
|
||||||
]
|
]
|
||||||
|
|
||||||
return selected
|
return selected
|
||||||
|
|
||||||
|
def _raw_data(self, more_than_90_days=False):
|
||||||
|
url_base = "https://www.ecb.europa.eu/stats/eurofxref"
|
||||||
|
if more_than_90_days:
|
||||||
|
source_url = f"{url_base}/eurofxref-hist.xml" # since 1999
|
||||||
|
else:
|
||||||
|
source_url = f"{url_base}/eurofxref-hist-90d.xml" # last 90 days
|
||||||
|
|
||||||
|
response = requests.get(source_url)
|
||||||
|
return response.content
|
||||||
|
|
Loading…
Add table
Reference in a new issue