def apply_filter(self, resp, f: SubstitutionFilter) -> Any: available_filters: dict[str, Callable[[Any, List[str]], Any]] = { "strftime": _filter_strftime, "lower": lambda var, args: var.lower(), "upper": lambda var, args: var.upper(), "title": lambda var, args: var.title(), "capitalize": lambda var, args: var.capitalize(), "swapcase": lambda var, args: var.swapcase(), "time_since_minutes": lambda var, args: "no time" if var == 0 else utils.time_since(var * 60, 0, time_format="long"), "time_since": lambda var, args: "no time" if var == 0 else utils.time_since(var, 0, time_format="long"), "time_since_dt": _filter_time_since_dt, "timedelta_days": _filter_timedelta_days, "urlencode": _filter_urlencode, "join": _filter_join, "number_format": _filter_number_format, "add": _filter_add, "or_else": _filter_or_else, "or_broadcaster": self._filter_or_broadcaster, "or_streamer": self._filter_or_broadcaster, "slice": _filter_slice, "subtract": _filter_subtract, "multiply": _filter_multiply, "divide": _filter_divide, "floor": _filter_floor, "ceil": _filter_ceil, } if f.name in available_filters: return available_filters[f.name](resp, f.arguments) return resp
def apply_filter(self, resp, f): available_filters = { "strftime": _filter_strftime, "lower": lambda var, args: var.lower(), "upper": lambda var, args: var.upper(), "title": lambda var, args: var.title(), "capitalize": lambda var, args: var.capitalize(), "swapcase": lambda var, args: var.swapcase(), "time_since_minutes": lambda var, args: "no time" if var == 0 else utils.time_since(var * 60, 0, time_format="long"), "time_since": lambda var, args: "no time" if var == 0 else utils.time_since(var, 0, time_format="long"), "time_since_dt": _filter_time_since_dt, "urlencode": _filter_urlencode, "join": _filter_join, "number_format": _filter_number_format, "add": _filter_add, "or_else": _filter_or_else, "or_broadcaster": self._filter_or_broadcaster, "or_streamer": self._filter_or_broadcaster, "slice": _filter_slice, } if f.name in available_filters: return available_filters[f.name](resp, f.arguments) return resp
def apply_filter(self, resp, filter): available_filters = { 'strftime': _filter_strftime, 'lower': lambda var, args: var.lower(), 'upper': lambda var, args: var.upper(), 'time_since_minutes': lambda var, args: 'no time' if var == 0 else time_since(var * 60, 0, format='long'), 'time_since': lambda var, args: 'no time' if var == 0 else time_since(var, 0, format='long'), 'time_since_dt': _filter_time_since_dt, 'urlencode': _filter_urlencode, 'join': _filter_join, 'number_format': _filter_number_format, } if filter.name in available_filters: return available_filters[filter.name](resp, filter.arguments) return resp
def apply_filter(resp, f): available_filters = { "strftime": _filter_strftime, "lower": lambda var, args: var.lower(), "upper": lambda var, args: var.upper(), "time_since_minutes": lambda var, args: "no time" if var == 0 else time_since(var * 60, 0, time_format="long"), "time_since": lambda var, args: "no time" if var == 0 else time_since(var, 0, time_format="long"), "time_since_dt": _filter_time_since_dt, "urlencode": _filter_urlencode, "join": _filter_join, "number_format": _filter_number_format, "add": _filter_add, } if f.name in available_filters: return available_filters[f.name](resp, f.arguments) return resp
def c_status_length(self): if self.stream_manager.online: return time_since(time.time(), self.stream_manager.current_stream.stream_start.timestamp()) else: if self.stream_manager.last_stream is not None: return time_since(time.time(), self.stream_manager.last_stream.stream_end.timestamp()) else: return 'No recorded stream FeelsBadMan '
def get_last_tweet(self, username): if self.twitter_client: try: public_tweets = self.twitter_client.user_timeline(username) for tweet in public_tweets: if not tweet.text.startswith( "RT ") and tweet.in_reply_to_screen_name is None: tw = tweet_prettify_urls(tweet) # Tweepy returns naive datetime object (but it's always UTC) # .replace() makes it timezone-aware :) created_at = tweet.created_at.replace( tzinfo=datetime.timezone.utc) return "{0} ({1} ago)".format( tw.replace("\n", " "), time_since(utils.now().timestamp(), created_at.timestamp(), time_format="short"), ) except Exception: log.exception("Exception caught while getting last tweet") return "FeelsBadMan" else: return "Twitter not set up FeelsBadMan" return "FeelsBadMan"
def check_follow_age(self, bot, source, username, streamer, event): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) is_self = source.username == username message = '' if age: # Following human_age = time_since( datetime.datetime.now().timestamp() - age.timestamp(), 0) suffix = 'been following {} for {}'.format(streamer, human_age) if is_self: message = 'You have ' + suffix else: message = username + ' has ' + suffix else: # Not following suffix = 'not following {}'.format(streamer) if is_self: message = 'You are ' + suffix else: message = username + ' is ' + suffix bot.send_message_to_user(source, message, event, method=self.settings['action_followage'])
def _filter_time_since_dt(var, args): try: ts = time_since(datetime.datetime.now().timestamp(), var.timestamp()) if len(ts) > 0: return ts else: return '0 seconds' except: return 'never FeelsBadMan ?'
def _filter_time_until_dt(var: Any, args: List[str]) -> Any: try: ts = utils.time_since(var.timestamp(), utils.now().timestamp()) if ts: return ts return "0 seconds" except: return "never FeelsBadMan ?"
def _filter_time_since_dt(var, args): try: ts = utils.time_since(utils.now().timestamp(), var.timestamp()) if ts: return ts return "0 seconds" except: return "never FeelsBadMan ?"
def top_watchers(self, **options): bot = options["bot"] data = [] with DBManager.create_session_scope() as db_session: for user in db_session.query(User).order_by(User.minutes_in_chat_online.desc())[: self.settings["num_top"]]: data.append( "{user.username_raw} ({time_spent})".format( user=user, time_spent=time_since(user.minutes_in_chat_online * 60, 0, time_format="short") ) ) bot.say("Top {num_top} watchers: {data}".format(num_top=self.settings["num_top"], data=", ".join(data)))
def check_follow_age(self, bot, source, username, streamer, event): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) message = '' if age: # Following human_age = time_since(datetime.datetime.now().timestamp() - age.timestamp(), 0) message = '{} has been following {} for {}'.format(username, streamer, human_age) else: # Not following message = '{} is not following {}'.format(username, streamer) bot.send_message_to_user(source, message, event, method=self.settings['action_followage'])
def check_follow_age(self, bot, source, username, streamer): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) if source.username == username: if age is False: bot.say('{}, you are not following {}'.format( source.username_raw, streamer)) else: bot.say('{}, you have been following {} for {}'.format( source.username_raw, streamer, time_since( datetime.datetime.now().timestamp() - age.timestamp(), 0))) else: if age is False: bot.say('{}, {} is not following {}'.format( source.username_raw, username, streamer)) else: bot.say('{}, {} has been following {} for {}'.format( source.username_raw, username, streamer, time_since( datetime.datetime.now().timestamp() - age.timestamp(), 0)))
def get_last_tweet(self, username): if self.twitter_client: try: public_tweets = self.twitter_client.user_timeline(username) for tweet in public_tweets: if not tweet.text.startswith('RT ') and tweet.in_reply_to_screen_name is None: tw = tweet_prettify_urls(tweet) return '{0} ({1} ago)'.format(tw.replace('\n', ' '), time_since(datetime.now().timestamp(), tweet.created_at.timestamp(), format='short')) except Exception: log.exception('Exception caught while getting last tweet') return 'FeelsBadMan' else: return 'Twitter not set up FeelsBadMan' return 'FeelsBadMan'
def check_follow_age(self, bot, source, username, streamer, event): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) is_self = source.username == username message = "" streamer = streamer.replace('admiralbulldog', 'Buldog') if age: # Following human_age = time_since(utils.now().timestamp() - age.timestamp(), 0) message = "{} has been following {} for {}".format(username, streamer, human_age) else: # Not following message = "{} is not following {}".format(username, streamer) bot.send_message_to_user(source, message, event, method=self.settings["action_followage"])
def top_offline(self, **options): bot = options['bot'] data = [] with DBManager.create_session_scope() as db_session: for user in db_session.query(User).order_by( User.minutes_in_chat_offline.desc())[:self. settings['num_top']]: data.append('{user.username_raw} ({time_spent})'.format( user=user, time_spent=time_since(user.minutes_in_chat_offline * 60, 0, format='short'))) bot.say('Top {num_top} offliners: {data}'.format( num_top=self.settings['num_top'], data=', '.join(data)))
def check_follow_age(self, bot, source, username, streamer): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) if source.username == username: if age is False: bot.say('{}, you are not following {}'.format(source.username_raw, streamer)) else: bot.say('{}, you have been following {} for {}'.format(source.username_raw, streamer, time_since(datetime.datetime.now().timestamp() - age.timestamp(), 0))) else: if age is False: bot.say('{}, {} is not following {}'.format(source.username_raw, username, streamer)) else: bot.say('{}, {} has been following {} for {}'.format( source.username_raw, username, streamer, time_since(datetime.datetime.now().timestamp() - age.timestamp(), 0)))
def check_follow_age(self, bot, source, username, streamer): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) is_self = source.username == username if age: # Following human_age = time_since(datetime.datetime.now().timestamp() - age.timestamp(), 0) suffix = 'been following {} for {}'.format(streamer, human_age) if is_self: bot.send_message_to_user(source, 'You have ' + suffix, method=self.settings['action_followage']) else: bot.send_message_to_user(source, username + ' has ' + suffix, method=self.settings['action_followage']) else: # Not following suffix = 'not following {}'.format(streamer) if is_self: bot.send_message_to_user(source, 'You are ' + suffix, method=self.settings['action_followage']) else: bot.send_message_to_user(source, username + ' is ' + suffix, method=self.settings['action_followage'])
def get_last_tweet(self, username): if self.twitter_client: try: public_tweets = self.twitter_client.user_timeline(username) for tweet in public_tweets: if not tweet.text.startswith( 'RT ') and tweet.in_reply_to_screen_name is None: tw = tweet_prettify_urls(tweet) return '{0} ({1} ago)'.format( tw.replace('\n', ' '), time_since(datetime.now().timestamp(), tweet.created_at.timestamp(), format='short')) except Exception: log.exception('Exception caught while getting last tweet') return 'FeelsBadMan' else: return 'Twitter not set up FeelsBadMan' return 'FeelsBadMan'
def cmd_puptime(self, **options): bot = options['bot'] source = options['source'] viewer_data = bot.stream_manager.get_viewer_data() if viewer_data is False: bot.say('{}, the stream is offline.'.format(source.username_raw)) return minutes_watched = viewer_data.get(source.username, None) if minutes_watched is None: bot.say( '{}, You haven\'t been registered watching the stream yet WutFace' .format(source.username_raw)) else: minutes_watched = int(minutes_watched) * 60 log.info(minutes_watched) bot.say('{}, You have been watching the stream for ~{}'.format( source.username_raw, time_since(minutes_watched, 0)))
def check_follow_age(self, bot, source, username, streamer, event): streamer = bot.streamer if streamer is None else streamer.lower() age = bot.twitchapi.get_follow_relationship(username, streamer) is_self = source.username == username message = "" if age: # Following human_age = time_since(utils.now().timestamp() - age.timestamp(), 0) suffix = "been following {} for {}".format(streamer, human_age) if is_self: message = "You have " + suffix else: message = username + " has " + suffix else: # Not following suffix = "not following {}".format(streamer) if is_self: message = "You are " + suffix else: message = username + " is " + suffix bot.send_message_to_user(source, message, event, method=self.settings["action_followage"])
def check_follow_age(self, bot, source, username, streamer, event): streamer = bot.streamer if streamer is None else streamer.lower() user_id = self.bot.twitch_helix_api.get_user_id(username) streamer_id = self.bot.twitch_helix_api.get_user_id(streamer) if user_id is None or streamer_id is None: # one of the users doesn't exist follow_since = None else: follow_since = bot.twitch_helix_api.get_follow_since(user_id, streamer_id) message = "" streamer = streamer.replace("admiralbulldog", "Buldog") if follow_since: # Following human_age = time_since(utils.now().timestamp() - follow_since.timestamp(), 0) message = "{} has been following {} for {}".format(username, streamer, human_age) else: # Not following message = "{} is not following {}".format(username, streamer) bot.send_message_to_user(source, message, event, method=self.settings["action_followage"])
def time_ago_timespan_seconds(t, format='long'): v = time_since(t, 0, format) return 'None' if len(v) == 0 else v
def time_ago(t, format='long'): return time_since(datetime.datetime.now().timestamp(), t.timestamp(), format)
def time_diff(t1, t2, format='long'): return time_since(t1.timestamp(), t2.timestamp(), format)
def c_uptime(self): return time_since(datetime.datetime.now().timestamp(), self.start_time.timestamp())
def cmd_puptime(self, **options): bot = options['bot'] source = options['source'] viewer_data = bot.stream_manager.get_viewer_data() if viewer_data is False: bot.say('{}, the stream is offline.'.format(source.username_raw)) return minutes_watched = viewer_data.get(source.username, None) if minutes_watched is None: bot.say('{}, You haven\'t been registered watching the stream yet WutFace'.format(source.username_raw)) else: minutes_watched = int(minutes_watched) * 60 log.info(minutes_watched) bot.say('{}, You have been watching the stream for ~{}'.format(source.username_raw, time_since(minutes_watched, 0)))
def on_pong(self, chatconn, event): # self.say('Received a pong. Last pong received {} ago'.format(time_since(datetime.datetime.now().timestamp(), self.last_pong.timestamp()))) log.info('Received a pong. Last pong received {} ago'.format( time_since(datetime.datetime.now().timestamp(), self.last_pong.timestamp()))) self.last_pong = datetime.datetime.now()
def on_pong(self, chatconn, event): # self.say('Received a pong. Last pong received {} ago'.format(time_since(datetime.datetime.now().timestamp(), self.last_pong.timestamp()))) log.info('Received a pong. Last pong received {} ago'.format(time_since(datetime.datetime.now().timestamp(), self.last_pong.timestamp()))) self.last_pong = datetime.datetime.now()
def _format_for_follow_age(follow_since): human_age = time_since( utils.now().timestamp() - follow_since.timestamp(), 0) return f"for {human_age}"