From 84d954c305cb6622ef58bd26812956289b07b18d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 16 Aug 2016 22:16:22 +0200 Subject: [PATCH] Added brawlhalla integration --- brawlhalla.py | 15 +++++++++++++++ main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ strings.py | 3 +++ 3 files changed, 67 insertions(+) create mode 100644 brawlhalla.py diff --git a/brawlhalla.py b/brawlhalla.py new file mode 100644 index 00000000..868c7070 --- /dev/null +++ b/brawlhalla.py @@ -0,0 +1,15 @@ +import asyncio +import requests +import bs4 +loop = asyncio.get_event_loop() + +# Get ladder page for a player +async def get_leaderboard_for(name: str): + print("[Brawlhalla] Getting leaderboards page for {name}".format(name=name)) + # Get leaderboards page for that name + r = loop.run_in_executor(None, requests.get, "http://www.brawlhalla.com/rankings/1v1/eu/?p={name}".format(name)) + # Check if the request is successful + if r.status_code == 200: + return r + else: + raise Exception("Something went wrong in the Brawlhalla request.") diff --git a/main.py b/main.py index da88e946..0a465086 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,8 @@ import overwatch import league import strings as s import telegram +import bs4 +import brawlhalla loop = asyncio.get_event_loop() d_client = discord.Client() @@ -145,6 +147,53 @@ async def league_level_up(timeout): else: await asyncio.sleep(1) +# Every timeout seconds, update player mmr and rank +async def brawlhalla_update_mmr(timeout): + while True: + if discord_is_ready: + print("[Brawlhalla] Starting check for mmr changes...") + # Update mmr for every player in list + for player in db: + if "brawlhalla" in db[player]: + try: + r = await brawlhalla.get_leaderboard_for(db[player]["brawlhalla"]["username"]) + except Exception: + print("[Brawlhalla] Request returned an unhandled exception.") + else: + # Parse the page + bs = bs4.BeautifulSoup(r.text) + # Divide the page into rows + rows = bs.find_all("tr") + # Find the row containing the rank + for row in rows: + # Skip header rows + if row['class'] == "rheader": + continue + # Check if the row belongs to the correct player + # (Brawlhalla searches aren't case sensitive) + for column in row.children: + # Find the player name column + if column['class'] == "pnameleft": + # Check if the name matches the parameter + if column.string == db[player]["brawlhalla"]["username"]: + break + else: + continue + # Get the current mmr + mmr = int(row.children[7].string) + # Compare the mmr with the value saved in the database + if mmr != db[player]["brawlhalla"]["mmr"]: + # Send a message + loop.create_task(send_event(s.brawlhalla_new_mmr, player, mmr=mmr)) + # Update database + db[player]["brawlhalla"]["mmr"] = mmr + f = open("db.json", "w") + json.dump(db, f) + f.close() + break + finally: + await asyncio.sleep(1) + # Send a new event to both Discord and Telegram async def send_event(eventmsg: str, player: str, **kwargs): # Create arguments dict diff --git a/strings.py b/strings.py index e926af41..66959787 100644 --- a/strings.py +++ b/strings.py @@ -17,3 +17,6 @@ league_tier_list = ["Bronzo", # League of Legends: Level up! league_level_up = "{player} รจ salito al livello **{level}** su _League of Legends_!" + +# Brawlhalla: new MMR +brawlhalla_new_mmr = "{player} ora ha **{mmr}** MMR su _Brawlhalla_!"