Queer European MD passionate about IT

bitcoin.py 596 B

123456789101112131415161718192021222324
  1. import json
  2. import requests
  3. import sys
  4. def main():
  5. try:
  6. n = float(sys.argv[1])
  7. except IndexError:
  8. sys.exit("Missing command-line argument")
  9. except (ValueError, TypeError):
  10. sys.exit("Command-line argument is not a number")
  11. try:
  12. response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
  13. data = response.json()
  14. conversion_rate = float(data['bpi']['USD']['rate'].replace(',', ''))
  15. except requests.RequestException:
  16. pass
  17. print(f"${n * conversion_rate:,.4f}")
  18. if __name__ == "__main__":
  19. main()