Skip to content

Commit 6ae7d1c

Browse files
authored
Merge pull request #62 from 0dminnimda/issue_60_making_get_brawlers
Issue 60 making get brawlers
2 parents 3baeb65 + 3f863ce commit 6ae7d1c

14 files changed

Lines changed: 113 additions & 47 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
5+
## [Unnoun yet] - 6/27/20
6+
### Added
7+
- `get_brawlers` function to get available brawlers
8+
### Changed
9+
- splitting `BaseBox` into `BaseBox` and `BaseBoxList` for convenience
10+
### Fixed
11+
- tox for py38 works (not completely sure)
12+
13+
## [4.0.3] - 4/17/20
14+
### Fixed
15+
- Brawler leaderboards for Python 3.5
16+
417
## [4.0.2] - 4/15/20
518
### Fixed
619
- Player model bug

brawlstats/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
############
99

1010

11-
__version__ = 'v4.0.2'
11+
__version__ = 'v4.0.3'
1212
__title__ = 'brawlstats'
1313
__license__ = 'MIT'
1414
__author__ = 'SharpBit'

brawlstats/core.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from cachetools import TTLCache
1010

1111
from .errors import Forbidden, NotFoundError, RateLimitError, ServerError, UnexpectedError
12-
from .models import BattleLog, Club, Constants, Members, Player, Ranking
12+
from .models import BattleLog, Brawlers, Club, Constants, Members, Player, Ranking
1313
from .utils import API, bstag, typecasted
1414

1515
log = logging.getLogger(__name__)
@@ -310,3 +310,13 @@ def get_constants(self, key=None):
310310
Returns Constants
311311
"""
312312
return self._get_model(self.api.CONSTANTS, model=Constants, key=key)
313+
314+
def get_brawlers(self):
315+
"""
316+
Get available brawlers and information about them.
317+
318+
No parameters
319+
320+
Returns Brawlers
321+
"""
322+
return self._get_model(self.api.BRAWLERS_URL, model=Brawlers)

brawlstats/models.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from .utils import bstag
33

44

5-
__all__ = ['Player', 'Club', 'Members', 'Ranking', 'BattleLog', 'Constants']
5+
__all__ = ['Player', 'Club', 'Members', 'Ranking', 'BattleLog', 'Constants', 'Brawlers']
6+
67

78
class BaseBox:
89
def __init__(self, client, data):
@@ -11,14 +12,7 @@ def __init__(self, client, data):
1112

1213
def from_data(self, data):
1314
self.raw_data = data
14-
if isinstance(data, list):
15-
self._boxed_data = BoxList(
16-
data, camel_killer_box=True
17-
)
18-
else:
19-
self._boxed_data = Box(
20-
data, camel_killer_box=True
21-
)
15+
self._boxed_data = Box(data, camel_killer_box=True)
2216
return self
2317

2418
def __getattr__(self, attr):
@@ -37,6 +31,17 @@ def __getitem__(self, item):
3731
raise IndexError('No such index: {}'.format(item))
3832

3933

34+
class BaseBoxList(BaseBox):
35+
def from_data(self, data):
36+
data = data['items']
37+
self.raw_data = data
38+
self._boxed_data = BoxList(data, camel_killer_box=True)
39+
return self
40+
41+
def __len__(self):
42+
return sum(1 for i in self)
43+
44+
4045
class Player(BaseBox):
4146
"""
4247
Returns a full player object with all of its attributes.
@@ -85,47 +90,45 @@ def get_members(self):
8590
return self.client._get_model(url, model=Members)
8691

8792

88-
class Members(BaseBox):
93+
class Members(BaseBoxList):
8994
"""
9095
Returns the members in a club.
9196
"""
9297

93-
def __init__(self, client, data):
94-
super().__init__(client, data['items'])
95-
96-
def __len__(self):
97-
return sum(1 for i in self)
98-
9998
def __repr__(self):
10099
return '<Members object count={}>'.format(len(self))
101100

102101

103-
class Ranking(BaseBox):
102+
class Ranking(BaseBoxList):
104103
"""
105104
Returns a player or club ranking that contains a list of players or clubs.
106105
"""
107106

108-
def __init__(self, client, data):
109-
super().__init__(client, data['items'])
110-
111-
def __len__(self):
112-
return sum(1 for i in self)
113-
114107
def __repr__(self):
115108
return '<Ranking object count={}>'.format(len(self))
116109

117110

118-
class BattleLog(BaseBox):
111+
class BattleLog(BaseBoxList):
119112
"""
120113
Returns a full player battle object with all of its attributes.
121114
"""
122-
123-
def __init__(self, client, data):
124-
super().__init__(client, data['items'])
115+
pass
125116

126117

127118
class Constants(BaseBox):
128119
"""
129120
Returns some Brawl Stars constants.
130121
"""
131122
pass
123+
124+
125+
class Brawlers(BaseBoxList):
126+
"""
127+
Returns list of available brawlers and information about them.
128+
"""
129+
130+
def __repr__(self):
131+
return '<Brawlers object count={}>'.format(len(self))
132+
133+
def __str__(self):
134+
return 'Here {} brawlers'.format(len(self))

brawlstats/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self, base_url, version=1):
1616
self.CLUB = self.BASE + '/clubs'
1717
self.RANKINGS = self.BASE + '/rankings'
1818
self.CONSTANTS = 'https://fourjr.herokuapp.com/bs/constants'
19+
self.BRAWLERS_URL = self.BASE + "/brawlers"
1920

2021
# Get package version from __init__.py
2122
path = os.path.dirname(__file__)
@@ -55,6 +56,7 @@ def bstag(tag):
5556

5657
return tag
5758

59+
5860
def get_datetime(timestamp: str, unix=True):
5961
"""
6062
Converts a %Y%m%dT%H%M%S.%fZ to a UNIX timestamp
@@ -76,6 +78,12 @@ def get_datetime(timestamp: str, unix=True):
7678
else:
7779
return time
7880

81+
82+
# do nothing
83+
def nothing(value):
84+
return value
85+
86+
7987
def typecasted(func):
8088
"""Decorator that converts arguments via annotations.
8189
Source: https://github.com/cgrok/clashroyale/blob/master/clashroyale/official_api/utils.py#L11"""
@@ -89,7 +97,7 @@ def wrapper(*args, **kwargs):
8997
for _, param in signature:
9098
converter = param.annotation
9199
if converter is inspect._empty:
92-
converter = lambda a: a # do nothing
100+
converter = nothing
93101
if param.kind is param.POSITIONAL_OR_KEYWORD:
94102
if args:
95103
to_conv = args.pop(0)

docs/api.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Data Models
2525
.. autoclass:: brawlstats.models.Constants
2626
:members:
2727

28+
.. autoclass:: brawlstats.models.Brawlers
29+
:members:
30+
2831

2932
Attributes of Data Models
3033
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -264,3 +267,16 @@ Attributes:
264267
]
265268
}
266269
}
270+
271+
Brawlers
272+
~~~~~~~~
273+
274+
Returns list of available brawlers and information about them with this structure:
275+
276+
Attributes:
277+
278+
::
279+
280+
[
281+
Brawler
282+
]

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Features
2424
- Get a player profile and battlelog.
2525
- Get a club and its members.
2626
- Get the top 200 rankings for players, clubs, or a specific brawler.
27-
- Get information about maps, brawlers, and more!
27+
- Get information about maps and more!
28+
- Get information about current available brawlers.
2829

2930
Installation
3031
~~~~~~~~~~~~

examples/async.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import brawlstats
22
import asyncio
33

4+
# Do not post your token on a public github!
45
client = brawlstats.Client('token', is_async=True)
5-
# Do not post your token on a public github
6+
67

78
# await only works in an async loop
89
async def main():
910
player = await client.get_profile('GGJVJLU2')
1011
print(player.trophies) # access attributes using dot.notation
11-
print(player.solo_victories) # access using snake_case instead of camelCase
12+
print(player.solo_victories) # use snake_case instead of camelCase
1213

1314
club = await player.get_club()
1415
print(club.tag)
15-
members = await club.get_members()
16-
best_players = members[:5] # members sorted by trophies, gets best 5 players
16+
members = await club.get_members() # members sorted by trophies
17+
best_players = members[:5] # gets best 5 players
1718
for player in best_players:
1819
print(player.name, player.trophies)
1920

20-
ranking = await client.get_rankings(ranking='players', limit=5) # gets top 5 players
21+
# get top 5 players in the world
22+
ranking = await client.get_rankings(ranking='players', limit=5)
2123
for player in ranking:
2224
print(player.name, player.rank)
2325

24-
# Get top 5 mortis players in the US
26+
# get top 5 mortis players in the US
2527
ranking = await client.get_rankings(
2628
ranking='brawlers',
2729
region='us',

examples/discord_cog.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from discord.ext import commands
33
import brawlstats
44

5+
56
class BrawlStars(commands.Cog, name='Brawl Stars'):
67
"""A simple cog for Brawl Stars commands using discord.py"""
78

@@ -17,6 +18,7 @@ async def profile(self, ctx, tag: str):
1718
except brawlstats.RequestError as e: # catches all exceptions
1819
return await ctx.send('```\n{}: {}\n```'.format(e.code, e.message)) # sends code and error message
1920
em = discord.Embed(title='{0.name} ({0.tag})'.format(player))
21+
2022
em.description = 'Trophies: {}'.format(player.trophies) # you could make this better by using embed fields
2123
await ctx.send(embed=em)
2224

examples/sync.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import brawlstats
22

3+
# Do not post your token on a public github!
34
client = brawlstats.Client('token')
4-
# Do not post your token on a public github
5+
56

67
player = client.get_profile('GGJVJLU2')
78
print(player.trophies) # access attributes using dot.notation
8-
print(player.solo_victories) # access using snake_case instead of camelCase
9+
print(player.solo_victories) # use snake_case instead of camelCase
910

1011
club = player.get_club()
1112
print(club.tag)
12-
best_players = club.get_members()[:5] # members sorted by trophies, gets best 5 players
13+
members = club.get_members() # members sorted by trophies
14+
best_players = members[:5] # gets best 5 players
1315
for player in best_players:
1416
print(player.name, player.trophies) # prints name and trophies
1517

16-
ranking = client.get_rankings(ranking='players', limit=5) # gets top 5 players
18+
# gets top 5 players in the world
19+
ranking = client.get_rankings(ranking='players', limit=5)
1720
for player in ranking:
1821
print(player.name, player.rank)
1922

20-
# Get top 5 mortis players in the US
23+
# get top 5 mortis players in the US
2124
ranking = client.get_rankings(
2225
ranking='brawlers',
2326
region='us',

0 commit comments

Comments
 (0)