Advertisement
steve-shambles-2109

Bitcion and Ethereum price ticker

Oct 22nd, 2019
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. """
  2. Python code snippets vol 36:
  3. 178-Bitcion and Ethereum price ticker.
  4. stevepython.wordpress.com
  5.  
  6. Reports price of BTC or ETH, if changed, every 60 seconds in the shell.
  7.  
  8. source:
  9. https://gist.github.com/Dogeek/a09e9d29ee301aa58e408e6eeba6d426
  10. """
  11. import time
  12. import requests
  13.  
  14. api_url_formatter = "https://api.coinmarketcap.com/v1/ticker/{}"
  15. api_names = {
  16.     "btc": "bitcoin",
  17.     "eth": "ethereum",
  18. }
  19.  
  20.  
  21. def get_latest_price(currency_code):
  22.     """Retrive latest price of selected crpto currency."""
  23.     response = requests.get(api_url_formatter.format(api_names[currency_code.lower()]))
  24.     if response.status_code == 200:
  25.         return response.json()[0]['price_usd']
  26.     else:
  27.         raise Exception("Response didn't return a 200 status code.")
  28.  
  29.  
  30. usr_input = None
  31. while usr_input not in ("eth", "btc"):
  32.     usr_input = input("Enter ETH/BTC : ").lower()
  33.  
  34. last_price = -1
  35.  
  36. while True:
  37.     price = get_latest_price(usr_input)
  38.     if price != last_price:
  39.         print(f"Price : {price}")
  40.         last_price = price
  41.     time.sleep(60)  # sleeps for a minute
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement