Created
March 6, 2017 19:27
-
-
Save dulek/e1bf72cd818f9257e162e61d816677d7 to your computer and use it in GitHub Desktop.
Gets you average NBP rate for given date and currency.
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
#!/usr/bin/env python | |
import argparse | |
from datetime import datetime | |
import json | |
import requests | |
def date_type(s): | |
try: | |
return datetime.strptime(s, "%d-%m-%Y") | |
except ValueError: | |
msg = "Not a valid date: '{0}'.".format(s) | |
raise argparse.ArgumentTypeError(msg) | |
parser = argparse.ArgumentParser(description='Gets average NBP rate for a ' | |
'given day.') | |
parser.add_argument('-d', '--date', | |
help="Date to get the rate - format DD-MM-YYYY. Today by " | |
"default.", | |
type=date_type) | |
parser.add_argument('-c', '--currency', | |
help="Currency code (ISO 4217). USD by default") | |
args = parser.parse_args() | |
d = args.date | |
c = args.currency | |
d = d or datetime.now() | |
c = c or 'USD' | |
url = ('http://api.nbp.pl/api/exchangerates/rates/a/%(c)s/%(d)s/?format=json' | |
% {'c': c, 'd': d.strftime('%Y-%m-%d')}) | |
response = requests.get(url) | |
print response.json()['rates'][0]['mid'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment