#msghub = MsgHub("198.199.97.15", 12345) current = int(round(time.time() * 1000)) msghub.erase('bitcoin', {"timeframe": {"max": current - 10800000}}) count = 0 while True: try: r = requests.get('http://data.mtgox.com/api/1/BTCUSD/ticker', timeout=5) _current = int(json.loads(r.text)['return']['now']) / 1000 _price = json.loads(r.text)['return']['last_local']['display'] if _current > current: current = _current print str(int(_current)) + ", " + _price msghub.publish("bitcoin", json.loads(r.text), True) msghub.save("bitcoin", json.loads(r.text)) count = count + 1 if count > 60: msghub.erase('bitcoin', {"timeframe": {"max": current - 10800 * 1000}}) count = 0 time.sleep(10) except Exception as e: print e time.sleep(1) msghub = MsgHub("pubsub.msghub.io", 443, secure=True)
def handle_keypress(ch): if ch == '\x7f': sys.stdout.write('\033[1D \033[1D') elif ch == '\x1b': pass else: sys.stdout.write(ch) fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) try: while True: try: c = sys.stdin.read(1) msghub.publish("keypress", {"ch": c, "key": None}, False) # msghub.publish("keypress", c, True) handle_keypress(c) # print "Got character", repr(c) except IOError: pass finally: termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)