Skip to content

Instantly share code, notes, and snippets.

@PiotrCzapla
Last active March 16, 2022 11:42
Show Gist options
  • Select an option

  • Save PiotrCzapla/074d8e73c6c12a66869918187e49b84d to your computer and use it in GitHub Desktop.

Select an option

Save PiotrCzapla/074d8e73c6c12a66869918187e49b84d to your computer and use it in GitHub Desktop.
Get NBP currency exchange rate in python with memory cache, this can act as simple python API :)
# 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']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment