def _parse_query(query): coin, interval_string = _split_query(query) coin = coin.upper() coin2 = None if '/' in coin: coin, coin2 = coin.split('/', 1) if coins_names.coin_name(coin) == '' and currencies_names.currency_name( coin) == '': raise SyntaxError("Invalid coin/currency name: %s" % coin) if coin2 and coins_names.coin_name( coin2) == '' and currencies_names.currency_name(coin2) == '': raise SyntaxError("Invalid coin/currency name: %s" % coin2) try: time_begin, time_end = interval.parse_interval(interval_string) except OverflowError: # to be fixed: ranges like yesterday, today, now and so on raise RuntimeError("Wrong range specification: %s" % interval_string) if time_begin is None or time_end is None: raise SyntaxError("Invalid time interval specification: %s" % interval_string) return coin, coin2, time_begin, time_end
def _make_header(self): coin_symbol = self.data['meta']['symbol'] coin_name = coins_names.coin_name(coin_symbol) meta = self.data['meta'] interval_name = interval.from_secs(self.interval) time_interval = "%s +%s" % (self._format_time( meta['time_begin']), interval_name) #self._format_time(meta['time_end'], show_date=True, show_time=True), output = "\n" if self.currency == 'USD': output += u"{-1▶ %s (%s) }{1▶}" % (coin_name, coin_symbol) else: currency_symbol = self.currency currency_name = currencies_names.currency_name(self.currency) if not currency_name: currency_name = coins_names.coin_name(self.currency) output += u"{-1▶ %s (%s) to %s (%s) }{1▶}" % \ (coin_name, coin_symbol, currency_name, currency_symbol) #output += u"{1%s (%s)}," % (coin_name, coin_symbol) output += " %s" % (time_interval) output += " %s\n" % self._show_change_percentage()[1] output += "\n\n" return output
def _parse_query(query): coin, interval_string = _split_query(query) coin = coin.upper() coin2 = None if '/' in coin: coin, coin2 = coin.split('/', 1) if coins_names.coin_name(coin) == '' and currencies_names.currency_name(coin) == '': raise SyntaxError("Invalid coin/currency name: %s" % coin) if coin2 and coins_names.coin_name(coin2) == '' and currencies_names.currency_name(coin2) == '': raise SyntaxError("Invalid coin/currency name: %s" % coin2) try: time_begin, time_end = interval.parse_interval(interval_string) except OverflowError: # to be fixed: ranges like yesterday, today, now and so on raise RuntimeError("Wrong range specification: %s" % interval_string) if time_begin is None or time_end is None: raise SyntaxError("Invalid time interval specification: %s" % interval_string) return coin, coin2, time_begin, time_end
def _make_header(self): coin_symbol = self.data['meta']['symbol'] coin_name = coins_names.coin_name(coin_symbol) meta = self.data['meta'] interval_name = interval.from_secs(self.interval) time_interval = "%s +%s" % ( self._format_time(meta['time_begin']), interval_name) #self._format_time(meta['time_end'], show_date=True, show_time=True), output = "\n" if self.currency == 'USD': output += u"{-1▶ %s (%s) }{1▶}" % (coin_name, coin_symbol) else: currency_symbol = self.currency currency_name = currencies_names.currency_name(self.currency) if not currency_name: currency_name = coins_names.coin_name(self.currency) output += u"{-1▶ %s (%s) to %s (%s) }{1▶}" % \ (coin_name, coin_symbol, currency_name, currency_symbol) #output += u"{1%s (%s)}," % (coin_name, coin_symbol) output += " %s" % (time_interval) output += " %s\n" % self._show_change_percentage()[1] output += "\n\n" return output
def view(query, use_currency=None): """ Main rendering function, entry point for this module. Returns rendered view for the ``query``. If currency is specified in ``query``, it overrides ``currency``. If ``currency`` is not specified, USD is used. """ try: coin, coin2, time_begin, time_end = _parse_query(query) except SyntaxError as e_msg: raise RuntimeError("%s" % e_msg) # if currency is specified in the domain name (use_currency) # but not in the query, then we use it as the output currency if use_currency \ and not coin2 \ and (coins_names.coin_name(use_currency) != '' \ or currencies_names.currency_name(use_currency) != ''): coin2 = use_currency print(coin2) ticks = 80 if coin2: data = aggregate.get_aggregated_pair(coin, coin2, time_begin, time_end, ticks) else: data = aggregate.get_aggregated_coin(coin, time_begin, time_end, ticks) if data['ticks'] == []: raise RuntimeError("No data found for your query. Wrong range?") #import json #print json.dumps(data['meta'], indent=True) warnings = [] if data['meta']['time_begin'] - time_begin > 3600 * 24: warnings.append( "for the moment, rate.sx has historical data only starting from 2018-Jan-07" ) options = dict( width=80, height=25, msg_interval='@' not in query, currency=coin2 or 'USD', warnings=warnings, ) dia = Diagram(data, (time_begin, time_end), options=options) return dia.make_view()
def view(query, use_currency=None): """ Main rendering function, entry point for this module. Returns rendered view for the ``query``. If currency is specified in ``query``, it overrides ``currency``. If ``currency`` is not specified, USD is used. """ try: coin, coin2, time_begin, time_end = _parse_query(query) except SyntaxError as e_msg: raise RuntimeError("%s" % e_msg) # if currency is specified in the domain name (use_currency) # but not in the query, then we use it as the output currency if use_currency \ and not coin2 \ and (coins_names.coin_name(use_currency) != '' \ or currencies_names.currency_name(use_currency) != ''): coin2 = use_currency print coin2 ticks = 80 if coin2: data = aggregate.get_aggregated_pair(coin, coin2, time_begin, time_end, ticks) else: data = aggregate.get_aggregated_coin(coin, time_begin, time_end, ticks) if data['ticks'] == []: raise RuntimeError("No data found for your query. Wrong range?") #import json #print json.dumps(data['meta'], indent=True) warnings = [] if data['meta']['time_begin'] - time_begin > 3600*24: warnings.append( "for the moment, rate.sx has historical data only starting from 2018-Jan-07") options = dict( width=80, height=25, msg_interval='@' not in query, currency=coin2 or 'USD', warnings=warnings, ) dia = Diagram(data, (time_begin, time_end), options=options) return dia.make_view()
def get_data(query, use_currency=None): try: coin, coin2, time_begin, time_end = _parse_query(query) except SyntaxError as e_msg: raise RuntimeError("%s" % e_msg) # if currency is specified in the domain name (use_currency) # but not in the query, then we use it as the output currency if use_currency \ and not coin2 \ and (coins_names.coin_name(use_currency) != '' \ or currencies_names.currency_name(use_currency) != ''): coin2 = use_currency ticks = 80 if coin2: data = aggregate.get_aggregated_pair(coin, coin2, time_begin, time_end, ticks) else: data = aggregate.get_aggregated_coin(coin, time_begin, time_end, ticks) return data