Python: League of Legends Quick Stats

Introduction

Disclaimer: I do not own Riot Games or any copyrights, trademarks attributable to the company.

A critical part of learning programming is the delight of learning new languages and libraries. I always recommend to people that ask me “how do I learn programming,” that they should do three things:

  • Learn the fundamentals and syntax of a language (think variables, loops, controls, objects, etc.)
  • Do something creative with the language (don’t blindly follow a tutorial, do something unique)
  • Do something fun!

This post focuses on the third topic (and a little bit of the second): Doing something fun, specifically with Python! While there are definitely websites around for the same (and more) information as the short program I wrote, it still is interesting to learn interesting tidbits of a language.

I play League of Legends on an on-again, off-again basis. My desire to learn new and interesting things about programming languages lead me to discovering that Riot Games, the developers of League of Legends, maintain an API to dynamically pull data about the game. It did not take me too long to decide that I wanted to try some simple calls using HTTP requests to pull JSON data!

For reference, here is the API reference for the Riot Games API. You will need a developer key to use it, which also requires you to have a League of Legends game account.

Prerequisites:

  • Python installed, with an editor/IDE of your choice
  • Riot Games API Key and a League of Legends account

Code

We will need to work with HTTP requests and JSON data. As such, we will need to import these:

import requests
import json

This python script will be sweet, simple, and to the point. I am sure there are many ways to increase its readability and performance, but those topics are a blog post for another day. Let’s just say that to make it more flexible, the reader should implement it to ask the user for input.

## Parameters
summonerName = "<your_League_of_Legends_username>"
APIKey = "<your_riot_games_APIkey_here"

Now, we need to make an HTTP GET request to get some JSON data! The URL can be built with a method/function as follows:

## Get account details by providing the account name
def requestSummonerData(summonerName, APIKey):
    URL = "https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + 
    summonerName + "?api_key=" + APIKey
    response = requests.get(URL)
    return response.json()

In your main function, you will need to call this function and store it into a variable. From there, you will need to access the data via array indexing. Finally, you will print it out to the screen with some normal Python:

def main():
    summonerData  = requestSummonerData(summonerName, APIKey)

    ## Uncomment this line if you want a pretty JSON data dump
    ## print(json.dumps(summonerData, sort_keys=True, indent=2))

    ## Print to the console some basic account information
    print("\n\nSummoner Name:\t" + summonerData ['name'])
    print("Level:\t\t" + str(summonerData ['summonerLevel']))
  • Note: If you are unsure of what the return response for any HTTP GET request will look like, you can use the json.dumps() method to display it nicely.*

The next steps to learning more about the interesting things you can do with Python, HTTP requests, and the Riot Games’ League of Legends API is to try more interesting stuff! For example, below you will find the following code which will display ranked match stats and account information about a given username:

import requests
import json
## Get account details by providing the account name
def requestSummonerData(summonerName, APIKey):
    URL = "https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + summonerName + "?api_key=" + APIKey
    response = requests.get(URL)
    return response.json()

## Get an account's ranked match data by account ID
def requestRankedData(ID, APIKey):
    URL = "https://na1.api.riotgames.com/lol/league/v3/positions/by-summoner/" + str(ID) + "?api_key=" + APIKey
    response = requests.get(URL)
    return response.json()

def main():
    ## Parameters
    summonerName = "<username_here>"
    APIKey = "<riot_games_api_key_here>"

    summonerData  = requestSummonerData(summonerName, APIKey)

    # Uncomment this line if you want a pretty JSON data dump
    #print(json.dumps(summonerData, sort_keys=True, indent=2))

    ## Print to the console some basic account information
    print("\n\nSummoner Name:\t" + str(summonerData ['name']))
    print("Level:\t\t" + str(summonerData ['summonerLevel']))

    ## Pull the ID field from the response data, cast it to an int
    ID = int(summonerData ['id'])

    rankedData = requestRankedData(ID, APIKey)
    # Uncomment this line if you want a pretty JSON data dump
    #print(json.dumps(rankedData, sort_keys=True, indent=2))

    # Get some information form the ranked match data, and do some really basic math
    wins = rankedData [1]['wins']
    losses = rankedData [1]['losses']
    ratio = wins / (wins + losses)

    # Display some information about the account's rank and stats to the console
    print("Rank:\t\t" + rankedData [1]['tier'] + " " + rankedData [1]['rank'] + " " + str(rankedData [1]['leaguePoints']) + " LP")
    print("W/L:\t\t" + str(wins) + "/" + str(losses))
    print("W/L Ratio:\t" + str(round((ratio) * 100.0, 2)) + "%\n")

    # Some really endearing or supportive messaging, conditionally based on their recent games
    hotStreak = rankedData [1]['hotStreak']
    if hotStreak:
        print("Based on recent games, you must be a smurf because you've been on fire lately!!")
    else:
        print("Based on recent games, you are either boosted or stuck in ELO hell.")

if __name__ == "__main__":
    main()

Code above is provided in a Github repository.

And here is a sample output of the code. Name blurred out for reasons!

That’s it! Questions? Comments? Email me at: [email protected]