Skip to content

Commit 3a6d190

Browse files
committed
p4 bitcoin api request json sys
1 parent 961685d commit 3a6d190

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

problemSet4/bitcoin/bitcoin.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import sys
2+
import requests
3+
import json
4+
5+
def main():
6+
if len(sys.argv) == 2:
7+
try:
8+
n = float(sys.argv[1])
9+
except ValueError:
10+
sys.exit("Command-line argument is not a number")
11+
else:
12+
url = "https://api.coindesk.com/v1/bpi/currentprice.json"
13+
cost = get_cost(n, url)
14+
print(f"${cost:,.4f}")
15+
else:
16+
sys.exit("Missing command-line argument")
17+
18+
19+
def get_cost(n, url):
20+
try:
21+
res = requests.get(url)
22+
res.raise_for_status()
23+
except requests.exceptions.HTTPError as e:
24+
print('Http Error:', e)
25+
except requests.RequestException as e:
26+
print("RequestException:", e)
27+
except:
28+
print("Error")
29+
else:
30+
try:
31+
data = (res.json())
32+
except requests.exceptions.JSONDecodeError:
33+
sys.exit("JSONDecodeError")
34+
# print(json.dumps(data, indent=2))
35+
cost = data["bpi"]["USD"]["rate_float"] * n
36+
return cost
37+
38+
39+
40+
if __name__ == "__main__":
41+
main()
42+
43+
44+
45+
46+
# {
47+
# "time": {
48+
# "updated": "Dec 23, 2023 13:07:00 UTC",
49+
# "updatedISO": "2023-12-23T13:07:00+00:00",
50+
# "updateduk": "Dec 23, 2023 at 13:07 GMT"
51+
# },
52+
# "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
53+
# "chartName": "Bitcoin",
54+
# "bpi": {
55+
# "USD": {
56+
# "code": "USD",
57+
# "symbol": "$",
58+
# "rate": "43,752.8579",
59+
# "description": "United States Dollar",
60+
# "rate_float": 43752.8579
61+
# },
62+
# "GBP": {
63+
# "code": "GBP",
64+
# "symbol": "£",
65+
# "rate": "36,559.5381",
66+
# "description": "British Pound Sterling",
67+
# "rate_float": 36559.5381
68+
# },
69+
# "EUR": {
70+
# "code": "EUR",
71+
# "symbol": "€",
72+
# "rate": "42,621.6715",
73+
# "description": "Euro",
74+
# "rate_float": 42621.6715
75+
# }
76+
# }
77+
# }
78+
79+
80+
81+
"""
82+
{
83+
"time": {
84+
"updated": "Dec 23, 2023 13:07:00 UTC",
85+
"updatedISO": "2023-12-23T13:07:00+00:00",
86+
"updateduk": "Dec 23, 2023 at 13:07 GMT"
87+
},
88+
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
89+
"chartName": "Bitcoin",
90+
"bpi": {
91+
"USD": {
92+
"code": "USD",
93+
"symbol": "$",
94+
"rate": "43,752.8579",
95+
"description": "United States Dollar",
96+
"rate_float": 43752.8579
97+
},
98+
"GBP": {
99+
"code": "GBP",
100+
"symbol": "£",
101+
"rate": "36,559.5381",
102+
"description": "British Pound Sterling",
103+
"rate_float": 36559.5381
104+
},
105+
"EUR": {
106+
"code": "EUR",
107+
"symbol": "€",
108+
"rate": "42,621.6715",
109+
"description": "Euro",
110+
"rate_float": 42621.6715
111+
}
112+
}
113+
}
114+
"""

0 commit comments

Comments
 (0)