Exemplo n.º 1
0
async def format_stream(member: discord.Member, score: dict, beatmap: dict):
    """ Format the stream url and a VOD button when possible. """
    stream_url = getattr(member.game, "url", None)
    if not stream_url:
        return ""

    # Add the stream url and return immediately if twitch is not setup
    text = "**Watch live @** <{}>".format(stream_url)
    if not twitch.client_id:
        return text + "\n"

    # Try getting the vod information of the current stream
    try:
        twitch_id = await twitch.get_id(member)
        vod_request = await twitch.request(
            "channels/{}/videos".format(twitch_id),
            limit=1,
            broadcast_type="archive",
            sort="time")
        assert vod_request["_total"] >= 1
    except:
        print_exc()
        return text + "\n"

    vod = vod_request["videos"][0]

    # Find the timestamp of where the play would have started without pausing the game
    score_created = datetime.strptime(score["date"], "%Y-%m-%d %H:%M:%S")
    vod_created = datetime.strptime(
        vod["created_at"], "%Y-%m-%dT%H:%M:%SZ") + timedelta(hours=8)  # UTC-8
    beatmap_length = int(beatmap["total_length"])

    # Convert beatmap length when speed mods are enabled
    mods = Mods.list_mods(int(score["enabled_mods"]))
    if Mods.DT in mods or Mods.NC in mods:
        beatmap_length /= 1.5
    elif Mods.HT in mods:
        beatmap_length /= 0.75

    # Get the timestamp in the VOD when the score was created
    timestamp_score_created = (score_created - vod_created).total_seconds()
    timestamp_play_started = timestamp_score_created - beatmap_length

    # Add the vod url with timestamp to the formatted text
    text += " | **[`Video of this play :)`]({0}?t={1}s)**\n".format(
        vod["url"], int(timestamp_play_started))
    return text