0

I'm trying to pull a list of team and player stats from match IDs. Everything looks fine to me but when I run my "for loops" to call the functions for pulling the stats I want, it just prints the error from my try/except block. I'm still pretty new to python and this is my first project so I've tried everything I can think of in the past few days but no luck. I believe the problem is with my actual pull request but I'm not sure as I'm also using a GitHub library I found to help me with the Riot API while I change and update it to get the info I want.

def get_match_json(matchid):
url_pull_match = "https://{}.api.riotgames.com/lol/match/v5/matches/{}/timeline?api_key={}".format(region, matchid, api_key)
match_data_all = requests.get(url_pull_match).json()

# Check to make sure match is long enough
try:
    length_match = match_data_all['frames'][15]
    return match_data_all

except IndexError:
    return ['Match is too short. Skipping.']

And then this is a shortened version of the stat function:

def get_player_stats(match_data, player):
# Get player information at the fifteenth minute of the game.

player_query = match_data['frames'][15]['participantFrames'][player]
player_team = player_query['teamId']
player_total_gold = player_query['totalGold']
player_level = player_query['level']

And there are some other functions in the code as well but I'm not sure they are faulty as well or if they are needed to figure out the error. But here is the "for loop" to call the request and defines the variable 'matchid'

for matchid_batch in all_batches:
match_data = []
for match_id in matchid_batch:
    time.sleep(1.5)
    if match_id == 'MatchId':
        pass

    else:
        try:
            match_entry = get_match_row(match_id)
            if match_entry[0] == 'Match is too short. Skipping.':
                print('Match', match_id, "is too short.")

            else:
                match_entry = get_match_row(match_id).reshape(1, -1)
                match_data.append(np.array(match_entry))

        except KeyError:
            print('KeyError.')

match_data = np.array(match_data)
match_data.shape = -1, 17

df = pd.DataFrame(match_data, columns=column_titles)
df.to_csv('Match_data_Diamond.csv', mode='a')
print('Done Batch!')

Since this is my first project any help would be appreciated since I can't find any info on this particular subject so I really don't know where to look to learn why it's not working on my own.

DharTyr
  • 1
  • 1
  • Can you put `except KeyError as e: print(e)` in your except (I imagine it's this except who is throwing the error) to print your error. Also try to do small functions by functionalities it will help you debug. Finally why do you only take the frame 15 (to understand what you are trying to achive) ? – TheSmartMonkey Mar 03 '22 at 08:51

1 Answers1

0

I guess your issue was that the 'frame' array is subordinate to the array 'info'.

def get_match_json(matchid):
    url_pull_match = "https://{}.api.riotgames.com/lol/match/v5/matches/{}/timeline?api_key={}".format(region, matchid, api_key)
    match_data_all = requests.get(url_pull_match).json()

    try:
        length_match = match_data_all['info']['frames'][15]
        return match_data_all
    except IndexError:
        return ['Match is too short. Skipping.']
def get_player_stats(match_data, player): #  player has to be an int (1-10)
    # Get player information at the fifteenth minute of the game.

    player_query = match_data['info']['frames'][15]['participantFrames'][str(player)]
    #player_team = player_query['teamId'] - It is not possibly with the endpoint to get the teamId
    player_total_gold = player_query['totalGold']
    player_level = player_query['level']
    
    return player_query

This example worked for me. Unfortunately it is not possible to gain the teamId only through your API-endpoint. Usually the players 1-5 are in team 100 (blue side) and 6-10 in team 200 (red side).

DarkIntaqt
  • 150
  • 1
  • 10