Skip to content

Instantly share code, notes, and snippets.

@chrisns
Created December 18, 2013 14:38
Show Gist options
  • Save chrisns/8023379 to your computer and use it in GitHub Desktop.
Save chrisns/8023379 to your computer and use it in GitHub Desktop.
btc/ltc/usd ticker
#!/usr/bin/env python
# Based on the Gmail appindicator from
# http://conjurecode.com/create-indicator-applet-for-ubuntu-unity-with-python/
import sys
import os
import gtk
import appindicator
import requests
import imaplib
import re
PING_FREQUENCY = 2 # seconds
class CheckStock:
def __init__(self):
self.ind = appindicator.Indicator('new-stock-indicator',
'',
appindicator.CATEGORY_OTHER)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.menu_setup()
self.ind.set_menu(self.menu)
def menu_setup(self):
self.menu = gtk.Menu()
self.refresh_item = gtk.MenuItem('Refresh')
self.refresh_item.connect('activate', self.refresh)
self.refresh_item.show()
self.menu.append(self.refresh_item)
self.reload_item = gtk.MenuItem('Reload')
self.reload_item.connect('activate', self.reload)
self.reload_item.show()
self.menu.append(self.reload_item)
self.quit_item = gtk.MenuItem('Quit')
self.quit_item.connect('activate', self.quit)
self.quit_item.show()
self.menu.append(self.quit_item)
def main(self):
self.update_stock_price()
gtk.timeout_add(PING_FREQUENCY * 1000, self.update_stock_price)
gtk.main()
def quit(self, widget):
sys.exit(0)
def refresh(self, widget):
self.update_stock_price()
def reload(self, widget):
# If this script was updated, just reload it instead of forcing the
# user to quit first.
os.execl(sys.executable, *([sys.executable]+sys.argv))
def update_stock_price(self):
try:
btc = requests.get('https://btc-e.com/api/2/btc_usd/ticker').json()
ltc = requests.get('https://btc-e.com/api/2/ltc_usd/ticker').json()
self.ind.set_label('BTC: %0.2f LTC: %0.2f' % (btc['ticker']['last'], ltc['ticker']['last']))
except:
self.ind.set_label('???')
return True
if __name__ == '__main__':
indicator = CheckStock()
indicator.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment