def _regression_test_for_parser(self, parser_name: str): parser = get_parser(parser_name)() usd_currency = parser.get_currency('USD', date=self.TODAY) self._validate_currency(usd_currency, iso_code='USD') currencies = parser.get_all_currencies(date=self.TODAY) [self._validate_currency(c) for c in currencies]
def course(bot, update, args, **kwargs): user_id = str(update.message.from_user.id) chat_id = update.message.chat_id preferences = utils.parse_args(bot, update, args) if not preferences: return days_diff = preferences['days_ago'] bank_name = preferences['bank_name'] if not bank_name: bank_name = utils.get_user_selected_bank(user_id) parser = utils.get_parser(bank_name) parser_instance = parser(cache=default_cache) parse_date = utils.get_date_from_date_diff(days_diff, datetime.date.today()) if preferences['currency'] == 'all': # We need to send data about all of the currencies all_currencies = cache_proxy.get_all_currencies(parser_instance, date=parse_date) all_currencies = utils.sort_currencies(all_currencies) displayed_values = [ utils.format_currency_string(x) for x in all_currencies ] header = [ _("\tBuy\tSell"), ] currencies_text_value = "\n".join(header + displayed_values) bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.sendMessage( chat_id=chat_id, text=_("Currencies: \n{curs}").format(curs=currencies_text_value), parse_mode=telegram.ParseMode.HTML) return currency = preferences['currency'] if currency.upper() in parser.allowed_currencies: # TODO: unify passing currency names (lowercase or uppercase only) cur = cache_proxy.get_currency(parser_instance, currency_name=currency, date=parse_date) if cur.name == 'NoValue': bot.sendMessage(chat_id=chat_id, text=_("Unknown currency: {}").format(args[0])) return else: text = utils.format_currency_string(cur) bot.sendMessage(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML) return else: text = _("Unknown currency: {}").format(currency) bot.sendMessage(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML) return
def show_currency_graph(bot, update, args, **kwargs): """Sends user currency graph changes for the specified period of time. E.g. user wants to get exchange rates for the US currency for 10 last days, he needs to send something like '/graph USD -d 10' """ user_id = str(update.message.from_user.id) chat_id = update.message.chat_id bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) preferences = utils.parse_args(bot, update, args) if not preferences: return days_diff = preferences['days_ago'] if days_diff == 0: # Well, it's dirty days_diff = 30 currency = preferences['currency'] bank_name = preferences['bank_name'] if not bank_name: bank_name = utils.get_user_selected_bank(user_id) parser = utils.get_parser(bank_name) parser_instance = parser(cache=default_cache) if currency == 'all': currency = settings.DEFAULT_CURRENCY.upper() date_diffs = utils.date_diffs_for_long_diff(days_diff) today = datetime.date.today() dates = [utils.get_date_from_date_diff(d, today) for d in date_diffs] past_date, future_date = dates[0], dates[-1] plot_image_name = plotting.generate_plot_name(parser.short_name, currency, past_date, future_date) if not os.path.exists(settings.IMAGES_FOLDER): try: os.mkdir(settings.IMAGES_FOLDER) except OSError as e: logger.error("Error creating images folder: ".format(e)) output_file = os.path.join(settings.IMAGES_FOLDER, plot_image_name) if not utils.is_image_cached(output_file): # We use thread pool to asyncronously get pages currencies_deque = deque() with ThreadPoolExecutor(max_workers=10) as executor: future_to_date = { executor.submit(result_date_saver, parser_instance, currency, date): date for date in dates } for future in as_completed(future_to_date): data = future.result() currencies_deque.append(data) currencies = utils.sort_by_value(currencies_deque, dates) logging.info("Creating a plot.") x = [d for d in dates] y_buy = [c.buy / c.multiplier for c in currencies] y_sell = [c.sell / c.multiplier for c in currencies] plotting.render_exchange_rate_plot(x, y_buy, y_sell, output_file) plotting.reset_plot() bot.sendPhoto(chat_id=chat_id, photo=open(output_file, 'rb')) return
def test_getting_non_existing_parser_raises_error(self): with self.assertRaises(BotParserLookupError): get_parser(self.NON_EXISTING_PARSER_NAME)
def test_getting_existing_parser_returns_appropriate_class(self): parser_cls = get_parser(self.EXISTING_PARSER_NAME) self.assertEqual(parser_cls.short_name, self.EXISTING_PARSER_NAME)