From f606253c348397240f89e632ee759a9e60c34c75 Mon Sep 17 00:00:00 2001 From: the fixer of typos <30674647+umbresp@users.noreply.github.com> Date: Thu, 22 Feb 2018 18:12:36 -0600 Subject: [PATCH] huge changes --- brawlstars/asyncclient.py | 122 +++++++++++++++++++++++++++++++++++++- brawlstars/client.py | 120 ++++++++++++++++++++++++++++++++++++- 2 files changed, 238 insertions(+), 4 deletions(-) diff --git a/brawlstars/asyncclient.py b/brawlstars/asyncclient.py index b3ae073..a8a5e55 100644 --- a/brawlstars/asyncclient.py +++ b/brawlstars/asyncclient.py @@ -114,6 +114,50 @@ async def get_band(self, tag): band = Band(data) return band + async def get_player_leaderboard(self): + + try: + async with self.session.get(self._base_url + 'leaderboards/players', timeout=self.timeout, headers=self.headers) as resp: + if resp.status == 200: + data = await resp.json() + elif 500 > resp.status > 400: + raise HTTPError(resp.status) + else: + raise Error() + except asyncio.TimeoutError: + raise Timeout() + except ValueError: + raise MissingData('data') + except: + raise InvalidArg('tag') + + + data = Box(data) + data = PlayerLeaderboard(data) + return data + + async def get_band_leaderboard(self): + + try: + async with self.session.get(self._base_url + 'leaderboards/bands', timeout=self.timeout, headers=self.headers) as resp: + if resp.status == 200: + data = await resp.json() + elif 500 > resp.status > 400: + raise HTTPError(resp.status) + else: + raise Error() + except asyncio.TimeoutError: + raise Timeout() + except ValueError: + raise MissingData('data') + except: + raise InvalidArg('tag') + + + data = Box(data) + data = BandLeaderboard(data) + return data + class Player(Box): def __str__(self): @@ -191,7 +235,7 @@ async def get_id(self): async def get_members(self): try: memberList = self.bandMembers - except AttributError: + except AttributeError: return None members = [] for i in memberList: @@ -232,3 +276,79 @@ def __str__(self): def __repr__(self): return '' + +class PlayerLeaderboard(Box): + + def __str__(self): + return 'Player Leaderboard' + + def __repr__(self): + return '' + + async def get_players(self): + try: + memberList = self.players + except AttributeError: + return None + members = [] + for i in memberList: + thing = Box(i) + thing = RankedPlayer(thing) + members.append(thing) + + return members + +class RankedPlayer(Box): + + def __str__(self): + return self.name + ' (#' + self.tag + '), ranked #' + self.positionInLeaderboard + + def __repr__(self): + return '' + + async def get_id(self): + try: + ret = self.id + except AttributeError: + return None + ret = Box(ret) + ret = Id(ret) + return ret + +class BandLeaderboard(Box): + + def __str__(self): + return 'Band Leaderboard' + + def __repr__(self): + return '' + + async def get_bands(self): + try: + memberList = self.bands + except AttributeError: + return None + members = [] + for i in memberList: + thing = Box(i) + thing = RankedBand(thing) + members.append(thing) + + return members + +class RankedBand(Box): + + def __str__(self): + return self.name + ' (#' + self.tag + '), ranked #' + self.positionInLeaderboard + + def __repr__(self): + return '' + + async def get_id(self): + try: + ret = self.id + except AttributeError: + return None + ret = Box(ret) + ret = Id(ret) + return ret diff --git a/brawlstars/client.py b/brawlstars/client.py index 2f40cb7..6df633f 100644 --- a/brawlstars/client.py +++ b/brawlstars/client.py @@ -106,6 +106,44 @@ def get_band(self, tag): band = Band(data) return band + def get_player_leaderboard(self): + + try: + resp = requests.get(self._base_url + 'leaderboards/players', headers=self.headers, timeout=self.timeout) + if resp.status_code == 200: + data = resp.json() + elif 500 > resp.status_code > 400: + raise HTTPError(resp.status_code) + else: + raise Error() + except ValueError: + raise MissingData('data') + except: + raise Timeout() + + data = Box(data) + data = PlayerLeaderboard(data) + return data + + def get_band_leaderboard(self): + + try: + resp = requests.get(self._base_url + 'leaderboards/bands', headers=self.headers, timeout=self.timeout) + if resp.status_code == 200: + data = resp.json() + elif 500 > resp.status_code > 400: + raise HTTPError(resp.status_code) + else: + raise Error() + except ValueError: + raise MissingData('data') + except: + raise Timeout() + + data = Box(data) + data = BandLeaderboard(data) + return data + class Player(Box): def __str__(self): @@ -140,7 +178,7 @@ def get_brawlers(self): def get_band(self): try: band = self.band - except AttributError: + except AttributeError: return None band = Box(band) band = MinimalBand(band) @@ -184,7 +222,7 @@ def get_id(self): def get_members(self): try: memberList = self.bandMembers - except AttributError: + except AttributeError: return None members = [] for i in memberList: @@ -225,4 +263,80 @@ def __str__(self): return self.name + ' (' + self.trophies + ' trophies)' def __repr__(self): - return '' \ No newline at end of file + return '' + +class PlayerLeaderboard(Box): + + def __str__(self): + return 'Player Leaderboard' + + def __repr__(self): + return '' + + def get_players(self): + try: + memberList = self.players + except AttributeError: + return None + members = [] + for i in memberList: + thing = Box(i) + thing = RankedPlayer(thing) + members.append(thing) + + return members + +class RankedPlayer(Box): + + def __str__(self): + return self.name + ' (#' + self.tag + '), ranked #' + self.positionInLeaderboard + + def __repr__(self): + return '' + + def get_id(self): + try: + ret = self.id + except AttributeError: + return None + ret = Box(ret) + ret = Id(ret) + return ret + +class BandLeaderboard(Box): + + def __str__(self): + return 'Band Leaderboard' + + def __repr__(self): + return '' + + def get_bands(self): + try: + memberList = self.bands + except AttributeError: + return None + members = [] + for i in memberList: + thing = Box(i) + thing = RankedBand(thing) + members.append(thing) + + return members + +class RankedBand(Box): + + def __str__(self): + return self.name + ' (#' + self.tag + '), ranked #' + self.positionInLeaderboard + + def __repr__(self): + return '' + + async def get_id(self): + try: + ret = self.id + except AttributeError: + return None + ret = Box(ret) + ret = Id(ret) + return ret \ No newline at end of file