# Path: bot/commands/abstract/command.py
# class Command(ABC):
#     """Represents a command, a way of reacting to chat messages."""
# 
#     perm = Permission.Admin
# 
#     def __init__(self, bot):
#         """Initialize the command."""
#         pass
# 
#     def match(self, bot, user, msg, tag_info):
#         """Return whether this command should be run."""
#         return False
# 
#     def run(self, bot, user, msg, tag_info):
#         """Run this command."""
#         pass
# 
#     def close(self, bot):
#         """Clean up."""
#         pass
# 
# Path: bot/utilities/tools.py
# def emote_list_to_string(emote_list):
#     """Convert an EmoteList to a string."""
#     # Use string.join to glue string of emotes in emoteList
#     separator = " "
#     return separator.join(emote_list)
# 
# def twitch_time_to_datetime(twitch_time):
#     """Convert Twitch time string to datetime object.
# 
#     E.g.: 2017-09-08T22:35:33.449961Z
#     """
#     for ch in ["-", "T", "Z", ":"]:
#         twitch_time = twitch_time.replace(ch, "")
# 
#     return datetime.strptime(twitch_time, "%Y%m%d%H%M%S")
# 
# Path: bot/utilities/tools.py
# def replace_vars(msg, args):
#     """Replace the variables in the message."""
#     oldmsg = msg
#     newmsg = msg
# 
#     for key in args:
#         newmsg = newmsg.replace(key, str(args[key]))
#         """Check if something was replaced, otherwise something went wrong."""
#         if newmsg is oldmsg:
#             print("ERROR: Could not replace variable in string!")
# 
#         oldmsg = newmsg
#     return newmsg


from datetime import datetime
from bot.commands.abstract.command import Command
from bot.utilities.permission import Permission
from bot.utilities.tools import emote_list_to_string, twitch_time_to_datetime
from bot.utilities.tools import replace_vars

    def run(self, bot, user, msg, tag_info):
        """Get stream object and return requested information."""
        responses = bot.config.responses["StreamInfo"]
        stream = bot.twitch.get_stream(bot.config.channelID)
        cmd = msg.lower()

        if cmd.startswith("!bttv"):
            var = {
                "<MULTIEMOTES>": emote_list_to_string(
                    bot.emotes.get_channel_bttv_emotes()
                )
            }
            bot.write(replace_vars(responses["bttv_msg"]["msg"], var))
        elif stream["stream"] is None:
            bot.write(responses["stream_off"]["msg"])
        elif cmd.startswith("!fps"):
            fps = format(stream["stream"]["average_fps"], ".2f")
            var = {"<FPS>": fps}
            bot.write(replace_vars(responses["fps_msg"]["msg"], var))
        elif cmd.startswith("!uptime"):
            created_at = stream["stream"]["created_at"]
            streamstart = twitch_time_to_datetime(created_at)
            now = datetime.utcnow()
            elapsed_time = now - streamstart
            seconds = int(elapsed_time.total_seconds())
            hours = seconds // 3600
            minutes = (seconds % 3600) // 60
            seconds = seconds % 60
            var = {"<HOURS>": hours, "<MINUTES>": minutes, "<SECONDS>": seconds}

# Path: bot/commands/abstract/command.py
# class Command(ABC):
#     """Represents a command, a way of reacting to chat messages."""
# 
#     perm = Permission.Admin
# 
#     def __init__(self, bot):
#         """Initialize the command."""
#         pass
# 
#     def match(self, bot, user, msg, tag_info):
#         """Return whether this command should be run."""
#         return False
# 
#     def run(self, bot, user, msg, tag_info):
#         """Run this command."""
#         pass
# 
#     def close(self, bot):
#         """Clean up."""
#         pass
# 
# Path: bot/utilities/tools.py
# def emote_list_to_string(emote_list):
#     """Convert an EmoteList to a string."""
#     # Use string.join to glue string of emotes in emoteList
#     separator = " "
#     return separator.join(emote_list)
# 
# def twitch_time_to_datetime(twitch_time):
#     """Convert Twitch time string to datetime object.
# 
#     E.g.: 2017-09-08T22:35:33.449961Z
#     """
#     for ch in ["-", "T", "Z", ":"]:
#         twitch_time = twitch_time.replace(ch, "")
# 
#     return datetime.strptime(twitch_time, "%Y%m%d%H%M%S")
# 
# Path: bot/utilities/tools.py
# def replace_vars(msg, args):
#     """Replace the variables in the message."""
#     oldmsg = msg
#     newmsg = msg
# 
#     for key in args:
#         newmsg = newmsg.replace(key, str(args[key]))
#         """Check if something was replaced, otherwise something went wrong."""
#         if newmsg is oldmsg:
#             print("ERROR: Could not replace variable in string!")
# 
#         oldmsg = newmsg
#     return newmsg


# Path: bot/commands/streaminfo.py
from datetime import datetime
from bot.commands.abstract.command import Command
from bot.utilities.permission import Permission
from bot.utilities.tools import emote_list_to_string, twitch_time_to_datetime
from bot.utilities.tools import replace_vars


    def run(self, bot, user, msg, tag_info):
        """Get stream object and return requested information."""
        responses = bot.config.responses["StreamInfo"]
        stream = bot.twitch.get_stream(bot.config.channelID)
        cmd = msg.lower()

        if cmd.startswith("!bttv"):
            var = {
                "<MULTIEMOTES>": emote_list_to_string(
                    bot.emotes.get_channel_bttv_emotes()
                )
            }
            bot.write(replace_vars(responses["bttv_msg"]["msg"], var))
        elif stream["stream"] is None:
            bot.write(responses["stream_off"]["msg"])
        elif cmd.startswith("!fps"):
            fps = format(stream["stream"]["average_fps"], ".2f")
            var = {"<FPS>": fps}
            bot.write(replace_vars(responses["fps_msg"]["msg"], var))
        elif cmd.startswith("!uptime"):
            created_at = stream["stream"]["created_at"]
            streamstart = twitch_time_to_datetime(created_at)
            now = datetime.utcnow()
            elapsed_time = now - streamstart
            seconds = int(elapsed_time.total_seconds())
            hours = seconds // 3600
            minutes = (seconds % 3600) // 60
            seconds = seconds % 60
            var = {"<HOURS>": hours, "<MINUTES>": minutes, "<SECONDS>": seconds}

            bot.write(replace_vars(responses["uptime"]["msg"], var))