Author

Topic: Sharing my Bitcoin Conky stuff (Read 2613 times)

hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
September 13, 2011, 08:57:16 PM
#1
For Linux users I'd thought I'd just share a few bits of code I used to put Bitcoin stats in my conky config.

Since accessing the various site apis takes a bit of time and the conky exec functions are blocking (stops the other stats updating) I decided to do the updating as a separate cron job. This seems like the easiest approach.

First, here's a snippet of the resulting conky info on my screen:


I borrowed a nice little jsongrep python script from Terry Jones blog. I modified it slightly to output data using the json.dumps so that the unicode (u'') wouldn't be prefixed. I use this script from my conkybtc script to grab json data from some api calls.

(note that I like to put my scripts in a ~/.bin directory. they can be put wherever you prefer)
(just make sure they are in the path and chmod +x them)

~/.bin/jsgrep
Code:
#!/usr/bin/env python

import sys
import re
import json

def jsongrep(d, patterns):
    try:
        pattern = patterns.pop(0)
    except IndexError:
        print json.dumps(d, indent=4)
    else:
        if isinstance(d, dict):
            keys = filter(pattern.match, d.keys())
        elif isinstance(d, list):
            keys = map(int,
                       filter(pattern.match,
                              ['%d' % i for i in range(len(d))]))
        else:
            if pattern.match(str(d)):
                print json.dumps(d, indent=4)
            return
        for item in (d[key] for key in keys):
            jsongrep(item, patterns[:])

if __name__ == '__main__':
    try:
        j = json.loads(sys.stdin.read())
    except ValueError, e:
        print >>sys.stderr, 'Could not load JSON object from stdin.'
        sys.exit(1)

    jsongrep(j, map(re.compile, sys.argv[1:]))

Now my conkybtc script that is called by cron. You can mod this as you like to give different data or formatting. Also, you may want to mod it for some pool other than ArsBitcoin.

~/.bin/conkybtc
Code:
#!/bin/bash

APIKEY="blahblah"
BUF="/home/`whoami`/.conkybtc"
HSTYLE="\${font DejaVuSansMono:size=8.5}\${color red}"
DSTYLE="\${font DejaVuSansMono:size=8.5:style=bold}\${color white}"
SMALL="\${font DejaVuSansMono:size=7}"

DIFF=`curl -s http://blockexplorer.com/q/getdifficulty`
BLK=`curl -s http://blockexplorer.com/q/getblockcount`
ARS=( `curl -sk https://www.arsbitcoin.com/api.php?api_key=$APIKEY |.bin/jsgrep "hashrate|totalPPSWork|paidPPSWork" |tr -d \"` )
GOX=( `curl -sk https://mtgox.com/api/0/data/ticker.php |.bin/jsgrep ticker "last|vol" |tr -d \"` )
_NR="`curl -s http://blockexplorer.com/q/estimate`*0.007158278"
_NXT=`curl -s http://blockexplorer.com/q/nextretarget`
NETHASH="`echo $_NR | bc`"
NXT=$(($_NXT-$BLK))

echo -e "${HSTYLE}Total BTC \${goto 87}${DSTYLE}${ARS[0]}\${goto 182}${HSTYLE}Confirmed BTC \$alignr${DSTYLE}${ARS[1]}" >$BUF
echo -e "${HSTYLE}MHash/s \${goto 87}${DSTYLE}${ARS[2]}\${goto 182}${HSTYLE}Network GHash/s \$alignr${DSTYLE}${NETHASH%%.*}" >>$BUF
echo -e "${HSTYLE}BlockCount \${goto 87}${DSTYLE}$BLK ${SMALL}($NXT)\${goto 182}${HSTYLE}Diffculty \$alignr${DSTYLE}${DIFF%%.*}" >>$BUF
echo -e "${HSTYLE}Last Price \${goto 87}${DSTYLE}${GOX[0]}\${goto 182}${HSTYLE}Volume BTC/24h\$alignr${DSTYLE}${GOX[1]}" >>$BUF

To add this to your cron, running every minute, use crontab -e and enter,

* * * * * ~/.bin/conkybtc

Finally, to make conky update the stats every 30 seconds (arbitrary) add a line like this to your .conkyrc file. This just inserts the data using cat, which means it's a quick update and other stuff still continues to update as well.

~/.conkyrc
Code:
${color}${font DejaVuSansMono:style=bold:size=8.5}BitCoin $hr
${execpi 30 cat .conkybtc }

That's all folks!
Jump to: