-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin.py
More file actions
47 lines (29 loc) · 968 Bytes
/
Copy pathbitcoin.py
File metadata and controls
47 lines (29 loc) · 968 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests, sys
URL = f"https://rest.coincap.io/v3/assets/bitcoin?apiKey=7e3906063c4ba25d6d7b3bb7403ac02ca7ed810facec9d7e8a9e3b8c1e8e6fa2"
def main():
validate_arguments(sys.argv)
bitcoins = get_bitcoins_float(sys.argv[1])
content = get_response()
usd = get_usd(content) * bitcoins
print(f"${usd:,.4f}")
def validate_arguments(arguments):
if len(arguments) != 2:
sys.exit("Missing command-line argument")
def get_bitcoins_float(bitcoins):
try:
return float(bitcoins)
except ValueError:
sys.exit("Command-line argument is not a number")
def get_response():
try:
response = requests.get(URL)
except requests.RequestException as e:
sys.exit(e)
return response.json()
def get_usd(content):
try:
return float(content.get("data").get("priceUsd"))
except ValueError:
sys.exit("Invalid usd price response")
if __name__ == "__main__":
main()