Background
I use conky to monitor things (system as well as news, mail, weather, etc.) and felt the need for a bitcoin price tracker (ticker). After finding nothing that fit my needs (cli based, light on resources, and customizable), I decided to write my own. The following code is my first attempt (which works) but I did make a project out of this to make it a little more portable and robust (not to mention to enable other exchanges as Mt.Gox didn’t last long). See the links section for project info.
Details
The python script is as follows (or see attached source):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| #! /usr/bin/env python
#coding: utf-8
#Adercon Consulting (adercon.com). This code may be reused at will but please leave this attribution intact.
import urllib2,datetime
import json as simplejson
#variables.
data_source = 'https://data.mtgox.com/api/2/BTCUSD/money/ticker'
date_format = '%Y-%m-%d %H:%M:%S'
out_sep = ' | '
app_name = 'MT.GOX Ticker'
app_version = '1.0.0'
#get.
req = urllib2.Request(data_source, None, {'user-agent':'text/json'})
opener = urllib2.build_opener()
f = opener.open(req)
data = simplejson.load(f) #deserialize
#process.
price = data['data']['last_all']['display_short']
high = data['data']['high']['display_short']
low = data['data']['low']['display_short']
volume = data['data']['vol']['display_short'].replace(u'\xa0',' ') #re-encode space character for utf-8
asof = datetime.datetime.fromtimestamp(int(data['data']['now'][:10])).strftime(date_format)
#print.
print app_name+' @'+asof+ \
'\n Now: '+price+' (vol: '+volume+')'+ \
'\n high: '+high+out_sep+'low: '+low
|
This script can be called from conky as follows:
${texeci 60 ~/scripts/mtgox_ticker.py}
The above uses threaded execi to call the mtgox_ticker.py script every 60 seconds.
The results will be similar to the following:
Notes
- Please see the “Project info” link below for updated code and or logic as the above uses the now defunct mt gox exchange api.
Links