2017-04-26 08:46:36 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import royalbotconfig
|
|
|
|
|
|
|
|
# https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/52348350?api_key=RGAPI-1008c33d-b0a4-4091-8600-27022d570964
|
|
|
|
|
|
|
|
class LoLAPIError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
tiers = {
|
|
|
|
"BRONZE": 0,
|
|
|
|
"SILVER": 1,
|
|
|
|
"GOLD": 2,
|
|
|
|
"PLATINUM": 3,
|
|
|
|
"DIAMOND": 4,
|
|
|
|
"MASTER": 5,
|
|
|
|
"CHALLENGER": 6
|
|
|
|
}
|
|
|
|
|
|
|
|
divisions = {
|
|
|
|
"I": 0,
|
|
|
|
"II": 1,
|
|
|
|
"III": 2,
|
|
|
|
"IV": 3,
|
|
|
|
"V": 4
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def get_json(url, **kwargs):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(url, **kwargs) as response:
|
2017-04-26 09:59:44 +00:00
|
|
|
json = await response.json()
|
2017-04-26 08:46:36 +00:00
|
|
|
if response.status != 200:
|
|
|
|
raise LoLAPIError(f"Riot API returned {response.status}")
|
|
|
|
return json
|
|
|
|
|
|
|
|
|
|
|
|
async def get_summoner_data(region: str, summoner_id=None, summoner_name=None):
|
|
|
|
# Check for the number of arguments
|
|
|
|
if bool(summoner_id) == bool(summoner_name):
|
|
|
|
# TODO: use the correct exception
|
|
|
|
raise Exception("Invalid number of arguments specified")
|
|
|
|
params = {
|
|
|
|
"api_key": royalbotconfig.lol_token
|
|
|
|
}
|
|
|
|
if summoner_id is not None:
|
2017-04-28 10:55:36 +00:00
|
|
|
data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/{summoner_id}", params=params)
|
|
|
|
return data[str(summoner_id)]
|
2017-04-26 08:46:36 +00:00
|
|
|
elif summoner_name is not None:
|
2017-04-28 10:55:36 +00:00
|
|
|
data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/by-name/{summoner_name}", params=params)
|
|
|
|
return data[summoner_name.lower().replace(" ", "")]
|
2017-04-26 08:46:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_rank_data(region: str, summoner_id: int):
|
2017-04-28 10:55:36 +00:00
|
|
|
params = {
|
|
|
|
"api_key": royalbotconfig.lol_token
|
|
|
|
}
|
|
|
|
data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v2.5/league/by-summoner/{summoner_id}/entry", params=params)
|
2017-04-26 08:46:36 +00:00
|
|
|
soloq = None
|
|
|
|
flexq = None
|
|
|
|
ttq = None
|
2017-04-28 10:55:36 +00:00
|
|
|
for entry in data[str(summoner_id)]:
|
|
|
|
if entry["queue"] == "RANKED_SOLO_5x5":
|
2017-04-26 08:46:36 +00:00
|
|
|
soloq = entry
|
2017-04-28 10:55:36 +00:00
|
|
|
elif entry["queue"] == "RANKED_FLEX_SR":
|
2017-04-26 08:46:36 +00:00
|
|
|
flexq = entry
|
2017-04-28 10:55:36 +00:00
|
|
|
elif entry["queue"] == "RANKED_FLEX_TT":
|
2017-04-26 08:46:36 +00:00
|
|
|
ttq = entry
|
|
|
|
return soloq, flexq, ttq
|