1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/league.py

61 lines
2.4 KiB
Python
Raw Normal View History

2016-08-12 20:53:12 +00:00
import asyncio
import requests
class NotFoundException(Exception):
pass
class NoRankedGamesCompletedException(Exception):
pass
2016-08-13 13:56:27 +00:00
class RateLimitException(Exception):
pass
2016-08-12 20:53:12 +00:00
loop = asyncio.get_event_loop()
# Load League of Legends API key from the leaguetoken.txt file
file = open("leaguetoken.txt", "r")
token = file.read()
file.close()
ranklist = ['BRONZE', 'SILVER', 'GOLD', 'PLATINUM', 'DIAMOND', 'MASTER', 'CHALLENGER']
roman = ['I', 'II', 'III', 'IV', 'V']
# Get player rank info
async def get_player_rank(region: str, summonerid: int, **kwargs):
2016-08-13 13:46:07 +00:00
print("[League] Getting league info for: {region} {summonerid}".format(region=region, summonerid=summonerid))
2016-08-13 18:03:47 +00:00
# GET the json API response
2016-08-12 20:53:12 +00:00
r = await loop.run_in_executor(None, requests.get,
'https://{region}.api.pvp.net/api/lol/{region}/v2.5/league/by-summoner/{summonerid}'
'/entry?api_key={token}'.format(region=region, summonerid=summonerid, token=token))
# Ensure the request is successful
if r.status_code == 200:
2016-08-12 21:29:09 +00:00
return r.json()[str(summonerid)][0]
2016-08-12 20:53:12 +00:00
elif r.status_code == 404:
2016-08-12 21:29:09 +00:00
raise NoRankedGamesCompletedException("This player hasn't completed any ranked games.")
2016-08-13 13:56:27 +00:00
elif r.status_code == 429:
raise RateLimitException("You've been ratelimited by Riot. Check your developer dashboard.")
2016-08-12 20:53:12 +00:00
else:
raise Exception("Unhandled API response.")
2016-08-13 18:03:47 +00:00
# Get player info data
async def get_player_info(region: str, summonerid: int, **kwargs):
print("[League] Getting profile info for: {region} {summonerid}".format(region=region, summonerid=summonerid))
# GET the json API response
2016-08-13 18:11:46 +00:00
r = await loop.run_in_executor(None, requests.get, 'https://{region}.api.pvp.net/api/lol/{region}/v1.4/summoner/'
2016-08-13 18:16:09 +00:00
'{summonerid}?api_key={token}'
2016-08-13 18:03:47 +00:00
.format(region=region, summonerid=summonerid, token=token))
# Ensure the request is successful
if r.status_code == 200:
return r.json()[str(summonerid)]
elif r.status_code == 404:
raise NoRankedGamesCompletedException("This player hasn't completed any ranked games.")
elif r.status_code == 429:
raise RateLimitException("You've been ratelimited by Riot. Check your developer dashboard.")
else:
raise Exception("Unhandled API response.")