Created
August 29, 2011 20:01
-
-
Save andrix/1179246 to your computer and use it in GitHub Desktop.
Notificador de cambio de cotización | Brou | Gales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import time | |
import urllib | |
import pynotify | |
import cPickle | |
import signal | |
from scrapy.selector import HtmlXPathSelector | |
COTIZHOME = "%s/.notifycotiz" % os.getenv('HOME') | |
COTIZDB = "cotizaciones.pickle" | |
def get_cotizacion_moneda(html, main_xpath, compra_xpath, venta_xpath): | |
hsel = HtmlXPathSelector(text=html) | |
cotiz = hsel.select(main_xpath) | |
return { | |
'compra': float(cotiz.select(compra_xpath).extract()[0].replace(',', '.')), | |
'venta': float(cotiz.select(venta_xpath).extract()[0].replace(',', '.')) | |
} | |
def _cotizacion_dolar_brou(html): | |
return get_cotizacion_moneda(html, "//td[@id='column-2']//div[@id='exchangeRatesLarge']/table/tr[2]", | |
"td[@class='buy']/text()", "td[@class='sale']/text()") | |
def _cotizacion_dolar_gales(html): | |
return get_cotizacion_moneda(html, "//table[@class='monedas']/tr[1]", | |
"td[2]/text()", "td[3]/text()") | |
SOURCES = [ | |
('brou', 'http://brou.com.uy/web/guest/institucional/cotizaciones', _cotizacion_dolar_brou), | |
('gales', 'http://gales.com.uy/home/', _cotizacion_dolar_gales), | |
] | |
def notify(title, msg): | |
"""Simple wrapper around pynotify OSD""" | |
n = pynotify.Notification(title, msg) | |
if not n.show(): | |
print "Fail to show notification" | |
del n | |
sys.exit(1) | |
del n | |
def main(): | |
if not pynotify.init("Basics"): | |
print "Can't initialize PyNotify" | |
sys.exit(1) | |
if not os.path.exists(COTIZHOME): | |
os.mkdir(COTIZHOME) | |
dbpath = os.path.join(COTIZHOME, COTIZDB) | |
cotizs = {} | |
if os.path.exists(dbpath): | |
try: | |
cotizs = cPickle.load(open(dbpath)) | |
except EOFError, UnpicklingError: | |
pass | |
def _format_cotiz(cotizs): | |
if not cotizs: | |
return "" | |
last = "\n".join("[%s] %.2f - %.2f" % (src, v['compra'], v['venta']) for src, v in cotizs.items()) | |
return "Dolar\n%s" % last | |
def _handler(signum, frame): | |
print "Exiting applicatin gracefully..." | |
print "Saving data ..." | |
with open(dbpath, 'w') as db: | |
cPickle.dump(cotizs, db) | |
print "Saved!" | |
print "Bye!" | |
sys.exit(1) | |
signal.signal(signal.SIGINT, _handler) | |
while True: | |
notify("Obteniendo cotizaciones ...", _format_cotiz(cotizs)) | |
for src, url, fun in SOURCES: | |
html = urllib.urlopen(url).read() | |
print >>sys.stderr, "Get data from [%s] : [%s]" % (src, url) | |
try: | |
dolar = fun(html) | |
except Exception, e: | |
print >>sys.stderr, "ERROR: getting data from [%s], exc: %s" % (src, e.msg) | |
if src not in cotizs: | |
cotizs[src] = dolar | |
oldcotiz = cotizs[src] | |
if oldcotiz['compra'] != dolar['compra'] or oldcotiz['venta'] != dolar['venta']: | |
ncotiz = "Vieja: %.2f - %.2f\nNueva: %.2f - %.2f" % (oldcotiz['compra'], oldcotiz['venta'], | |
dolar['compra'], dolar['venta']) | |
notify("[%s] Cotizacion cambio" % src.upper(), ncotiz) | |
cotizs[src] = dolar | |
with open(dbpath, 'w') as db: | |
cPickle.dump(cotizs, db) | |
time.sleep(300) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment