-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.py
More file actions
66 lines (57 loc) · 1.56 KB
/
Copy pathcurrency.py
File metadata and controls
66 lines (57 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from dotenv import load_dotenv
import requests
import json
from decimal import Decimal
import asyncio
load_dotenv()
CURRENCIES = [
"USD",
"EUR",
"JPY",
"GBP",
"CHF",
"CAD",
"AUD",
"ZAR",
"SYP",
"NGN",
"EGP",
"QAR",
"IRR",
"DZD",
"BSD",
"AOA",
"ALL"
]
API_URL = f'https://v6.exchangerate-api.com/v6/{os.environ["EXCHANGE_RATE_API_KEY"]}/latest/'
JSON_FILE = 'exchange_rates/exchange_rates_'
async def fetch_exchange_rates():
try:
for cur in CURRENCIES:
response = requests.get(API_URL+f"{cur.strip().upper()}")
response.raise_for_status()
data = response.json()
with open(JSON_FILE+f"{cur.strip().upper()}.json", 'w') as file:
json.dump(data['conversion_rates'], file)
await asyncio.sleep(5)
except requests.exceptions.RequestException as e:
print(f"Error fetching exchange rates: {e}")
return {}
async def read_exchange_rates(cur):
try:
with open(JSON_FILE+f"{cur.strip().upper()}.json", 'r') as file:
return json.load(file)
except FileNotFoundError as e:
print(f"Error reading exchange rates for {cur}: {e}")
return {}
async def convert_currency(amount, from_currency, to_currency):
rates = await read_exchange_rates(from_currency)
if from_currency == to_currency:
return amount
try:
rate = rates[to_currency] / rates[from_currency]
return amount * Decimal(rate)
except KeyError:
print(f"Conversion rate for {from_currency} to {to_currency} not found.")
return amount