Last active
March 16, 2022 11:42
-
-
Save PiotrCzapla/074d8e73c6c12a66869918187e49b84d to your computer and use it in GitHub Desktop.
Revisions
-
PiotrCzapla revised this gist
Mar 16, 2022 . 1 changed file with 6 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,17 @@ # license: mit import requests from functools import lru_cache @lru_cache(maxsize=None) def get_nbp_exchange_rate(date:datetime, table='A'): """ Get the exchange rate from the NBP API, and store it in cache folder >> get_nbp_exchange_rate(datetime(2022,3,8))['EUR'] 4.9121 """ url = 'http://api.nbp.pl/api/exchangerates/tables/{:s}/{:%Y-%m-%d}/?format=json'.format(table, date) r = requests.get(url) if r.status_code != 200: raise RuntimeError("Unable to fetch exchange rate from NBP API") return {rate['code']:rate['mid'] for rate in r.json()[0]['rates']} -
PiotrCzapla created this gist
Mar 16, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ # Code mostly written by github copilot with few modifications # license: mit import requests import pandas as pd from functools import lru_cache from datetime import datetime @lru_cache(maxsize=None) def get_nbp_exchange_rate(date:datetime, table='A'): """ Get the exchange rate from the NBP API, and store it in cache folder """ url = 'http://api.nbp.pl/api/exchangerates/tables/{:s}/{:%Y-%m-%d}/?format=json'.format(table, date) r = requests.get(url) df = pd.DataFrame(r.json()[0]['rates']).set_index('code') return df get_nbp_exchange_rate(datetime.now())['mid']['EUR']