def live(lrrbot, conn, event, respond_to): """ Command: !live Post the currenly live fanstreamers. """ users = lrrbot.metadata.tables["users"] with lrrbot.engine.begin() as pg_conn: token, = pg_conn.execute( sqlalchemy.select([ users.c.twitch_oauth ]).where(users.c.name == config["username"])).first() try: yield from extract_new_channels(lrrbot.loop, token) except urllib.error.HTTPError: pass streams = yield from twitch.get_streams_followed(token) if streams == []: return conn.privmsg(respond_to, "No fanstreamers currently live.") streams.sort(key=lambda e: e["channel"]["display_name"]) tag = "Currently live fanstreamers: " # Full message message = tag + ", ".join([ "%s (%s%s)%s%s" % (data["channel"]["display_name"], data["channel"]["url"], space.SPACE, " is playing %s" % data["game"] if data.get("game") is not None else "", " (%s)" % data["channel"]["status"] if data["channel"].get("status") not in [None, ""] else "") for data in streams ]) if len(message) <= 450: return conn.privmsg(respond_to, message) # Shorter message message = tag + ", ".join([ "%s (%s%s)%s" % ( data["channel"]["display_name"], data["channel"]["url"], space.SPACE, " is playing %s" % data["game"] if data.get("game") is not None else "", ) for data in streams ]) if len(message) <= 450: return conn.privmsg(respond_to, message) # Shortest message message = tag + ", ".join([ "%s (%s%s)" % (data["channel"]["display_name"], data["channel"]["url"], space.SPACE) for data in streams ]) return conn.privmsg(respond_to, utils.shorten(message, 450))
def live(lrrbot, conn, event, respond_to): """ Command: !live Post the currenly live fanstreamers. """ users = lrrbot.metadata.tables["users"] with lrrbot.engine.begin() as pg_conn: token, = pg_conn.execute(sqlalchemy.select([users.c.twitch_oauth]) .where(users.c.name == config["username"])).first() try: yield from extract_new_channels(lrrbot.loop, token) except urllib.error.HTTPError: pass streams = yield from twitch.get_streams_followed(token) if streams == []: return conn.privmsg(respond_to, "No fanstreamers currently live.") streams.sort(key=lambda e: e["channel"]["display_name"]) tag = "Currently live fanstreamers: " # Full message message = tag + ", ".join([ "%s (%s%s)%s%s" % ( data["channel"]["display_name"], data["channel"]["url"], space.SPACE, " is playing %s" % data["game"] if data.get("game") is not None else "", " (%s)" % data["channel"]["status"] if data["channel"].get("status") not in [None, ""] else "" ) for data in streams ]) if len(message) <= 450: return conn.privmsg(respond_to, message) # Shorter message message = tag + ", ".join([ "%s (%s%s)%s" % ( data["channel"]["display_name"], data["channel"]["url"], space.SPACE, " is playing %s" % data["game"] if data.get("game") is not None else "", ) for data in streams ]) if len(message) <= 450: return conn.privmsg(respond_to, message) # Shortest message message = tag + ", ".join([ "%s (%s%s)" % ( data["channel"]["display_name"], data["channel"]["url"], space.SPACE ) for data in streams ]) return conn.privmsg(respond_to, utils.shorten(message, 450))
def live(lrrbot, conn, event, respond_to): """ Command: !live Post the currenly live fanstreamers. """ try: yield from extract_new_channels(lrrbot.loop) except urllib.error.HTTPError: pass streams = yield from twitch.get_streams_followed() if streams == []: return conn.privmsg(respond_to, "No fanstreamers currently live.") streams.sort(key=lambda e: e["channel"]["display_name"]) tag = "Currently live fanstreamers: " # Full message message = tag + ", ".join( [ "%s (%s)%s%s" % ( data["channel"]["display_name"], data["channel"]["url"], " is playing %s" % data["game"] if data.get("game") is not None else "", " (%s)" % data["channel"]["status"] if data["channel"].get("status") not in [None, ""] else "", ) for data in streams ] ) if len(message) <= 450: return conn.privmsg(respond_to, message) # Shorter message message = tag + ", ".join( [ "%s (%s)%s" % ( data["channel"]["display_name"], data["channel"]["url"], " is playing %s" % data["game"] if data.get("game") is not None else "", ) for data in streams ] ) if len(message) <= 450: return conn.privmsg(respond_to, message) # Shortest message message = tag + ", ".join( ["%s (%s)" % (data["channel"]["display_name"], data["channel"]["url"]) for data in streams] ) return conn.privmsg(respond_to, utils.shorten(message, 450))
def get_next_event_text(calendar, after=None, include_current=None, tz=None, verbose=True): """ Build the actual human-readable response to the !next command. The tz parameter can override the timezone used to display the event times. This can be an actual timezone object, or a string timezone name. Defaults to moonbase time. """ if after is None: after = datetime.datetime.now(datetime.timezone.utc) if not tz: tz = config['timezone'] elif isinstance(tz, str): tz = tz.strip() try: tz = common.time.get_timezone(tz) except pytz.exceptions.UnknownTimeZoneError: return "Unknown timezone: %s" % tz events = get_next_event(calendar, after=after, include_current=include_current) if not events: return "There don't seem to be any upcoming scheduled streams" strs = [] for i, ev in enumerate(events): if ev['location'] is not None: title = "%(title)s (%(location)s%(space)s)" % { "title": ev["title"], "location": ev["location"], "space": space.SPACE, } else: title = ev['title'] # If several events are at the same time, just show the time once after all of them if i == len(events) - 1 or ev['start'] != events[i+1]['start']: if verbose: if ev['start'] < after: nice_duration = common.time.nice_duration(after - ev['start'], 1) + " ago" else: nice_duration = common.time.nice_duration(ev['start'] - after, 1) + " from now" strs.append("%s at %s (%s)" % (title, ev['start'].astimezone(tz).strftime(DISPLAY_FORMAT), nice_duration)) else: strs.append("%s at %s" % (ev['title'], ev['start'].astimezone(tz).strftime(DISPLAY_FORMAT))) else: strs.append(title if verbose else ev['title']) response = ', '.join(strs) if verbose: if calendar == CALENDAR_LRL: response = "Next scheduled stream: %s." % response elif calendar == CALENDAR_FAN: response = "Next scheduled fan stream: %s." % response return utils.shorten(response, 450) # For safety
def check_polls(lrrbot, conn): now = time.time() for end, title, poll_id, respond_to in lrrbot.polls: if end < now: url = "https://www.strawpoll.me/api/v2/polls/%s" % poll_id data = json.loads(common.http.request(url)) options = sorted(zip(data["options"], data["votes"]), key=lambda e: (e[1], random.random()), reverse=True) options = "; ".join(map(strawpoll_format, enumerate(options))) response = "Poll complete: %s: %s" % (html.unescape(data["title"]), options) response = utils.shorten(response, 450) conn.privmsg(respond_to, response) lrrbot.polls = list(filter(lambda e: e[0] >= now, lrrbot.polls))
def check_polls(lrrbot, conn): now = time.time() for end, title, poll_id, respond_to in lrrbot.polls: if end < now: url = "https://strawpoll.me/api/v2/polls/%s" % poll_id data = json.loads(utils.http_request(url)) options = sorted(zip(data["options"], data["votes"]), key=lambda e: (e[1], random.random()), reverse=True) options = "; ".join(map(strawpoll_format, enumerate(options))) response = "Poll complete: %s: %s" % (data["title"], options) response = utils.shorten(response, 450) conn.privmsg(respond_to, response) lrrbot.polls = list(filter(lambda e: e[0] >= now, lrrbot.polls))
def polls(lrrbot, conn, event, respond_to): """ Command: !polls Section: misc List all currently active polls. """ if lrrbot.polls == []: return conn.privmsg(respond_to, "No active polls.") now = time.time() messages = [] for end, title, poll_id, respond_to in lrrbot.polls: messages += ["%s (https://www.strawpoll.me/%s%s): %s from now" % (title, poll_id, space.SPACE, common.time.nice_duration(end - now, 1))] conn.privmsg(respond_to, utils.shorten("Active polls: "+"; ".join(messages), 450))
def polls(lrrbot, conn, event, respond_to): """ Command: !polls Section: misc List all currently active polls. """ if lrrbot.polls == []: return conn.privmsg(respond_to, "No active polls.") now = time.time() messages = [] for end, title, poll_id, respond_to in lrrbot.polls: messages += ["%s (https://strawpoll.me/%s): %s from now" % (title, poll_id, utils.nice_duration(end - now, 1))] conn.privmsg(respond_to, utils.shorten("Active polls: " + "; ".join(messages), 450))
def get_next_event_text(calendar, after=None, include_current=None, tz=None, verbose=True): """ Build the actual human-readable response to the !next command. The tz parameter can override the timezone used to display the event times. This can be an actual timezone object, or a string timezone name. Defaults to moonbase time. """ if after is None: after = datetime.datetime.now(datetime.timezone.utc) if not tz: tz = config['timezone'] elif isinstance(tz, str): tz = tz.strip() try: tz = common.time.get_timezone(tz) except pytz.exceptions.UnknownTimeZoneError: return "Unknown timezone: %s" % tz events = get_next_event(calendar, after=after, include_current=include_current) if not events: return "There don't seem to be any upcoming scheduled streams" concise_strs = [] verbose_strs = [] for i, ev in enumerate(events): title = ev['title'] if ev['location'] is not None: title += " (%(location)s%(space)s)" % { "location": ev["location"], "space": space.SPACE, } concise_title = title if ev['description'] is not None: title += " (%(description)s)" % { "description": utils.shorten(ev['description'], 200), } # If several events are at the same time, just show the time once after all of them if i == len(events) - 1 or ev['start'] != events[i+1]['start']: if verbose: if ev['start'] < after: nice_duration = common.time.nice_duration(after - ev['start'], 1) + " ago" else: nice_duration = common.time.nice_duration(ev['start'] - after, 1) + " from now" start = ev['start'].astimezone(tz).strftime(DISPLAY_FORMAT) concise_strs.append("%s at %s (%s)" % (concise_title, start, nice_duration)) verbose_strs.append("%s at %s (%s)" % (title, start, nice_duration)) else: concise_strs.append("%s at %s" % (ev['title'], ev['start'].astimezone(tz).strftime(DISPLAY_FORMAT))) else: concise_strs.append(concise_title if verbose else ev['title']) verbose_strs.append(title) if verbose: for strs in [verbose_strs, concise_strs]: if calendar == CALENDAR_LRL: response = "Next scheduled stream: %s." % ", ".join(strs) elif calendar == CALENDAR_FAN: response = "Next scheduled fan stream: %s." % ", ".join(strs) if len(response) <= 450: break else: response = ", ".join(concise_strs) return utils.shorten(response, 450) # For safety