1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/lol.py

59 lines
2.2 KiB
Python
Raw Normal View History

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):
2017-04-28 17:31:24 +00:00
def __init__(self, status_code, text):
self.status_code = status_code
self.text = text
2017-04-26 08:46:36 +00:00
2017-04-28 16:54:20 +00:00
tiers = ["BRONZE", "SILVER", "GOLD", "PLATINUM", "DIAMOND", "MASTER", "CHALLENGER"]
divisions = ["I", "II", "III", "IV", "V"]
2017-04-26 08:46:36 +00:00
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:
2017-04-28 17:31:24 +00:00
raise LoLAPIError(response.status, f"Riot API returned {response.status}")
2017-04-26 08:46:36 +00:00
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