Last active
April 23, 2019 10:03
-
-
Save DuckOfDoom/5ce5b64523d4b8bb4ada00369677016e to your computer and use it in GitHub Desktop.
A python script to pull data about your current game participants from Riot Games API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
import requests | |
import os | |
import json | |
import sys | |
from prettytable import PrettyTable | |
apiKey="YOUR_KEY" | |
name= "The Duck Of Doom" if len(sys.argv) <= 1 else sys.argv[1] | |
region="euw1" if len(sys.argv) <= 2 else sys.argv[2] | |
baseUrl=f"https://{region}.api.riotgames.com/lol" | |
# settings for ddragon to get champion info from | |
version="9.6.1" | |
locale="en_GB" | |
ddragonUrl=f"http://ddragon.leagueoflegends.com/cdn/{version}/data/{locale}/championFull.json" | |
championData = {} | |
def main(): | |
loadChampionData() | |
summonerId = getSummonerId(name); | |
if not summonerId: print(f"Can't find summoner id for name '{name}'"); exit() | |
currentGameInfo = getCurrentGameInfo(summonerId); | |
if not currentGameInfo: print(f"No active game info for player '{name}' on '{region}'"); exit() | |
participants = currentGameInfo['participants'] | |
table = PrettyTable() | |
table.title = f'Current game for {name}:' | |
table.field_names = ["Me", "Name", "Champion", "Mastery", "League", "LP", "W / L"] | |
table.align["Name"] = "l" | |
table.align["Champion"] = "l" | |
table.align["Mastery"] = "r" | |
table.align["League"] = "l" | |
def formatThousands(i): | |
s = str(i) | |
v = "" | |
for i, c in enumerate(reversed(s)): | |
v += c | |
if i != len(s) - 1 and (i + 1) % 3 == 0: | |
v += " " | |
return "".join(list(reversed(v))) | |
for i, participant in enumerate(sorted(participants, key=lambda x: x['teamId'])): | |
summonerId = participant['summonerId'] | |
(champion, points) = getChampionInfo(summonerId, participant['championId']) | |
leagueInfo = getLeagueInfo(summonerId) | |
summonerName = participant['summonerName'] | |
if leagueInfo: | |
tier = leagueInfo['tier'] | |
rank = leagueInfo['rank'] | |
wins = leagueInfo['wins'] | |
losses = leagueInfo['losses'] | |
lp = leagueInfo['leaguePoints'] | |
else: | |
tier = "Unranked" | |
rank = "" | |
wins = 0 | |
losses = 0 | |
lp = 0 | |
table.add_row(["X" if summonerName == name else " ", summonerName, champion, f"{formatThousands(points)} pts.", f"{tier} {rank}", f"{lp} LP", f"{wins} / {losses} "]) | |
if i == 4: | |
table.add_row([""] * 7) | |
print(table) | |
def makeRequest(url, params={}): | |
params['api_key'] = apiKey | |
combinedUrl=f"{baseUrl}{url}" | |
result = requests.get(combinedUrl, params=params).json() | |
if 'status' in result: | |
status = result['status']['status_code'] | |
if status == 404: | |
print(f"Request {combinedUrl} returned with status code code 404:\n{result}") | |
return None | |
if status == 403: | |
print(f"Request {combinedUrl} returned with status code code 403:\n{result}") | |
return None | |
return result | |
def loadChampionData(): | |
global championData | |
filename="champions.json" | |
if os.path.exists(filename): | |
with open(filename) as f: | |
championData = json.load(f) | |
return | |
data = requests.get(ddragonUrl).json()['data'] | |
for k, v in data.items(): | |
championData[v['key']] = v['id'] | |
with open(filename, 'w') as f: | |
f.write(json.dumps(championData)) | |
def getSummonerId(summonerName): | |
sumInfo = makeRequest(f"/summoner/v4/summoners/by-name/{summonerName}") | |
if sumInfo: return sumInfo['id'] | |
def getCurrentGameInfo(summonerId): | |
return makeRequest(f"/spectator/v4/active-games/by-summoner/{summonerId}"); | |
def getChampionInfo(summonerId, championId): | |
masteryInfo = makeRequest(f"/champion-mastery/v4/champion-masteries/by-summoner/{summonerId}/by-champion/{championId}"); | |
if masteryInfo: | |
points = masteryInfo['championPoints'] | |
else: | |
points = -1 | |
return (championData[str(championId)], points) | |
def getLeagueInfo(summonerId): | |
entries = makeRequest(f"/league/v4/entries/by-summoner/{summonerId}") | |
if entries: | |
return next((x for x in entries if x['queueType'] == 'RANKED_SOLO_5x5'), None) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment