1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2025-02-17 10:53:57 +00:00
royalnet/overwatch.py

33 lines
1.3 KiB
Python
Raw Normal View History

2016-08-12 16:40:22 +02:00
import asyncio
2016-08-12 16:58:22 +02:00
import requests
2016-08-12 17:09:47 +02:00
loop = asyncio.get_event_loop()
2016-08-12 16:40:22 +02:00
class NotFoundException(Exception):
pass
2016-08-12 16:58:22 +02:00
# Get player data
2016-08-12 19:21:43 +02:00
async def get_player_data(platform: str, region: str, battletag: str, **kwargs):
2016-08-13 15:46:07 +02:00
print("[Overwatch] Getting player info for: {platform} {region} {battletag}".format(platform=platform,
region=region,
battletag=battletag))
2016-08-12 16:58:22 +02:00
# Unofficial API requires - for discriminator numbers
2016-08-12 17:09:47 +02:00
battletag = battletag.replace("#", "-")
2016-08-12 16:58:22 +02:00
# GET the json unofficial API response
2016-08-12 17:09:47 +02:00
r = await loop.run_in_executor(None, requests.get,
'https://api.lootbox.eu/{platform}/{region}/{battletag}/profile'.format(**locals()))
2016-08-12 16:58:22 +02: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 21:12:51 +02:00
else:
raise Exception("Unhandled API response.")