Home > Back-end >  How to update crypto price live through API
How to update crypto price live through API

Time:12-29

I wrote this code which brings Bitcoin's price from CoinMarketCap through its API.
Then it prints the price in the terminal and also sends it to Arduino so I can see it on an LCD Display live (thanks to Firmata library)
It works fine but the problem is that the price does not change, it prints always the same price of when I start the code.

from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import time
from pyfirmata import Arduino, util, STRING_DATA


port = 'COM3'

board = Arduino(port)


url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
  'start':'1',
  'limit':'1',
  'convert':'USD'
}
headers = {
  'Accepts': 'application/json',
  'X-CMC_PRO_API_KEY': '646f5705-1a58-48f4-af8a-7156f90d6885',
}

session = Session()
session.headers.update(headers)

try:
  response = session.get(url, params=parameters)
  data = json.loads(response.text)


  for entry in data["data"]:
      symbol = entry["symbol"]
      price1 = str(entry["quote"]["USD"]["price"])[:5]

  while(True):    
    print(symbol   ':', price1)
    board.send_sysex(STRING_DATA, util.str_to_two_byte_iter(symbol))
    board.send_sysex(STRING_DATA, util.str_to_two_byte_iter(price1))
    time.sleep(30)
except (ConnectionError, Timeout, TooManyRedirects) as e:
  print(e)

So, let's say that I run the program and at that exact moment the Bitcoin's price is 47000 dollars.

The output is something like this:

BTC: 47000
BTC: 47000
BTC: 47000
BTC: 47000
BTC: 47000

CodePudding user response:

You should move this part:

  response = session.get(url, params=parameters)
  data = json.loads(response.text)


  for entry in data["data"]:
      symbol = entry["symbol"]
      price1 = str(entry["quote"]["USD"]["price"])[:5]

into your while loop, right now you are only fetching the data once from the API and then printing/sending it forever.

It will then look like this:

from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import time
from pyfirmata import Arduino, util, STRING_DATA


port = 'COM3'

board = Arduino(port)


url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
  'start':'1',
  'limit':'1',
  'convert':'USD'
}
headers = {
  'Accepts': 'application/json',
  'X-CMC_PRO_API_KEY': '646f5705-1a58-48f4-af8a-7156f90d6885',
}

session = Session()
session.headers.update(headers)

try:
    while(True):
        response = session.get(url, params=parameters)
        data = json.loads(response.text)

        for entry in data["data"]:
            symbol = entry["symbol"]
            price1 = str(entry["quote"]["USD"]["price"])[:5]

        print(symbol   ':', price1)
        board.send_sysex(STRING_DATA, util.str_to_two_byte_iter(symbol))
        board.send_sysex(STRING_DATA, util.str_to_two_byte_iter(price1))
        time.sleep(30)
    except (ConnectionError, Timeout, TooManyRedirects) as e:
    print(e)
  • Related