def _Handle(self, channel: types.Channel, unused_user: str, symbols: str): symbols = symbols or 'GOOG,GOOGL' symbols = self._core.stocks.ParseSymbols(symbols) quotes = self._core.stocks.Quotes(symbols) if not quotes: if symbols[0].upper() == self._core.nick.upper(): return ( 'You can\'t buy a sentient AI for some vague promise of future ' 'value, peon.') else: return ( 'You can\'t trade money for %s openly, try the black market.' % symbols[0]) responses = [] if len(quotes) > 5: responses.append( 'Only displaying 5 quotes, I\'m a(n) %s not a financial advisor' % self._core.nick) histories = self._core.stocks.History(symbols) for symbol, quote in list(quotes.items())[:5]: history = histories.get(symbol) change_str = '%0.2f (%0.2f%%)' % (quote.change, quote.percent) if quote.change > 0: change_str = util_lib.Colorize(u'⬆ %s' % change_str, 'green') elif quote.change < 0: change_str = util_lib.Colorize(u'⬇ %s' % change_str, 'red') if history: change_str += ' %s' % util_lib.Sparkline(history) responses.append('One share of %s is currently worth %0.2f [%s]' % (symbol, quote.price, change_str)) return responses
def _Handle(self, channel: channel_pb2.Channel, user: user_pb2.User, target_user: user_pb2.User) -> hype_types.CommandResponse: now = arrow.utcnow() recent_transactions = self._core.bank.GetTransactions(target_user) if not recent_transactions: return '%s doesn\'t believe in the HypeCoin economy.' % target_user.display_name responses = [ 'Recent HypeCoin Transactions for %s' % target_user.display_name ] for tx in recent_transactions[:5]: amount = util_lib.FormatHypecoins(tx.amount) if tx.amount < 0: amount = util_lib.Colorize(f'{amount}', 'red') direction = 'to' elif tx.amount > 0: amount = util_lib.Colorize(f'+{amount}', 'green') direction = 'from' else: amount = amount direction = 'with' ago = util_lib.TimeDeltaToHumanDuration( now - arrow.get(tx.create_time.ToSeconds())) responses.append( f'{amount} {direction} {tx.counterparty.display_name} ' f'{ago} ago [{tx.details}]') return responses
def _InfoField(self, title, value, delta=0, population=None, up_is_good=False): value_str = '{:,}'.format(value) percent = delta / (value - delta) if percent > 0: color = 'green' if up_is_good else 'red' value_str += util_lib.Colorize('⬆{:,} ({:.1%})'.format( delta, percent), color, irc=False) elif percent < 0: color = 'red' if up_is_good else 'green' value_str += util_lib.Colorize('⬇{:,} ({:.1%})'.format( delta, percent), color, irc=False) if population: percent = float(value) / population if percent >= 0.0005: value_str += ' [{:.1%} of the population]'.format(percent) return message_pb2.Card.Field(title=title, text=value_str)
def _Handle(self, channel, user, account, tx_user): tx_user = tx_user or user if tx_user == 'me': tx_user = user tx_user = _GetUserAccount(tx_user, account) now = arrow.utcnow() recent_transactions = self._core.bank.GetTransactions(tx_user) if not recent_transactions: return '%s doesn\'t believe in the HypeCoin economy.' % tx_user responses = ['Recent HypeCoin Transactions for %s' % tx_user] for tx in recent_transactions[:5]: if tx_user == tx.get('source'): base_tx_description = ' {} to {} %s ago [%s]'.format( util_lib.Colorize('-%s', 'red'), tx.get('destination', 'unknown')) else: base_tx_description = ' {} from {} %s ago [%s]'.format( util_lib.Colorize('+%s', 'green'), tx.get('source', 'unknown')) time_delta = now - arrow.get(tx.get('ts', now.timestamp)) time_str = util_lib.TimeDeltaToHumanDuration( time_delta) if time_delta else '??' tx_description = base_tx_description % ( util_lib.FormatHypecoins(tx['amount']), time_str, tx.get('details', 'Unknown')) responses.append(tx_description) return responses
def _FormatChangeStr(self, change, percent): if not change and not percent: return '' change_str = ' %0.2f (%0.2f%%)' % (change, percent) if change > 0: change_str = util_lib.Colorize('⬆%s' % change_str, 'green') elif change < 0: change_str = util_lib.Colorize('⬇%s' % change_str, 'red') return change_str
def GetSchedule(self, subcommand, include_playoffs, num_games=5): """Get the schedule for the specified region or team.""" qualifier = 'All' display_qualifier = 'All' with self._lock: if subcommand in self.teams: qualifier = self.teams[subcommand].team_id display_qualifier = self.teams[subcommand].name if subcommand in self.leagues: qualifier = self.leagues[subcommand].league_id display_qualifier = self.leagues[subcommand].name now = arrow.utcnow() schedule = [] livestream_links = self.GetLivestreamLinks() for match in self.schedule: if self._MatchIsInteresting(match, qualifier, now, include_playoffs): # If the game is in the future, add a livestream link if one exists. If # the game is considered live, add either an existing livestream link or # the fallback link. if match.time > now: if match.time == arrow.Arrow.max: # This means rito hasn't scheduled this match yet date_time = 'TBD' else: local_time = match.time.to(self._timezone) date_time = local_time.strftime('%a %m/%d %I:%M%p %Z') if match.match_id in livestream_links: date_time += ' - %s' % livestream_links[match.match_id] else: date_time = 'LIVE' if match.match_id in livestream_links: date_time += ' - %s' % livestream_links[match.match_id] num_games_str = '' if match.games: num_games_str = 'Bo%s - ' % len(match.games) blue_team = self.MatchTeamName(match.blue) blue_team = util_lib.Colorize('{:3}'.format(blue_team), 'blue') red_team = self.MatchTeamName(match.red) red_team = util_lib.Colorize('{:3}'.format(red_team), 'red') schedule.append('{} v {}: {}{}'.format(blue_team, red_team, num_games_str, date_time)) if len(schedule) >= num_games: break if not schedule: schedule = [messages.SCHEDULE_NO_GAMES_STRING] qualifier = 'No' display_qualifier = 'No' return schedule[:num_games], display_qualifier
def GetSchedule(self, subcommand, include_playoffs, num_games=5): """Get the schedule for the specified region or team.""" qualifier = 'All' if subcommand in [t['acronym'] for t in self.teams.values()]: qualifier = subcommand qualifier = self._AliasToRegion(subcommand) or qualifier now = arrow.utcnow() query_params = {'num_games': num_games, 'include_completed': False} schedule_data = self._proxy.FetchJson( ESPORTS_API_BASE_URL + 'schedule/%s' % qualifier, query_params) schedule = [] livestream_links = self.GetLivestreamLinks() for match in schedule_data.get('matches', []): # Parse the match time into an Arrow object. Yay for serialization. match['time'] = arrow.get(match['time']) if self._MatchIsInteresting(match, qualifier, now, include_playoffs): # If the game is in the future, add a livestream link if one exists. If # the game is considered live, add either an existing livestream link or # the fallback link. if match['time'] > now: if match['time'] == arrow.Arrow.max: # This means rito hasn't scheduled this match yet date_time = 'TBD' else: local_time = match['time'].to(self._timezone) date_time = local_time.strftime('%a %m/%d %I:%M%p %Z') if match['id'] in livestream_links: date_time += ' - %s' % livestream_links[match['id']] else: date_time = 'LIVE - ' + (livestream_links.get( match['id']) or messages.FALLBACK_LIVESTREAM_LINK) num_games_str = '' if match['num_games']: num_games_str = 'Bo%s - ' % match['num_games'] blue_team = util_lib.Colorize('{:3}'.format(match['blue']), 'blue') red_team = util_lib.Colorize('{:3}'.format(match['red']), 'red') schedule.append('{} v {}: {}{}'.format(blue_team, red_team, num_games_str, date_time)) if len(schedule) >= num_games: break if not schedule: schedule = [messages.SCHEDULE_NO_GAMES_STRING] qualifier = 'No' return schedule[:num_games], qualifier
def GetResults(self, subcommand, num_games=5): """Get the results of past games for the specified region or team.""" qualifier = 'All' display_qualifier = 'All' is_team = False with self._lock: if subcommand in self.teams: qualifier = self.teams[subcommand].team_id display_qualifier = self.teams[subcommand].name is_team = True if subcommand in self.leagues: qualifier = self.leagues[subcommand].league_id display_qualifier = self.leagues[subcommand].name is_team = False results = [] # Shallow copy so we don't actually reverse the schedule tmp_schedule = self.schedule[:] tmp_schedule.reverse() for match in tmp_schedule: if self._ResultIsInteresting(match, qualifier): blue_team = self.MatchTeamName(match.blue) blue_team = util_lib.Colorize('{:3}'.format(blue_team), 'blue') red_team = self.MatchTeamName(match.red) red_team = util_lib.Colorize('{:3}'.format(red_team), 'red') is_tie = match.winner == 'TIE' if is_team: winner_msg = 'Tie' if is_tie else ( 'Won!' if match.winner == qualifier else 'Lost') else: winner_msg = '{} {}'.format( match.winner if is_tie else util_lib.Colorize( '{:3}'.format(self.teams[match.winner].abbreviation), 'red' if match.red == match.winner else 'blue'), ':^)' if is_tie else 'wins!') results.append('{} v {}: {}'.format(blue_team, red_team, winner_msg)) if len(results) >= num_games: break return results[:num_games], display_qualifier
def GetResults(self, subcommand, num_games=5): """Get the results of past games for the specified region or team.""" qualifier = 'All' is_team = False if subcommand in [t['acronym'] for t in self.teams.values()]: qualifier = subcommand is_team = True region_qualifier = self._AliasToRegion(subcommand) if region_qualifier: qualifier = region_qualifier is_team = False results = [] # Shallow copy so we don't actually reverse the schedule tmp_schedule = self.schedule[:] tmp_schedule.reverse() for match in tmp_schedule: if self._ResultIsInteresting(match, qualifier): blue_team = util_lib.Colorize('{:3}'.format(match['blue']), 'blue') red_team = util_lib.Colorize('{:3}'.format(match['red']), 'red') is_tie = match['winner'] == 'TIE' if is_team: winner_msg = 'Won!' if match[ 'winner'] == qualifier else 'Lost' else: winner_msg = '{} {}'.format( match['winner'] if is_tie else util_lib.Colorize( '{:3}'.format(match['winner']), 'red' if match['red'] == match['winner'] else 'blue'), ':^)' if is_tie else 'wins!') results.append('{} v {}: {}'.format(blue_team, red_team, winner_msg)) if len(results) >= num_games: break return results[:num_games], qualifier
def FormatBean(bean_data: coffee_pb2.Bean, uppercase: bool = False, count: int = 1) -> Text: """Given a bean, returns a pretty string for output.""" rarity = bean_data.rarity rarity_str = rarity if uppercase: rarity_str = rarity.title() if rarity in _RARITY_COLORS: rarity_str = util_lib.Colorize(rarity_str, _RARITY_COLORS[rarity], irc=False) count_str = '' if count > 1: count_str = '%d ' % count return '%s%s %s beans from %s' % (count_str, rarity_str, bean_data.variety, bean_data.region)
def _FormatTemp(self, temp_f, unit): color = '' if temp_f <= 10: color = 'cyan' elif temp_f <= 32: color = 'blue' elif temp_f >= 100: color = 'red' elif temp_f >= 80: color = 'orange' if unit == 'C': raw_str = '%.1f°C' % ((temp_f - 32) * 5 / 9) elif unit == 'K': raw_str = '%.1fK' % ((temp_f - 32) * 5 / 9 + 273.15) else: raw_str = '%.0f°F' % temp_f return util_lib.Colorize(raw_str, color, irc=False)