1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00

omg funziona

This commit is contained in:
Steffo 2017-04-28 12:55:36 +02:00
parent 8fec58827f
commit 72b7ed2276
3 changed files with 53 additions and 44 deletions

View file

@ -89,39 +89,41 @@ def new_diario_entry(dt, text):
session.commit() session.commit()
# TODO: improve this async def update_lol(discord_id):
async def update_lol(lid):
# Create a new database session # Create a new database session
session = Session() session = Session()
# Find the user # Find the user
user = session.query(Account).join(LoL).filter_by(id=lid).first() user = session.query(Account).filter_by(id=discord_id).join(LoL).first()
# Poll the League API for more information for account in user.lol:
data = await lol.get_summoner_data("euw", summoner_id=user.lol.id) # Find the League of Legends ID
# Update the user data lid = account.id
user.lol.summoner_name = data["name"] # Poll the League API for more information
user.lol.level = data["level"] data = await lol.get_summoner_data("euw", summoner_id=lid)
# Poll the League API for ranked data # Update the user data
soloq, flexq, ttq = await lol.get_rank_data("euw", lid) account.summoner_name = data["name"]
# Update the user data account.level = data["summonerLevel"]
if soloq is not None: # Poll the League API for ranked data
user.lol.soloq_tier = lol.tiers[soloq["tier"]] soloq, flexq, ttq = await lol.get_rank_data("euw", lid)
user.lol.soloq_division = lol.divisions[soloq["entries"][0]["division"]] # Update the user data
else: if soloq is not None:
user.lol.soloq_tier = None account.soloq_tier = lol.tiers[soloq["tier"]]
user.lol.soloq_division = None account.soloq_division = lol.divisions[soloq["entries"][0]["division"]]
if flexq is not None: else:
user.lol.flexq_tier = lol.tiers[flexq["tier"]] account.soloq_tier = None
user.lol.flexq_division = lol.divisions[flexq["entries"][0]["division"]] account.soloq_division = None
else: if flexq is not None:
user.lol.flexq_tier = None account.flexq_tier = lol.tiers[flexq["tier"]]
user.lol.flexq_division = None account.flexq_division = lol.divisions[flexq["entries"][0]["division"]]
if ttq is not None: else:
user.lol.ttq_tier = lol.tiers[ttq["tier"]] account.flexq_tier = None
user.lol.ttq_division = lol.divisions[ttq["entries"][0]["division"]] account.flexq_division = None
else: if ttq is not None:
user.lol.ttq_tier = None account.ttq_tier = lol.tiers[ttq["tier"]]
user.lol.ttq_division = None account.ttq_division = lol.divisions[ttq["entries"][0]["division"]]
# Mark the user as updated else:
user.lol.last_updated = datetime.datetime.now() account.ttq_tier = None
account.ttq_division = None
# Mark the user as updated
account.last_updated = datetime.datetime.now()
# Commit the changes # Commit the changes
session.commit() session.commit()

22
lol.py
View file

@ -1,7 +1,6 @@
import asyncio import asyncio
import aiohttp import aiohttp
import royalbotconfig import royalbotconfig
import enum
# https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/52348350?api_key=RGAPI-1008c33d-b0a4-4091-8600-27022d570964 # https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/52348350?api_key=RGAPI-1008c33d-b0a4-4091-8600-27022d570964
@ -46,23 +45,26 @@ async def get_summoner_data(region: str, summoner_id=None, summoner_name=None):
"api_key": royalbotconfig.lol_token "api_key": royalbotconfig.lol_token
} }
if summoner_id is not None: if summoner_id is not None:
data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/{summoner_id}") data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/{summoner_id}", params=params)
return data[summoner_id] return data[str(summoner_id)]
elif summoner_name is not None: elif summoner_name is not None:
data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/by-name/{summoner_name}") 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] return data[summoner_name.lower().replace(" ", "")]
async def get_rank_data(region: str, summoner_id: int): async def get_rank_data(region: str, summoner_id: int):
data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v2.5/league/by-summoner/{summoner_id}/entry") 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)
soloq = None soloq = None
flexq = None flexq = None
ttq = None ttq = None
for entry in data: for entry in data[str(summoner_id)]:
if data["queue"] == "RANKED_SOLO_5x5": if entry["queue"] == "RANKED_SOLO_5x5":
soloq = entry soloq = entry
elif data["queue"] == "RANKED_FLEX_SR": elif entry["queue"] == "RANKED_FLEX_SR":
flexq = entry flexq = entry
elif data["queue"] == "RANKED_FLEX_TT": elif entry["queue"] == "RANKED_FLEX_TT":
ttq = entry ttq = entry
return soloq, flexq, ttq return soloq, flexq, ttq

View file

@ -261,7 +261,7 @@ Sintassi: `{symbol}synclol <nome evocatore>`"""
# Set status to typing # Set status to typing
await status_typing(bot, thing) await status_typing(bot, thing)
# Check the command syntax # Check the command syntax
if len(arguments) != 1: if len(arguments) < 1:
await display_help(bot, thing, synclol) await display_help(bot, thing, synclol)
return return
# Open a new database session # Open a new database session
@ -275,11 +275,16 @@ Sintassi: `{symbol}synclol <nome evocatore>`"""
# TODO: IMPROVE THIS # TODO: IMPROVE THIS
summoner_name = " ".join(arguments) summoner_name = " ".join(arguments)
data = await lol.get_summoner_data("euw", summoner_name=summoner_name) data = await lol.get_summoner_data("euw", summoner_name=summoner_name)
lolaccount = database.LoL(data["id"]) lolaccount = database.LoL(id=data["id"], summoner_name=summoner_name)
lolaccount.parentid = user.id lolaccount.parentid = user.id
session.add(lolaccount) session.add(lolaccount)
session.commit() session.commit()
database.update_lol(data["id"]) await database.update_lol(user.id)
async def update_stats(timeout):
await asyncio.sleep(timeout)
loop.create_task()
if __name__ == "__main__": if __name__ == "__main__":
# Init universal bot commands # Init universal bot commands