Skip to content

Instantly share code, notes, and snippets.

@nrobinson2000
Last active December 31, 2015 15:28
Show Gist options
  • Save nrobinson2000/b915d417115b64ea032f to your computer and use it in GitHub Desktop.
Save nrobinson2000/b915d417115b64ea032f to your computer and use it in GitHub Desktop.
bitcoin_price.py
#!/usr/bin/env python
#Bitcoin price Monitor by Nathan Robinson. GPL3 2015
import urllib2
import time
import os
refresh_rate = 5
def isInt(s):
try:
int(s)
return True
except ValueError:
return False
def turnFloat(x):
result = ""
for i in range(len(x)):
if isInt(x[i]) and i > 5:
result = result + x[i]
if x[i] == ".":
result = result + x[i]
return float(result)
def main():
while True:
data = urllib2.urlopen("https://api.bitcoinaverage.com/ticker/global/USD/")
text = data.readlines()
avg_data = text.__getitem__(1)
avg = turnFloat(avg_data)
ask_data = text.__getitem__(2)
ask = turnFloat(ask_data)
bid_data = text.__getitem__(3)
bid = turnFloat(bid_data)
last_data = text.__getitem__(4)
last = turnFloat(last_data)
os.system("clear")
print("[ Bitcoin Price Monitor ]")
print
labels = ['Average', 'Ask', 'Bid', 'Last']
values = [str(avg), str(ask), str(bid), str(last)]
for i in xrange(0, len(labels), 4):
print '\t'.join(labels[i:i+4])
for i in xrange(0, len(values), 4):
print '\t'.join(values[i:i+4])
data.close()
time.sleep(refresh_rate)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment