def execute(self, exchange, key): """Search the ticker response recursively for the key specified Args: exchange (OBJECT): Exchange object Returns: FLOAT: Value for the key specified """ res = None # Request the ticker and get the HTTP response response = get_ticker_response(exchange) if response: # Deserialize the response content data = self._deserialize(response) if data: # If the route to key is set on the exchange configuration if exchange.route: route = exchange.route route = route.split(",") res = self._search_by_key_route(data, *route) # If the route is not set then search for the key directly else: res = self._search_by_key(data, key) return res
def get_graphs(): """ Store graph data in database to be used later by the graphs API """ # List of exchanges to store values for a candlesticks graph candlesticks_exchanges = ['Bitstamp'] for exchange in active_exchanges(): ticker = exchange.url + exchange.api subject = '[ERROR] %s exchange (GRAPH)' % exchange.name error_msg = "Exchange: %s\nTicker: %s\nException: " % (exchange.name, ticker) try: last = opn = high = low = close = volume = None last = exchange.get_value(exchange.key_last) # Close session opened after the execution of get_value method db.session.close() if exchange.name in candlesticks_exchanges: # Get the open value (the first value of the day) opn, buy, trend = get_alternate_value(exchange, position='first') # Get ticker to obtain additional values to show in graph response = get_ticker_response(exchange) if opn and response: # Serialize ticker response json = loads(response.content) close = json['last'] or None, high = json['high'] or None, low = json['low'] or None, volume = float(json['volume']) or None, except ConnectionError: if not last: previous = get_alternate_value(exchange, position='previous') if previous: last = previous[0] excep = 'Error connecting with API. Using previous value.' if app.config.get('ENABLE_MAIL_NOTIFICATIONS', False): send_async_email(subject, error_msg + excep) else: excep = 'Error connecting with API. No previous value get.' if app.config.get('ENABLE_MAIL_NOTIFICATIONS', False): send_async_email(subject, error_msg + excep) print subject + ': ' + excep except Exception as err: print subject + ': ' + str(err.message) if app.config.get('ENABLE_MAIL_NOTIFICATIONS', False): send_async_email(subject, error_msg + str(err.message)) finally: try: # Write obtained values to database graph = Graph( exchange_id=exchange.id, last=last, opn=opn, close=close, high=high, low=low, volume=volume, date=date.today(), time=datetime.now().time() ) if graph.last: print "Getting graph data for '%s'" % exchange.name db.session.add(graph) else: raise Exception except Exception as err: if not err.message: err.message = 'Connection Error. Ticker API is down.' print subject + ': ' + str(err.message) if app.config.get('ENABLE_MAIL_NOTIFICATIONS', False): send_async_email(subject, error_msg + str(err.message)) finally: db.session.commit() db.session.close()