コード例 #1
0
def leaderboard(input):
    if input == message.lower().strip():
        try:
            response = urlopen(
                'https://api.twitch.tv/kraken/channels/{}?oauth_token={}'.
                format(CHANNEL, CHANNELPASS.strip('oauth:')))
        except urllib.error.HTTPError as err:
            sendMessage(s,
                        "Error: Invalid CHANNEL/CHANNELPASS in settings file")
            cooldown()
            return
        readable = response.read().decode('utf-8')
        lst = loads(readable)
        title = lst['status'].lower()
        game = None
        game_title = None

        for i in range(len(GAMES)):
            if GAMES[i][0].lower() in title:
                game = GAMES[i][1]
                game_title = GAMES[i][0]
                break

        category = None
        category_title = None
        for i in range(len(CATEGORIES)):
            if CATEGORIES[i][0].lower() in title:
                category = CATEGORIES[i][1]
                category_title = CATEGORIES[i][0]
                break

        if game == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return

        if category != None:
            sendMessage(
                s, game_title + " " + category_title +
                " Leaderboard: https://www.speedrun.com/{}#{}".format(
                    game, category))
            cooldown()
            return

        elif category == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return
コード例 #2
0
def raceCommand(input):
    if input == message.lower().strip():
        #Get the stream title from the Twitch API
        try:
            response = urlopen(
                'https://api.twitch.tv/kraken/channels/{}?oauth_token={}'.
                format(CHANNEL, CHANNELPASS.strip('oauth:')))
        except urllib.error.HTTPError as err:
            sendMessage(s,
                        "Error: Invalid CHANNEL/CHANNELPASS in settings file")
            cooldown()
            return
        readable = response.read().decode('utf-8')
        lst = loads(readable)
        title = lst['status'].lower()

        if 'race with' in title:
            pass
        elif 'race with' not in title:
            sendMessage(
                s,
                CHANNEL.title() +
                " is not currently racing or no racers detected in stream title."
            )
            cooldown()
            return

        title_list = title.split()
        r = title_list.index('with') + 1
        contenders = []
        length = len(title_list)
        diff = length - r
        while True:
            contenders.append(title_list[r].strip(','))
            diff = diff - 1
            r = r + 1
            if diff == 0:
                break

        sendMessage(
            s, "Race link: http://kadgar.net/live/" + CHANNEL +
            "/".join(contenders))
        cooldown()
コード例 #3
0
def personalBest(input):
    if input == message.lower().split()[0]:
        category_specified = False
        try:
            message.split()[2]
        except IndexError as err:
            pass
        else:
            category_specified = True

        #Get the stream title from the Twitch API
        try:
            response = urlopen(
                'https://api.twitch.tv/kraken/channels/{}?oauth_token={}'.
                format(CHANNEL, CHANNELPASS.strip('oauth:')))
        except urllib.error.HTTPError as err:
            sendMessage(s,
                        "Error: Invalid CHANNEL/CHANNELPASS in settings file")
            cooldown()
            return
        readable = response.read().decode('utf-8')
        lst = loads(readable)
        title = lst['status'].lower()

        game = None
        for i in range(len(GAMES)):
            if GAMES[i][0].lower() in title:
                game = GAMES[i][1].lower()
                platform_title = GAMES[i][2]
                break

        category_title = None
        if category_specified == True:
            category_title = message.lower().strip('!pb ')
            first_word = category_title.lower().split()[0]
            category_title = category_title.split(first_word, 1)[-1].strip()
            check = False
            for i in range(len(CATEGORIES)):
                if CATEGORIES[i][0].lower() == category_title:
                    check = True
                    category_title = CATEGORIES[i][0]
                    break
            if check == False:
                sendMessage(s, "Error: Invalid category specified")
                cooldown()
                return

        elif category_specified == False:
            for i in range(len(CATEGORIES)):
                if CATEGORIES[i][0].lower() in title:
                    category_title = CATEGORIES[i][0]
                    break

        if game == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return

        username = None
        try:
            message.split()[1]
        except IndexError as err:
            username = SRC_USERNAME
        else:
            username = message.split()[1]

        if category_title != None:
            try:
                response = urlopen(
                    'https://www.speedrun.com/api/v1/users/{}/personal-bests?embed=category,game,platform'
                    .format(username))
            except urllib.error.HTTPError as err:
                sendMessage(s, "Error: Speedrun.com user not found")
                cooldown()
                return

            readable = response.read().decode('utf-8')
            lst = loads(readable)

            place = None
            time_in_sec = None
            for cat in lst['data']:
                if cat['category']['data']['name'].lower(
                ) == category_title.lower() and cat['game']['data'][
                        'abbreviation'].lower() == game and cat['platform'][
                            'data']['name'] == platform_title:
                    time_in_sec = int(cat['run']['times']['realtime_t'])
                    place = cat['place']
                    break

            if place == None:
                sendMessage(
                    s,
                    username.title() + " currently does not have a PB for " +
                    category_title + " on the leaderboard.")
                cooldown()
                return

            ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[
                (math.floor(n / 10) % 10 != 1) * (n % 10 < 4) * n % 10::4])

            hours = divmod(time_in_sec, 3600)
            minutes = divmod(hours[1], 60)
            seconds = minutes[1]
            pb = ''
            if hours[0] > 0:
                pb = str(hours[0]) + "h " + str(
                    minutes[0]) + "m " + str(seconds) + "s"
            elif minutes[0] > 0:
                pb = str(minutes[0]) + "m " + str(seconds) + "s"
            else:
                pb = str(seconds) + "s"

            sendMessage(
                s,
                username.title() + "\'s " + category_title + " PB is " + pb +
                " (" + ordinal(place) + " place).")
            cooldown()

        elif category_title == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return
コード例 #4
0
def place(input):
    if input == message.lower().split()[0]:
        username = None
        try:
            message.split()[1]
        except IndexError as err:
            username = SRC_USERNAME
        else:
            username = message.split()[1]

        #Get the stream title from the Twitch API
        try:
            response = urlopen(
                'https://api.twitch.tv/kraken/channels/{}?oauth_token={}'.
                format(CHANNEL, CHANNELPASS.strip('oauth:')))
        except urllib.error.HTTPError as err:
            sendMessage(s,
                        "Error: Invalid CHANNEL/CHANNELPASS in settings file")
            cooldown()
            return
        readable = response.read().decode('utf-8')
        lst = loads(readable)
        title = lst['status'].lower()

        game = None
        for i in range(len(GAMES)):
            if GAMES[i][0].lower() in title:
                game = GAMES[i][1].lower()
                platform_title = GAMES[i][2]
                break

        if game == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return

        category_title = None
        for i in range(len(CATEGORIES)):
            if CATEGORIES[i][0].lower() in title:
                category_title = CATEGORIES[i][0]
                break

        if category_title != None:
            try:
                response = urlopen(
                    'https://www.speedrun.com/api/v1/users/{}/personal-bests?embed=category,game,platform'
                    .format(username))
            except urllib.error.HTTPError as err:
                sendMessage(s, "Error: Speedrun.com user not found")
                cooldown()
                return

            readable = response.read().decode('utf-8')
            lst = loads(readable)

            place = None
            time_in_sec = None
            for cat in lst['data']:
                if cat['category']['data']['name'].lower(
                ) == category_title.lower() and cat['game']['data'][
                        'abbreviation'].lower() == game and cat['platform'][
                            'data']['name'] == platform_title:
                    time_in_sec = int(cat['run']['times']['realtime_t'])
                    place = cat['place']
                    break

            if place == None:
                sendMessage(
                    s,
                    username.title() + " currently does not have a PB for " +
                    category_title + " on the leaderboard.")
                cooldown()
                return

            ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[
                (math.floor(n / 10) % 10 != 1) * (n % 10 < 4) * n % 10::4])

            sendMessage(
                s,
                username.title() + " is in " + ordinal(place) + " place for " +
                category_title + ".")

        elif category_title == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return
コード例 #5
0
def fourth(input):
    if input == message.lower().split()[0].strip():
        #Check to see if an argument is specified first
        argument = False
        try:
            message.lower().split()[1]
        except IndexError as err:
            pass
        else:
            argument = True

        #Get the stream title from the Twitch API
        try:
            response = urlopen(
                'https://api.twitch.tv/kraken/channels/{}?oauth_token={}'.
                format(CHANNEL, CHANNELPASS.strip('oauth:')))
        except urllib.error.HTTPError as err:
            sendMessage(s,
                        "Error: Invalid CHANNEL/CHANNELPASS in settings file")
            cooldown()
            return
        readable = response.read().decode('utf-8')
        lst = loads(readable)
        title = lst['status'].lower()
        game = None

        for i in range(len(GAMES)):
            if GAMES[i][0].lower() in title:
                game = GAMES[i][1]
                platform = GAMES[i][3]
                break

        category = None
        category_title = None

        #Check again to see if an argument was specified
        if argument == False:
            for i in range(len(CATEGORIES)):
                if CATEGORIES[i][0].lower() in title:
                    category = CATEGORIES[i][1]
                    category_title = CATEGORIES[i][0]
                    break
        elif argument == True:
            specified_category = message.lower().split(input, 1)[-1].strip()
            for i in range(len(CATEGORIES)):
                if specified_category == CATEGORIES[i][0].lower():
                    category_title = CATEGORIES[i][0]
                    category = CATEGORIES[i][1]
                    break
            if category == None:
                sendMessage(s, "Error: Invalid category specified")
                cooldown()
                return

        if game == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return

        if category != None:
            response = urlopen(
                'https://www.speedrun.com/api/v1/leaderboards/{}/category/{}?top=4&embed=players&platform={}'
                .format(game, category, platform))
            readable = response.read().decode('utf-8')
            lst = loads(readable)
            runner = lst['data']['players']['data'][3]['names'][
                'international']
            time_in_sec = int(
                lst['data']['runs'][3]['run']['times']['realtime_t'])
            hours = divmod(time_in_sec, 3600)
            minutes = divmod(hours[1], 60)
            seconds = minutes[1]
            place4th = ''
            if hours[0] > 0:
                place4th = str(hours[0]) + "h " + str(
                    minutes[0]) + "m " + str(seconds) + "s "
            elif minutes[0] > 0:
                place4th = str(minutes[0]) + "m " + str(seconds) + "s "
            else:
                place4th = str(seconds) + "s "

            sendMessage(
                s, "The 4th place time for " + category_title + " is " +
                place4th + "by " + runner + ".")
            cooldown()
            return

        elif category == None:
            sendMessage(s, "No game and/or category detected in stream title.")
            cooldown()
            return