Play by hitting the Riot Games API in Python First half

This article was written as an article on the kstm Advent Calendar.

About the Riot Games API

API provided by Riot Games, the operator of the online game "League of Legends" (commonly known as LoL) If you use this API, the results of individual and team battles will be pulled in JSON format, so this time I will try to hit it and play with it.

Target

It would be nice if we could create a "somewhat cool" profile image (like a Twitter header image) using the battle record data of ranked battles.

--Execution environment

How to call the API

Condition: You have a LoL account on a server operated by Riot Games https://developer.riotgames.com/ If you jump to API DOCUMENTATION-> FULL API REFERENCE, there are various APIs, and if you give an arbitrary argument, JSON will be returned. If you hit it, you need the API key assigned to each account, so check it on the dashboard.

I will actually hit it

Library used

lib.py


import urllib.request as urllib2
import json

I was addicted to ʻimport as it is ʻurllib2, so when I looked it up, it seems that the same function can be obtained by using ʻurllib.request in python3, so I will ʻimport this as ʻurllib2`

First of all, from the summoner ID acquisition

getSummID.py


SUM_NAME = "sumname" #Summoner name Change it to whatever you like
SUMMONER_V14 = "https://jp.api.pvp.net/api/lol/jp/v1.4/summoner/by-name/"
API_KEY = "api_key=XXXXXXXXXXXXXXXX" #Change it to your own API key
try:
    s = urllib2.urlopen(SUMMONER_V14 + SUM_NAME + '?' + API_KEY)
    summ = json.loads(s.read().decode('utf-8'))
    SUM_ID = summ[SUM_NAME.lower()]["id"]
    print(SUM_ID)
finally:
    s.close()

The API used here is summoner-v1.4 When you actually get it, it returns an object of lowercase summoner name type. Summoner ID, profile icon ID, etc. are stored in this object. Since the summoner ID is required to get the battle record data, access the id like root [SUM_NAME.lower ()] ["id "] and try to get it. If you use my summoner name and you can see 6300501, you are successful.

This summoner ID is very important, and you will use this ID to get the data associated with the player such as battle record.

Let's get the results of ranked battles

The API used here is stats-v1.3 This API does not get the data for each match, but can get the data for each champion. Let's display a list of battle records for each champion.

getRankedStats.py


try:
    r = urllib2.urlopen(RANKED_STATS_V13 + '?' + SEASON + '&' +API_KEY)
    ranked = json.loads(r.read().decode('utf-8'))
    champions = ranked['champions']
    for champion in champions:
        stats = champion["stats"]
        print("Champion id:" + str(champion["id"]) + "The battle record of" + str(stats["totalSessionsWon"]) + "Win" + str(stats["totalSessionsLost"]) + "Loss")
finally:
    r.close()

If you try to get it, you can get summonerID, modifyData and champions list. As mentioned above, this API can get the battle record for each champion, and the data is stored in the stats object in the champions list. So, if you use the for statement to get the number of wins and losses and display it, it will be as follows.

Champion id: 42 has a record of 1 win and 1 loss. Champion id: 3 has a record of 0 wins and 1 loss Champion id: 4 has a record of 1 win and 6 losses ... Champion id: 0 has a record of 22 wins and 28 losses This champion id is the id assigned to each champion, and the champion name can also be obtained by associating it with the static-data explained next time. By the way, this champion id: 0 is the total data of the battle records of all champions.

It's going to be long, so this time it's going to continue in the second half (I want to continue)

Recommended Posts

Play by hitting the Riot Games API in Python First half
Try hitting the YouTube API in Python
Tips for hitting the ATND API in Python
Play music by hitting the unofficial API of Google Play Music
Second half of the first day of studying Python Try hitting the Twitter API with Bottle
Getting the arXiv API in Python
Hit the Sesami API in Python
Hit the web API in Python
Access the Twitter API in Python
The first step in Python Matplotlib
Try using the Wunderlist API in Python
Continuously play the first Python Sukusta MV
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Read the file line by line in Python
Read the file line by line in Python
MongoDB for the first time in Python
Try hitting the Spotify API in Django.
The first web app created by Python beginners
The first step in the constraint satisfaction problem in Python
Try using the BitFlyer Ligntning API in Python
Hit the Firebase Dynamic Links API in Python
Play with the power usage API provided by Yahoo
Initial settings when using the foursquare API in python
Play youtube in python
Evernote API in Python
Shift the alphabet string by N characters in Python
Store the stock price scraped by Python in the DB
Using the National Diet Library Search API in Python
C API in Python 3
I tried hitting the API with echonest's python client
Read English sentences by hitting Google Translate API with Python without using the distributed module
Try hitting the Twitter API quickly and easily with Python
A note about hitting the Facebook API with the Python SDK
[Python] Open the csv file in the folder specified by pandas
Get your heart rate from the fitbit API in Python!
The first API to make with python Djnago REST framework
Minimum load test knowledge gained in the first API server load test
Visualize the correlation matrix by principal component analysis in Python
Hit Mastodon's API in Python
Download the file in Python
Find the difference in Python
Play RocketChat with API / Python
Blender Python API in Houdini (Python 3)
Call the API with python3.
Sort by date in python
What I learned by writing a Python Pull Request for the first time in my life
Play a sound in Python assuming that the keyboard is a piano keyboard
Export the contents acquired by Twitter Streaming API in JSON format
[Understanding in the figure] Management of Python virtual environment by Pipenv
Check if the password hash generated by PHP matches in Python
Read the standard output of a subprocess line by line in Python
Regularly upload files to Google Drive using the Google Drive API in Python
[Python] Do not put Japanese in the path used by OpenCV
Can Python implement the Dependency Inversion Principle (DIP) in the first place?
Python beginners hit the unofficial API of Google Play Music to play music
First simple regression analysis in Python
First Python 3 ~ The beginning of repetition ~
Python in the browser: Brython's recommendation
[Python] Hit the Google Translation API
Get the desktop path in Python