-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_exchange_rate.py
More file actions
27 lines (23 loc) · 922 Bytes
/
update_exchange_rate.py
File metadata and controls
27 lines (23 loc) · 922 Bytes
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
import os
import requests
import json
def fetch_exchange_rate(api_key):
url = f'https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=CNY&apikey={api_key}'
response = requests.get(url)
data = response.json()
if 'Realtime Currency Exchange Rate' in data:
return {
"exchangeRate": data['Realtime Currency Exchange Rate']['5. Exchange Rate'],
"lastRefreshed": data['Realtime Currency Exchange Rate']['6. Last Refreshed']
}
else:
raise Exception("Error fetching exchange rate")
def main():
api_key = os.getenv('ALPHA_VANTAGE_API_KEY')
if not api_key:
raise Exception("No API key found in environment variables")
exchange_rate = fetch_exchange_rate(api_key)
with open('exchange_rate.json', 'w') as f:
json.dump(exchange_rate, f, indent=4)
if __name__ == '__main__':
main()