Skip to content

Instantly share code, notes, and snippets.

@marcoscastro
Created February 15, 2017 09:53
Show Gist options
  • Select an option

  • Save marcoscastro/737d18df025f3c1cbadc5d03ca00bf7a to your computer and use it in GitHub Desktop.

Select an option

Save marcoscastro/737d18df025f3c1cbadc5d03ca00bf7a to your computer and use it in GitHub Desktop.

Revisions

  1. marcoscastro created this gist Feb 15, 2017.
    38 changes: 38 additions & 0 deletions btc_usd.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    '''
    Get Bitcoin Value
    http://api.coindesk.com/v1/bpi/currentprice.json
    '''

    import urllib.request, json, time

    def obter_valor():
    try:
    url = "http://api.coindesk.com/v1/bpi/currentprice.json"
    with urllib.request.urlopen(url) as url:
    response = url.read()
    data = json.loads(response.decode('utf-8'))
    valor = float(data['bpi']['USD']['rate'].replace(",", ""))
    return valor
    except urllib.error.HTTPError:
    print('URL inexistente!')

    def exibir_valores(tempo=1):
    valor = obter_valor()
    nova_cotacao = True
    print('1 Bitcoin vale %f dólares!' % valor)
    while True:
    valor_atual = obter_valor()
    if valor_atual < valor:
    print('---> Preço do Bitcoin caindo: 1 Bitcoin vale %f dólares!' % valor_atual)
    nova_cotacao = True
    elif valor_atual > valor:
    print('---> Preço do Bitcoin subindo: 1 Bitcoin vale %f dólares!' % valor_atual)
    nova_cotacao = True
    else:
    if nova_cotacao == True:
    print('Aguardando uma nova cotação...')
    nova_cotacao = False
    valor = valor_atual
    time.sleep(tempo)

    exibir_valores()