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 test_stock_news_happypath_neg(self): """make sure service works as expected - negative case""" url, score = commands.stock_news(self.stock_ticker, -1.0) if not url: url, score = self.retry_stock_news(1.0) assert isinstance(url, str) assert isinstance(score, str) # scores are cast to str assert float(score) <= 0.0
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_stock_news_badticker(self): """invalid ticker behavior""" url, score = commands.stock_news(self.bad_ticker, 1.0) assert url == 'NO NEWS FOUND' assert score == ''
def test_stock_news_happypath_neut(self): """make sure service works as expected - neutral case""" url, score = commands.stock_news(self.stock_ticker, 0.0) assert url == '' assert score == ''
def retry_stock_news(self, direction): """retry with a different ticker""" return commands.stock_news(self.alt_ticker, direction)