· 5 years ago · Jul 06, 2020, 11:14 AM
1import requests #Handles requests to Riot's API
2import json #So we can handle JSON data (I don't think we useanythign here as its requests that handles it, but just in case!)
3
4key = "<RIOT API KEY>" #API Key from riot games so we can change it once here
5
6def rankInfo(player):
7 #Formats summoner to be accpetable in a url - spaces turned into %20
8 summUrl = ""
9 for x in player:
10 if x == " ":
11 y = x.replace(" ","%20")
12 summUrl = summUrl+y
13 else:
14 summUrl = summUrl+x
15
16 #Defining 1st url and parameters we want info for
17 urlA = "https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+summUrl+"?api_key="+key
18 myobj = {
19 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
20 "Accept-Language": "en-US,en;q=0.9",
21 "Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8",
22 "Origin": "https://developer.riotgames.com",
23 "X-Riot-Token": key
24 }
25
26 #Getting initial info for that summoner
27 x = requests.get(urlA, params = myobj)
28 dictA = x.json()
29 x.close()
30
31 #Getting initial variables from dictA (dictionary)
32 summoner = dictA.get("name")
33 level = dictA.get("summonerLevel")
34 accountID = dictA.get("id") #We use this later to get ranked info
35
36 #Defining url for ranked info
37 urlB = "https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/"+accountID+"?api_key="+key
38 x = requests.get(urlB, params = myobj)
39 listA = x.json()
40
41 x.close()
42
43
44 #listA is a list of two dictionaries - cut out first one and use second one as that has solo queue info
45
46 solo = "RANKED_SOLO_5X5"
47 dictB = {}
48 for a in listA:
49 tempDict = a
50 if tempDict.get("queueType") == "RANKED_SOLO_5x5":
51 dictB = tempDict
52 break
53 if tempDict.get("queueType") == "RANKED_FLEX_SR":
54 dictB = listA[1]
55 break
56
57 #Defining variables to store relevent data
58 tier = dictB.get("tier") #Bronze / Silver / Gold etc.
59 division = dictB.get("rank") #I - IV
60 wins = int(dictB.get("wins"))
61 losses = int(dictB.get("losses"))
62
63 winRate = (wins / (wins + losses)) * 100 #Works out win rate % too see how gamer that summoner is
64
65 skillLvl = "" #Good meme :P
66 if winRate > 50:
67 skillLvl = "Gamer"
68 elif winRate < 50:
69 skillLvl = "Trash"
70 elif winRate == 50:
71 skillLvl = "Mediocre"
72
73 stats = [summoner,level,tier,division,wins,losses,winRate,skillLvl] #Store info in an array so we can return one variable and iterate through it to display
74 return stats
75
76
77 #print("Summoner:",summoner)
78 #print("Level:",level)
79 #print("Rank:",tier,division)
80 #print(str(wins)+"W / "+str(losses)+"L")
81 #print(str(winRate)+"% - Win Rate")