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

Added brawlhalla integration

This commit is contained in:
Steffo 2016-08-16 22:16:22 +02:00
parent 6aacc1524d
commit 84d954c305
3 changed files with 67 additions and 0 deletions

15
brawlhalla.py Normal file
View file

@ -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.")

49
main.py
View file

@ -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

View file

@ -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_!"