1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-17 23:24:20 +00:00
royalnet/overwatch.py

80 lines
3.1 KiB
Python
Raw Normal View History

2016-08-12 14:40:22 +00:00
import asyncio
2016-08-12 14:58:22 +00:00
import requests
2016-11-02 13:45:41 +00:00
import strings as s
2016-08-12 15:09:47 +00:00
loop = asyncio.get_event_loop()
2016-08-12 14:40:22 +00:00
class NotFoundException(Exception):
pass
2016-08-12 14:58:22 +00:00
# Get player data
2016-08-12 17:21:43 +00:00
async def get_player_data(platform: str, region: str, battletag: str, **kwargs):
2016-08-13 13:46:07 +00:00
print("[Overwatch] Getting player info for: {platform} {region} {battletag}".format(platform=platform,
region=region,
battletag=battletag))
2016-08-12 14:58:22 +00:00
# Unofficial API requires - for discriminator numbers
2016-08-12 15:09:47 +00:00
battletag = battletag.replace("#", "-")
2016-08-12 14:58:22 +00:00
# GET the json unofficial API response
2016-08-12 15:09:47 +00:00
r = await loop.run_in_executor(None, requests.get,
'https://api.lootbox.eu/{platform}/{region}/{battletag}/profile'.format(**locals()))
2016-08-12 14:58:22 +00:00
# Ensure the request is successful
if r.status_code == 200:
# Parse json and check for the status code inside the response
pj = r.json()
if "statusCode" in pj:
if pj["statusCode"] == 404:
raise NotFoundException("Player not found.")
else:
raise Exception("Unhandled API response.")
else:
# Success!
return pj
2016-08-12 19:12:51 +00:00
else:
raise Exception("Unhandled API response.")
2016-11-02 13:45:41 +00:00
# Convert rank to a medal
def rank_to_medal(rank):
2016-11-02 13:49:46 +00:00
if int(rank) == 0:
2016-11-02 13:45:41 +00:00
return s.overwatch_medal_list[0]
2016-11-02 13:49:46 +00:00
if int(rank) < 1500:
2016-11-02 13:45:41 +00:00
return s.overwatch_medal_list[1]
2016-11-02 13:49:46 +00:00
elif int(rank) < 2000:
2016-11-02 13:45:41 +00:00
return s.overwatch_medal_list[2]
2016-11-02 13:49:46 +00:00
elif int(rank) < 2500:
2016-11-02 13:45:41 +00:00
return s.overwatch_medal_list[3]
2016-11-02 13:49:46 +00:00
elif int(rank) < 3000:
2016-11-02 13:45:41 +00:00
return s.overwatch_medal_list[4]
2016-11-02 13:49:46 +00:00
elif int(rank) < 3500:
2016-11-02 13:45:41 +00:00
return s.overwatch_medal_list[5]
2016-11-02 13:49:46 +00:00
elif int(rank) < 4000:
return s.overwatch_medal_list[6]
2017-01-09 07:37:09 +00:00
else:
# TODO: what
raise Exception("Mi sfugge qualcosa.")
# Convert an url to a medal
def url_to_medal(rank: str):
if rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-1.png":
return s.overwatch_medal_list[1]
2017-01-20 22:23:28 +00:00
elif rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-2.png":
2017-01-09 07:37:09 +00:00
return s.overwatch_medal_list[2]
2017-01-20 22:23:28 +00:00
elif rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-3.png":
2017-01-09 07:37:09 +00:00
return s.overwatch_medal_list[3]
2017-01-20 22:23:28 +00:00
elif rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-4.png":
2017-01-09 07:37:09 +00:00
return s.overwatch_medal_list[4]
2017-01-20 22:23:28 +00:00
elif rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-5.png":
2017-01-09 07:37:09 +00:00
return s.overwatch_medal_list[5]
2017-01-20 22:23:28 +00:00
elif rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png":
2017-01-09 07:37:09 +00:00
return s.overwatch_medal_list[6]
2017-01-20 22:23:28 +00:00
elif rank == "https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-7.png":
return s.overwatch_medal_list[7]
2017-01-09 07:37:09 +00:00
else:
raise NotFoundException("The medal does not exist.")
def format_rankchange(rankchange: int):
2017-01-22 22:28:44 +00:00
if rankchange > 0:
return "+" + str(rankchange)
else:
return str(rankchange)