Skip to content

Instantly share code, notes, and snippets.

@vindolin
Last active December 16, 2015 01:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vindolin/5353600 to your computer and use it in GitHub Desktop.
Save vindolin/5353600 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*-
# 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
import requests
import json
from time import sleep
from sys import stdout
import argparse
import locale
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')
parser.add_argument('--interval', type=int, default=5, help='refresh interval')
args = parser.parse_args()
if args.interval < 1:
args.interval = 1
if args.proxy:
proxies = {"http": args.proxy}
else:
proxies = None
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()
while True:
try:
#r = requests.get('http://data.mtgox.com/api/1/BTCUSD/ticker', proxies=proxies)
#price = float(data['return']['last']['value'])
r = requests.get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', proxies=proxies)
data = json.loads(r.content)
price = float(data['data']['last']['value'])
except:
price = -1
if price > last_price:
color = c.RISE
changed = 1
elif price < 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()
last_price = price
sleep(args.interval)
@moshekaplan
Copy link

Some changes to consider:

def spinner():
    items = ['—', '\\', '|', '/',]
    while True:
        for item in items:
            yield item

There's also no need to use vars() with argparse, you can access the fields with:

args = parser.parse_args()
args.bitcoins
# ...

@vindolin
Copy link
Author

@moshekapian: thanks!

@Yeraze
Copy link

Yeraze commented Jun 8, 2014

Needs to be updated since mtgox.com failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment