Esempio n. 1
0
def call(salty_inst, c_msg, **kwargs):
    channel = salty_inst.channel
    success, response = salty_inst.srl_api.get_races(**kwargs)
    if not success:
        return False, \
            "Error retrieving data for SRL API ({0}).".format(response.status_code)

    races = response["races"]
    for i in races:
        if channel in [x["twitch"].lower() for x in i["entrants"].values()]:
            race_channel = i
            break
    else:
        return False, "User not currently in a race."

    entrants = []
    for k, v in race_channel["entrants"].iteritems():
        if salty_inst.channel == v["twitch"].lower():
            real_nick = k
        if v["statetext"] == "Ready" or v["statetext"] == "Entered":
            if v["twitch"] != "":
                entrants.append(k)

    user_place = race_channel["entrants"][real_nick]["place"]
    user_time = race_channel["entrants"][real_nick]["time"]
    race_status = race_channel["statetext"]
    race_time = race_channel["time"]
    race_link = "http://speedrunslive.com/race/?id={}".format(race_channel["id"])

    success, response = salty_inst.twitch_api.get_streams(entrants)
    if success:
        live_entrants = [x["channel"]["name"] for x in response["streams"]]
    else:
        live_entrants = []

    send_msg = "Game: {0}, Category: {1}, Status: {2}".format(
        race_channel["game"]["name"], race_channel["goal"], race_status)
    if race_time > 0:
        if user_time > 0:
            time_formatted = time_formatter.format_time(user_time)
            send_msg += ", Finished {0}{1} with a time of {2}".format(
                user_place, get_suffix.suffix(user_place), time_formatted)
        else:
            time_formatted = time_formatter.format_time(time.time() - race_time)
            send_msg += ", RaceBot Time: {0}".format(time_formatted)
    if race_status == "Complete":
        send_msg += ". {}".format(race_link)
    elif len(live_entrants) <= 6 and len(live_entrants) > 1:
        multi_link = "http://kadgar.net/live/{0}".format("/".join(live_entrants))
        send_msg += ". {0}".format(multi_link)
    else:
        send_msg += ". {}".format(race_link)
    return True, send_msg
Esempio n. 2
0
def call(salty_inst, c_msg, **kwargs):
    channel = salty_inst.channel
    success, response = salty_inst.srl_api.get_races(**kwargs)
    if not success:
        return False, \
            "Error retrieving data for SRL API ({0}).".format(response.status_code)

    races = response["races"]
    for i in races:
        if channel in [x["twitch"].lower() for x in list(i["entrants"].values())]:
            race_channel = i
            break
    else:
        return False, "User not currently in a race."

    entrants = []
    for k, v in race_channel["entrants"].items():
        if salty_inst.channel == v["twitch"].lower():
            real_nick = k
        if v["statetext"] == "Ready" or v["statetext"] == "Entered":
            if v["twitch"] != "":
                entrants.append(k)

    user_place = race_channel["entrants"][real_nick]["place"]
    user_time = race_channel["entrants"][real_nick]["time"]
    race_status = race_channel["statetext"]
    race_time = race_channel["time"]
    race_link = "http://speedrunslive.com/race/?id={}".format(race_channel["id"])

    success, response = salty_inst.twitch_api.get_streams(entrants)
    if success:
        live_entrants = [x["channel"]["name"] for x in response["streams"]]
    else:
        live_entrants = []

    send_msg = "Game: {0}, Category: {1}, Status: {2}".format(
        race_channel["game"]["name"], race_channel["goal"], race_status)
    if race_time > 0:
        if user_time > 0:
            time_formatted = time_formatter.format_time(user_time)
            send_msg += ", Finished {0}{1} with a time of {2}".format(
                user_place, get_suffix.suffix(user_place), time_formatted)
        else:
            time_formatted = time_formatter.format_time(time.time() - race_time)
            send_msg += ", RaceBot Time: {0}".format(time_formatted)
    if race_status == "Complete":
        send_msg += ". {}".format(race_link)
    elif len(live_entrants) <= 6 and len(live_entrants) > 1:
        multi_link = "http://kadgar.net/live/{0}".format("/".join(live_entrants))
        send_msg += ". {0}".format(multi_link)
    else:
        send_msg += ". {}".format(race_link)
    return True, send_msg
Esempio n. 3
0
def call(salty_inst, c_msg, balancer, **kwargs):
    video_ids = re.findall(regexes.YOUTUBE_URL, c_msg["message"])
    if not video_ids:
        return False, "No video ids"
    seen_ids = set()
    seen_add = seen_ids.add
    video_ids = [x for x in video_ids if not (x in seen_ids or seen_add(x))]
    parts = ["snippet", "statistics", "contentDetails"]
    final_list = []
    success, response = salty_inst.youtube_api.get_videos(video_ids, parts, **kwargs)
    if not success:
        return False, \
            "Error retrieving info from youtube API ({0})".format(response.status_code)

    if len(response["items"]) == 0:
        return False, "No valid ID's found."

    for i in response["items"]:
        final_list.append("[{0}] {1} uploaded by {2}. Views: {3}".format(
            time_formatter.format_time(isodate.parse_duration(i["contentDetails"]["duration"]).seconds),
            i["snippet"]["title"],
            i["snippet"]["channelTitle"],
            i["statistics"]["viewCount"]
        ))
    return True, " | ".join(final_list)
Esempio n. 4
0
def call(salty_inst, c_msg, **kwargs):
    msg_split = c_msg["message"].split(" ", 3)

    infer_category = False
    try:
        username = msg_split[1]
    except IndexError:
        username = salty_inst.channel
    try:
        game = msg_split[2]
    except IndexError:
        game = salty_inst.game
    try:
        category = msg_split[3]
    except IndexError:
        category = salty_inst.title
        infer_category = True

    success, response = salty_inst.splits_io_api.get_user_pbs(
        username, **kwargs)
    if not success:
        return False, \
            "Error retrieving info from splits.io ({0}).".format(response.status_code)
    game_pbs = []
    for i in response["pbs"]:
        if not i["game"]:
            continue
        if (i["game"]["name"] or "").lower() == game.lower():
            game_pbs.append(i)
        if (i["game"]["shortname"] or "").lower() == game.lower():
            game_pbs.append(i)
    game_categories = {
        x["category"]["name"]: x["category"]["name"]
        for x in game_pbs if x["category"]
    }

    if infer_category:
        category_finder = get_category_title.find_active_cat
    else:
        category_finder = get_category_string.find_active_cat

    cat_success, cat_response = category_finder(game_categories, category)
    if not cat_success:
        return False, cat_response

    pb_splits = [
        x for x in game_pbs
        if x["category"]["name"].lower() == cat_response.lower()
    ][0]
    output_game = pb_splits["game"]["name"]
    msg = "{0}'s best splits for {1} {2} is {3} https://splits.io{4}".format(
        username.capitalize(), output_game, cat_response,
        time_formatter.format_time(pb_splits["time"]), pb_splits["path"])
    return True, msg
Esempio n. 5
0
def call(salty_inst, c_msg, **kwargs):
    msg_split = c_msg["message"].split(" ", 3)


    infer_category = False
    try:
        username = msg_split[1]
    except IndexError:
        username = salty_inst.channel
    try:
        game = msg_split[2]
    except IndexError:
        game = salty_inst.game
    try:
        category = msg_split[3]
    except IndexError:
        category = salty_inst.title
        infer_category = True

    success, response = salty_inst.splits_io_api.get_user_pbs(username, **kwargs)
    if not success:
        return False, \
            "Error retrieving info from splits.io ({0}).".format(response.status_code)
    game_pbs = []
    for i in response["pbs"]:
        if not i["game"]:
            continue
        if (i["game"]["name"] or "").lower() == game:
            game_pbs.append(i)
        if (i["game"]["shortname"] or "").lower() == game:
            game_pbs.append(i)
    game_categories = {x["category"]["name"] : x["category"]["name"] for x in game_pbs if x["category"]}

    if infer_category:
        category_finder = get_category_title.find_active_cat
    else:
        category_finder = get_category_string.find_active_cat

    cat_success, cat_response = category_finder(game_categories, category)
    if not cat_success:
        return False, cat_response

    pb_splits = [x for x in game_pbs if x["category"]["name"].lower() == cat_response.lower()][0]
    output_game = pb_splits["game"]["name"]
    msg = "{0}'s best splits for {1} {2} is {3} https://splits.io{4}".format(
        username.capitalize(),
        output_game,
        cat_response,
        time_formatter.format_time(pb_splits["time"]),
        pb_splits["path"]
    )
    return True, msg
Esempio n. 6
0
def call(salty_inst, c_msg, balancer, **kwargs):
    beatmaps = re.findall(regexes.OSU_URL, c_msg["message"])
    final_list = []
    for i in beatmaps:
        success, response = salty_inst.osu_api.get_beatmap("{0}={1}".format(i[0], i[1]), **kwargs)
        if not success:
            continue
        final_list.append("[{0}] {1} - {2}, mapped by {3} ({4} stars)".format(
            time_formatter.format_time(response[0]["total_length"]),
            response[0]["artist"],
            response[0]["title"],
            response[0]["creator"],
            round(float(response[0]["difficultyrating"]), 2)
        ))

    if len(final_list) == 0:
        return False, "No valid beatmaps linked."

    return True, " | ".join(final_list)
Esempio n. 7
0
def call(salty_inst, c_msg, **kwargs):
    msg_split = c_msg["message"].split(" ", 2)
    infer_category = True
    search_game = True

    try:
        game = msg_split[1]
        search_game = False
    except IndexError:
        game = salty_inst.game
    try:
        category = msg_split[2].lower()
        infer_category = False
    except IndexError:
        category = salty_inst.title

    if search_game:
        success, response = salty_inst.sr_com_api.get_games(
            {"name": game}, embeds=["categories"])
        if not success:
            return False, \
                "Error retrieving games from speedrun.com ({0}).".format(response.status_code)

        for i in response["data"]:
            if i["names"]["international"] == game:
                found_categories = {
                    x["name"]: x["id"]
                    for x in i["categories"]["data"] if x["type"] == "per-game"
                }
                game_id = i["id"]
                break
        else:
            return False, "No game found for: {0}.".format(game)
    else:
        success, response = salty_inst.sr_com_api.get_games(
            {"abbreviation": game}, embeds=["categories"])
        if not success:
            try:
                decode = response.json()
            except ValueError:
                decode = {
                    "message": "Game {0} could not be found".format(game),
                    "status": 404
                }
            return False, "{0} ({1})".format(decode["message"],
                                             decode["status"])

        try:
            found_categories = {
                x["name"]: x["id"]
                for x in response["data"][0]["categories"]["data"]
                if x["type"] == "per-game"
            }
            game_id = response["data"][0]["id"]
        except IndexError:
            return False, "No game found for: {0}.".format(game)

    if infer_category:
        category_finder = get_category_title.find_active_cat
    else:
        category_finder = get_category_string.find_active_cat

    cat_success, cat_response = category_finder(found_categories, category)

    if not cat_success:
        cat_response = list(found_categories)[0]

    success, response = salty_inst.sr_com_api.get_leaderboards(
        game_id,
        found_categories[cat_response],
        embeds=["game", "players"],
        params={"top": 1},
        **kwargs)
    if not success:
        return False, \
            "Error retrieving leaderboard from speedrun.com ({0}).".format(response.status_code)

    try:
        run_time = response["data"]["runs"][0]["run"]["times"]["primary_t"]
    except Exception as e:
        logging.error("Could not get run duration from srcom response.")
        logging.error(game_id, found_categories)
        logging.exception(e)
        raise e

    msg = "The current world record for {0} {1} is {2} by {3}.".format(
        response["data"]["game"]["data"]["names"]["international"],
        cat_response, time_formatter.format_time(run_time),
        response["data"]["players"]["data"][0]["names"]["international"])
    if response["data"]["runs"][0]["run"]["videos"]:
        msg += " Video: {0}".format(
            response["data"]["runs"][0]["run"]["videos"]["links"][0]["uri"])
    if response["data"]["runs"][0]["run"]["splits"]:
        splits_path = response["data"]["runs"][0]["run"]["splits"][
            "uri"].split("/")[-1]
        msg += " Splits: https://splits.io/{0}".format(splits_path)

    return True, msg
Esempio n. 8
0
def call(salty_inst, c_msg, **kwargs):
    msg_split = c_msg["message"].split(" ", 2)
    infer_category = True
    search_game = True

    try:
        game = msg_split[1].lower()
        search_game = False
    except IndexError:
        game = salty_inst.game
    try:
        category = msg_split[2].lower()
        infer_category = False
    except IndexError:
        category = salty_inst.title

    if search_game:
        success, response = salty_inst.sr_com_api.get_games({"name": game}, embeds=["categories"])
        if not success:
            return False, "Error retrieving games from speedrun.com ({0}).".format(response.status_code)

        for i in response["data"]:
            if i["names"]["international"] == game:
                found_categories = {x["name"]: x["id"] for x in i["categories"]["data"] if x["type"] == "per-game"}
                game_id = i["id"]
                break
        else:
            return False, "No game found for: {0}.".format(game)
    else:
        success, response = salty_inst.sr_com_api.get_games({"abbreviation": game}, embeds=["categories"])
        if not success:
            try:
                decode = response.json()
            except ValueError:
                decode = {"message": "Game {0} could not be found".format(game), "status": 404}
            return False, "{0} ({1})".format(decode["message"], decode["status"])

        try:
            found_categories = {
                x["name"]: x["id"] for x in response["data"][0]["categories"]["data"] if x["type"] == "per-game"
            }
            game_id = response["data"][0]["id"]
        except IndexError:
            return False, "No game found for: {0}.".format(game)

    if infer_category:
        category_finder = get_category_title.find_active_cat
    else:
        category_finder = get_category_string.find_active_cat

    cat_success, cat_response = category_finder(found_categories, category)

    if not cat_success:
        return False, cat_response

    success, response = salty_inst.sr_com_api.get_leaderboards(
        game_id, found_categories[cat_response], embeds=["game", "players"], params={"top": 1}, **kwargs
    )
    if not success:
        return False, "Error retrieving leaderboard from speedrun.com ({0}).".format(response.status_code)

    msg = "The current world record for {0} {1} is {2} by {3}.".format(
        response["data"]["game"]["data"]["names"]["international"],
        cat_response,
        time_formatter.format_time(response["data"]["runs"][0]["run"]["times"]["primary_t"]),
        response["data"]["players"]["data"][0]["names"]["international"],
    )
    if response["data"]["runs"][0]["run"]["videos"]:
        msg += " Video: {0}".format(response["data"]["runs"][0]["run"]["videos"]["links"][0]["uri"])
    if response["data"]["runs"][0]["run"]["splits"]:
        splits_path = response["data"]["runs"][0]["run"]["splits"]["uri"].split("/")[-1]
        msg += " Splits: https://splits.io/{0}".format(splits_path)

    return True, msg
Esempio n. 9
0
def call(salty_inst, c_msg, **kwargs):
    msg_split = c_msg["message"].split(" ", 3)
    infer_category = True

    try:
        user_name = msg_split[1].lower()
    except IndexError:
        user_name = salty_inst.speedruncom_nick
    try:
        game = msg_split[2]
    except IndexError:
        game = salty_inst.game
    try:
        category = msg_split[3].lower()
        infer_category = False
    except IndexError:
        category = salty_inst.title

    success, response = salty_inst.sr_com_api.get_user_pbs(user_name, ["game", "category"], **kwargs)
    if not success:
        return False, \
            "Error retrieving pbs from speedrun.com ({0}).".format(response.status_code)

    found_categories = {}
    matching_games = []
    for i in response["data"]:
        if game == i["game"]["data"]["abbreviation"] or game == i["game"]["data"]["names"]["international"].lower():
            found_categories[i["category"]["data"]["name"]] = i["category"]["data"]["id"]
            matching_games.append(i)
            break
    else:
        return False, "Could not find a pb for {0}.".format(game)

    if infer_category:
        category_finder = get_category_title.find_active_cat
    else:
        category_finder = get_category_string.find_active_cat

    cat_success, cat_response = category_finder(found_categories, category)
    if not cat_success:
        return False, cat_response

    for i in matching_games:
        if i["category"]["data"]["name"] == cat_response:
            pb_record = i
            break
    else:
        return False, "Error finding PB record."

    pb_time = time_formatter.format_time(i["run"]["times"]["primary_t"])
    place = "{0}{1}".format(str(pb_record["place"]), get_suffix.suffix(pb_record["place"]))
    msg = "{0}'s pb for {1} {2} is {3}.  They are ranked {4} on speedrun.com {5}".format(
        user_name.capitalize(),
        pb_record["game"]["data"]["names"]["international"],
        cat_response,
        pb_time,
        place,
        pb_record["run"]["weblink"]
    )

    return True, msg
Esempio n. 10
0
def test_time_parsing():
    assert "3:28:54" == time_formatter.format_time(12534)
    assert "3:54" == time_formatter.format_time(234)
    assert "5:00" == time_formatter.format_time(300)
Esempio n. 11
0
def test_time_parsing():
    assert "3:28:54" == time_formatter.format_time(12534)
    assert "3:54" == time_formatter.format_time(234)
    assert "5:00" == time_formatter.format_time(300)