def test_generic_stock_info_cooldown(self): """make sure cooldown works as expected""" dump_response = commands.generic_stock_info(self.alternate_ticker, self.conn, cooldown_time=10) assert dump_response != '' response = commands.generic_stock_info(self.alternate_ticker, self.conn, cooldown_time=10) assert response == ''
def test_generic_stock_info_happypath(self): """make sure expected path works as expected""" response = commands.generic_stock_info(self.stock_ticker, self.conn) print(response) values = search('{company_name} {price:f} {change_pct:%}', response) assert values['company_name'] == 'Micron Technology, Inc. Common Stock' assert isinstance(values['price'], float) assert isinstance(values['change_pct'], float)
def stock_news(message, ticker): """fetch relevant article for requested stock""" ticker = ticker.upper() message_info = platform_utils.parse_slack_message_object(message) api_config.LOGGER.info('#%s @%s -- Stock News %s', message_info['channel_name'], message_info['user_name'], ticker) mode = connections.check_channel_mode(message_info['channel'], CONN, logger=api_config.LOGGER) api_config.LOGGER.info('Channel mode: %s', mode.value) try: if mode == connections.Modes.stocks: quote = commands.generic_stock_info( ticker, CONN, cooldown_time=0, logger=api_config.LOGGER, info_mask=['name', 'current_price', 'change_pct']) if not quote: raise exceptions.EmptyQuoteReturned direction = float(quote.split()[-1].replace('%', '')) link, details = commands.stock_news(ticker, direction, logger=api_config.LOGGER) elif mode == connections.Modes.coins: api_config.LOGGER.warning('not supported') message.send('mode=coins not supported') quote = '' else: api_config.LOGGER.error('UNEXPECTED CHANNEL MODE -- #%s %s', message_info['channel_name'], str(mode), exc_info=True) quote = '' except exceptions.ProsperBotException: api_config.LOGGER.warning('Unable to resolve basic stock info for %s', ticker, exc_info=True) quote = 'ERROR - NO QUOTE DATA FOUND FOR {}'.format(ticker) link = '' except Exception as err: api_config.LOGGER.error('Unable to resolve basic stock info for %s', ticker, exc_info=True) quote = 'ERROR - UNABLE TO RESOLVE NEWS {} -- {}'.format( ticker, repr(err)) link = '' if quote: # only emit if there is data api_config.LOGGER.debug(quote) message.send('`' + quote + '`\n' + link #+ ' ' + details )
def generic_stock_info(message, ticker): """echo basic info about stock""" ticker = ticker.upper() message_info = platform_utils.parse_slack_message_object(message) api_config.LOGGER.info('#%s @%s -- Basic company info %s', message_info['channel_name'], message_info['user_name'], ticker) mode = connections.check_channel_mode(message_info['channel'], CONN, logger=api_config.LOGGER) api_config.LOGGER.info('Channel mode: %s', mode.value) try: if mode == connections.Modes.stocks: data = commands.generic_stock_info(ticker, CONN, cooldown_time=CONFIG.get_option( 'ProsperBot', 'generic_info', None, 30), logger=api_config.LOGGER) elif mode == connections.Modes.coins: data = commands.generic_coin_info(ticker, CONN, cooldown_time=CONFIG.get_option( 'ProsperBot', 'generic_info', None, 30), logger=api_config.LOGGER) else: api_config.LOGGER.error('UNEXPECTED CHANNEL MODE -- #%s %s', message_info['channel_name'], str(mode), exc_info=True) data = '' except Exception: # pramga: no cover api_config.LOGGER.error('Unable to resolve basic stock info for %s', ticker, exc_info=True) data = '' if data: # only emit if there is data api_config.LOGGER.debug(data) message.send('`' + data + '`')
async def price(context, ticker): """fetch relevant article for requested stock""" ticker = ticker.upper() message_info = platform_utils.parse_discord_context_object(context) api_config.LOGGER.info('%s #%s @%s -- Stock News `%s`', message_info['team_name'], message_info['channel_name'], message_info['user_name'], ticker) try: quote = commands.generic_stock_info( ticker, CONN, cooldown_time=0, logger=api_config.LOGGER, info_mask=['name', 'current_price', 'change_pct']) if not quote: raise exceptions.EmptyQuoteReturned direction = float(quote.split()[-1].replace('%', '')) link, details = commands.stock_news(ticker, direction, logger=api_config.LOGGER) except exceptions.ProsperBotException: api_config.LOGGER.warning('Unable to resolve basic stock info for %s', ticker, exc_info=True) quote = 'ERROR - NO QUOTE DATA FOUND FOR {}'.format(ticker) link = '' details = '' except Exception as err: api_config.LOGGER.error('Unable to resolve basic stock info for %s', ticker, exc_info=True) quote = 'ERROR - UNABLE TO RESOLVE NEWS {} -- {}'.format( ticker, repr(err)) link = '' if quote: # only emit if there is data api_config.LOGGER.debug(quote) await bot.say('```' + quote + '```\n' + link + ' ' + details)
def test_generic_stock_info_badticker(self): """validate unsupported ticker response""" response = commands.generic_stock_info('BUTTS', self.conn) assert response == ''