Last active
June 6, 2019 17:27
-
-
Save lubomir/69781a03509568ac92f8a2ff42819da5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
import requests | |
def parse_rates(data): | |
header, names, *lines = data.splitlines() | |
rates = {} | |
for line in lines: | |
_, _, amount, currency, value = line.replace(",", ".").split("|") | |
rates[currency] = float(value) / float(amount) | |
return rates | |
def get_exchange_rates(date=None): | |
query = {} | |
if date: | |
query["date"] = date | |
response = requests.get("http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt", params=query) | |
return parse_rates(response.text) | |
@click.command() | |
@click.option("--date") | |
@click.option("--to", multiple=True) | |
@click.argument("amount", type=click.FLOAT) | |
def cnb(amount, date=None, to=None): | |
rates = get_exchange_rates(date) | |
for currency in sorted(rates): | |
if not to or currency in to: | |
converted = amount / rates[currency] | |
click.echo(f"{amount} CZK = {converted} {currency}") | |
if __name__ == "__main__": | |
cnb() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment