-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_api_handler.py
executable file
·39 lines (32 loc) · 1.04 KB
/
currency_api_handler.py
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
#!/usr/bin/env python3
# this is just genious
# https://github.com/fawazahmed0/currency-api
import requests
base_urls_arr = []
base_urls_arr.append('https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/')
base_urls_arr.append('https://raw.githubusercontent.com/fawazahmed0/currency-api/1/')
suffix_arr = []
suffix_arr.append('.min.json')
suffix_arr.append('.json')
def get_rate(curr1, curr2, date):
curr1 = curr1.lower()
curr2 = curr2.lower()
for base_url in base_urls_arr:
for suff in suffix_arr:
url = base_url + date + '/currencies/' + curr1 + '/' + curr2 + suff
rate = requests.get(url)
if rate.status_code == 200:
break
if rate.status_code == 200:
break
if rate.status_code != 200:
return 'Not found'
return rate.json()[curr2]
def get_today_rate(curr1, curr2):
return get_rate(curr1, curr2, 'latest')
# if __name__ == '__main__':
# # provoking error
# print(get_today_rate('aff', 'usd'))
# # normal usage
# print(get_rate('eur', 'usd', '2022-03-21'))
# print(get_today_rate('eur', 'usd'))