Skip to content

Instantly share code, notes, and snippets.

@vindolin
Last active December 16, 2015 03:29
Show Gist options
  • Save vindolin/5370386 to your computer and use it in GitHub Desktop.
Save vindolin/5370386 to your computer and use it in GitHub Desktop.
Rewrite of https://gist.github.com/vindolin/5353600, now uses a websocket instead of polling. Needs the socketIO_client module. Install: pip install socketIO_client Basic usage: python mtgox_price_ws.py 33 where 33 is the amount of bitcoins you own Donations -> 1HpQQds9wUFykgwKyzXp747SAb6374RjUL
# -*- coding: utf-8 -*-
# needs socketIO_client (pip install socketIO_client)
# Simply shows the bitcoin price in $ from mtgox.com and the total price of all the bitcoins you own.
# Screenshot: http://i.imgur.com/UatFwkM.png
# Donations --> 1HpQQds9wUFykgwKyzXp747SAb6374RjUL
from sys import stdout
import argparse
import locale
from socketIO_client import SocketIO, BaseNamespace
parser = argparse.ArgumentParser(description='Bitcoin watch')
parser.add_argument('bitcoins', type=float, help='how much bitcoins do you own?')
# parser.add_argument('--proxy', type=str, default='', help='http proxy') TODO
args = parser.parse_args()
class c:
FALL = '\033[%s;31m'
RISE = '\033[%s;32m'
ENDC = '\033[0m'
TOTAL = '\033[1;30m'
CLEAR = '\033[2J\033[;H'
def spinner():
items = ['—', '\\', '|', '/']
while True:
for item in items:
yield item
last_price = 0
changed = 0
color = c.RISE
the_spinner = spinner()
class MtgoxNamespace(BaseNamespace):
last_price = 0
changed = 0
the_spinner = spinner()
def on_message(self, message):
if 'ticker' in message:
try:
price = float(message['ticker']['last']['value'])
except:
price = -1
if price > last_price:
color = c.RISE
changed = 1
elif price < self.last_price:
color = c.FALL
changed = 1
else:
changed = 0
total_price = args.bitcoins * price
if total_price < 100: # show decimal points under $100
total_format = '%0.2f'
else:
total_format = '%d'
total_price = locale.format(total_format, total_price, grouping=True)
stdout.write('%s$%s%0.2f %s%s %s(%s)%s' % (c.CLEAR, color % changed, price, c.ENDC, next(the_spinner), c.TOTAL, total_price, c.ENDC))
stdout.flush()
self.last_price = price
socketIO = SocketIO('socketio.mtgox.com', 80)
#socketIO = SocketIO('websocket.mtgox.com', 80)
mtgocks_s = socketIO.connect('/mtgox', MtgoxNamespace)
mtgocks_s.emit('message', {
'channel': 'dbf1dee9-4f2e-4a08-8cb7-748919a71b21',
'op': 'unsubscribe',
})
mtgocks_s.emit('message', {
'channel': '24e67e0d-1cad-4cc0-9e7a-f8523ef460fe',
'op': 'unsubscribe',
})
socketIO.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment