This article was written as an article on the kstm Advent Calendar.
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.
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
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.
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`
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.
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