def get_api_graphs(exchange_name): """Generate response used by the graphs API Args: exchange_name (STRING): Exchange name to lowercase Returns: JSON: Response containing the exchange's close value of each day """ data = {} # Get the Exchange object corresponding to the exchange name provided exchange = Exchange.query.filter_by(name=exchange_name).first() try: # Get all datapoints (datapoint = close value of a day) datapoints = Graph.query.filter_by(exchange_id=exchange.id).all() # If there are datapoints and 'graph_type' parameter is in the request graph = [] if datapoints and 'graph_type' in request.args: # Linear graph if request.args['graph_type'] == 'linear': for dp in datapoints: date = datetime.combine(dp.date, datetime.min.time()) datapoint = { 'date': timestamp2js(timestamp2unix(date)), 'price': dp.last } graph.append(datapoint) # Candlesticks graph elif request.args['graph_type'] == 'candlesticks': for dp in datapoints: date = datetime.combine(dp.date, datetime.min.time()) datapoint = { 'date': timestamp2js(timestamp2unix(date)), 'open': dp.opn or None, 'high': dp.high or None, 'low': dp.low or None, 'close': dp.close or None, 'volume': dp.volume or None } graph.append(datapoint) data[exchange_name.replace(' ', '_')] = graph return jsonify(data) except: return 'Error'
def get_api_ticker(): """Generate response used by the ticker API Returns: DICT: Contains average values for ARS and USD exchanges """ exchanges_ars = ['digicoins', 'ripio', 'unisend', 'bitcoin_brothers'] exchanges_usd = ['la_nacion_blue', 'geeklab', 'infobae_blue'] now = datetime.now() res = { '_timestamp': now.strftime('%a %b %d %Y, %H:%M:%S'), '_timestamp_unix': timestamp2unix(now), 'source': 'bitcharts.io', } try: avg_ars = get_avg(exchanges_ars) avg_usd = get_avg(exchanges_usd) if avg_ars: res['last_ars'] = float("{0:.2f}".format(avg_ars)) if avg_usd: res['last_usd'] = float("{0:.2f}".format(avg_ars / avg_usd)) except Exception, e: print e res = 'An error has occurred. Try again later.'