-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.py
More file actions
24 lines (17 loc) · 748 Bytes
/
currency.py
File metadata and controls
24 lines (17 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'''
You’ve probably looked up conversion rates using your browser, but here’s how to automate the task of finding the current exchange rate between any two currencies.
'''
import json
import sys
import urllib.request
if len(sys.argv) != 3:
print("Usage: ./currencyrates.py lookup_currency base_currency. Example: ./currencyrates.py cad usd")
sys.exit()
currency = sys.argv[1]
basecurrency = sys.argv[2]
currencyurl = "http://freecurrencyrates.com/api/action.php?do=cvals&iso=" + currency + "&f=" + basecurrency + "&v=1&s=cbr"
f = urllib.request.urlopen(currencyurl)
obj = json.loads(f.read())
result = "1 " + currency.upper() + " is "
result+="{:,.2f}".format(1/obj[currency.upper()]) + " " + basecurrency.upper()
print(result);