Skip to content

Instantly share code, notes, and snippets.

@paladin-dranser
Created December 5, 2021 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paladin-dranser/658d5fbc8fbf07867cf35103932c1438 to your computer and use it in GitHub Desktop.
Save paladin-dranser/658d5fbc8fbf07867cf35103932c1438 to your computer and use it in GitHub Desktop.
Get official NBRB currency rata based on ISO 4217 currency code (1 <currency_code> = <N> BYN)
#!/usr/bin/python3
"""
Get official NBRB currency rata based on ISO 4217 currency code (1 <currency_code> = <N> BYN)
API Documentation: https://www.nbrb.by/bel/apihelp/exrates
"""
import argparse
import requests
def get_currency_rate(base_url: str, currency_iso_code: str, periodicity: int = 0) -> str:
'''
Get currency rate by ISO 4217 currency code
Parameters:
base_url (str): URL to do request
currency_iso_code (str): ISO 4217 currency code for which get rate
periodicity (int): periodicity of rate setting (0 - every day, 1 - every month)
'''
url = f'{base_url}/{currency_iso_code}'
params = {
'periodicity': periodicity,
'parammode': 2,
}
response = requests.get(url, params=params)
data = response.json()
return data['Cur_OfficialRate'] / data['Cur_Scale']
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Get currency rate based on ISO 4217')
parser.add_argument('currency_iso_code', metavar='XXX', type=str,
help='ISO 4217 Currency Code')
args = parser.parse_args()
BASE_URL = 'https://www.nbrb.by/api/exrates/rates'
rate = get_currency_rate(BASE_URL, args.currency_iso_code)
print(rate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment