Skip to content

Instantly share code, notes, and snippets.

@Natgho
Last active May 22, 2024 17:23
Show Gist options
  • Save Natgho/b45413315e0e648ead120f129c935fb3 to your computer and use it in GitHub Desktop.
Save Natgho/b45413315e0e648ead120f129c935fb3 to your computer and use it in GitHub Desktop.
cbar.az currency converter python
# Sezer BOZKIR - 2024 - sezerbozkir.com
# firstly install xmltodict library
# pip install xmltodict
import requests
import xmltodict
from datetime import datetime
def convert_currency(expected_date=datetime.now().strftime("%d.%m.%Y")):
url = f"https://cbar.az/currencies/{expected_date}.xml"
response = requests.get(url)
data = xmltodict.parse(response.content)
serialized_data = [*data['ValCurs']['ValType'][0]['Valute'], *data['ValCurs']['ValType'][1]['Valute']]
currencies = {x["@Code"]: (float(x["Value"]) if x["@Code"] != "JPY" else float(x["Value"]) / 100) for x in
serialized_data}
currency_names = [val['@Code'] for val in serialized_data]
# print currency codes with split in 3 lines
print("Currency Codes: ")
for i in range(0, len(currency_names), 10):
print(" ".join(currency_names[i:i + 10]))
amount = float(input("Enter the Ammount: "))
from_currency = input("Enter from Currency Name: ")
to_currency = input("Enter to Currency Name: ")
result = amount * currencies[from_currency] / currencies[to_currency]
return result
def cur():
expected_date = input("Please enter the date in the format of dd.mm.yyyy: (default: today just enter)")
cc = convert_currency(expected_date) if expected_date else convert_currency()
print(cc)
if __name__ == '__main__':
cur()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment