コード例 #1
0
ファイル: pb.py プロジェクト: carriercomm/salty_bot
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].lower()
    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 info 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.append(i["cateory"]["data"]["name"])
            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

    pb_time = time_formatter.format(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
コード例 #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 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
コード例 #3
0
ファイル: race.py プロジェクト: TulgaLisitsa/salty_bot
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
コード例 #4
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
コード例 #5
0
def test_get_suffix():
    assert "th" == get_suffix.suffix(11)
    assert "st" == get_suffix.suffix(1)
    assert "nd" == get_suffix.suffix(2)
    assert "rd" == get_suffix.suffix(3)
    assert "th" == get_suffix.suffix(4)
コード例 #6
0
def test_get_suffix():
    assert "th" == get_suffix.suffix(11)
    assert "st" == get_suffix.suffix(1)
    assert "nd" == get_suffix.suffix(2)
    assert "rd" == get_suffix.suffix(3)
    assert "th" == get_suffix.suffix(4)