1+ '''
2+ MIT License
3+
4+ Copyright (c) 2017 grokkers
5+
6+ Permission is hereby granted, free of charge, to any person obtaining a copy
7+ of this software and associated documentation files (the "Software"), to deal
8+ in the Software without restriction, including without limitation the rights
9+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ copies of the Software, and to permit persons to whom the Software is
11+ furnished to do so, subject to the following conditions:
12+
13+ The above copyright notice and this permission notice shall be included in all
14+ copies or substantial portions of the Software.
15+
16+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ SOFTWARE.
23+ '''
24+
125class Base :
226 '''
327 Base class for models. Only thing that
@@ -12,15 +36,12 @@ def __init__(self, client, data):
1236 self .from_data (data )
1337 endpoint = type (self ).__name__ .lower ()
1438 self .url = f'{ client .BASE } /{ endpoint } /{ self .tag } '
15-
16- def __str__ (self ):
17- return f'{ self .name } (#{ self .tag } )'
1839
1940 async def from_data (self ):
20- raise NotImplementedError
41+ return NotImplemented
2142
2243 async def update (self ):
23-
44+ '''Update an object with current info.'''
2445 async with self .client .session .get (self .url ) as resp :
2546 data = await resp .json ()
2647
@@ -80,7 +101,8 @@ def __repr__(self):
80101 return f'<Card id={ self .id } >'
81102
82103class Member :
83- def __init__ (self , client , data ):
104+ def __init__ (self , client , clan , data ):
105+ self .clan = clan
84106 self .client = client
85107 self .name = data .get ('name' )
86108 self .arena = Arena (data .get ('arena' ))
@@ -93,6 +115,9 @@ def __init__(self, client, data):
93115 self .crowns = data .get ('clanChestCrowns' )
94116 self .tag = data .get ('tag' )
95117
118+ def __str__ (self ):
119+ return f'{ self .name } (#{ self .tag } )'
120+
96121 def __repr__ (self ):
97122 return f'<Member tag={ self .tag } >'
98123
@@ -101,8 +126,8 @@ def get_profile(self):
101126
102127class Alliance :
103128 def __init__ (self , data ):
104- self .roles = [ c for c in data .get ('roles' )]
105- self .types = [ c for c in data .get ('types' )]
129+ self .roles = data .get ('roles' )
130+ self .types = data .get ('types' )
106131
107132class Country :
108133 def __init__ (self , data ):
@@ -149,6 +174,32 @@ def __init__(self, data):
149174 def __repr__ (self ):
150175 return f'<Card id={ self .id } >'
151176
177+ class ClanInfo :
178+ def __init__ (self , client , data ):
179+ self .raw_data = data
180+ self .name = data .get ('name' )
181+ self .tag = data .get ('tag' )
182+ self .trophies = data .get ('trophies' )
183+ self .region = data .get ('region' ).get ('name' )
184+ self .member_count = data .get ('memberCount' )
185+ self .rank = data .get ('rank' )
186+ self .previous_rank = data .get ('previousRank' )
187+
188+ @property
189+ def badge_url (self ):
190+ url = self .raw_data .get ('badge' ).get ('url' )
191+ return "http://api.cr-api.com" + url
192+
193+ def get_clan (self ):
194+ return self .client .get_clan (self .tag )
195+
196+ def __repr__ (self ):
197+ return f'<ClanInfo tag={ self .tag } >'
198+
199+ def __str__ (self ):
200+ return f'{ self .name } (#{ self .tag } )'
201+
202+
152203class Clan (Base ):
153204 '''Represents a clan'''
154205
@@ -163,15 +214,18 @@ def from_data(self, data):
163214 self .type_name = data .get ('typeName' )
164215 self .region = data .get ('region' ).get ('name' )
165216 self .clan_chest = ClanChest (data .get ('clanChest' ))
166- self .members = [Member (self .client , m ) for m in data .get ('members' )]
217+ self .members = [Member (self .client , self , m ) for m in data .get ('members' )]
167218
168219 @property
169220 def badge_url (self ):
170221 url = self .raw_data .get ('badge' ).get ('url' )
171- return f "http://api.cr-api.com{ url } "
222+ return "http://api.cr-api.com" + url
172223
173224 def __repr__ (self ):
174225 return f'<Clan tag={ self .tag } >'
226+
227+ def __str__ (self ):
228+ return f'{ self .name } (#{ self .tag } )'
175229
176230class Profile (Base ):
177231 '''Represents a player profile.
@@ -214,13 +268,16 @@ def clan_badge_url(self):
214268 if not url :
215269 return None
216270 else :
217- return f"http://api.cr-api.com{ url } "
271+ return "http://api.cr-api.com" + url
272+
273+ def get_clan (self ):
274+ return self .client .get_clan (self .clan_tag )
218275
219276 def __repr__ (self ):
220277 return f'<Profile tag={ self .tag } >'
221278
222- def get_clan (self ):
223- return self .client . get_clan ( self .clan_tag )
279+ def __str__ (self ):
280+ return f' { self .name } (# { self .tag } )'
224281
225282class Constants (Base ):
226283 '''Represents the constants from cr-api'''
@@ -231,4 +288,8 @@ def from_data(self, data):
231288 self .chest_cycle = [c for c in data .get ('chestCycle' ).get ('order' )]
232289 self .country_codes = [Country (c ) for c in data .get ('countryCodes' )]
233290 self .rarities = [Rarity (c ) for c in data .get ('rarities' )]
234- self .card = [CardInfo (c ) for c in data .get ('rarities' )]
291+ self .card = [CardInfo (c ) for c in data .get ('rarities' )]
292+
293+ def __repr__ (self ):
294+ return '<Clash Royale Constants Object>'
295+
0 commit comments