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
+
1
25
class Base :
2
26
'''
3
27
Base class for models. Only thing that
@@ -12,15 +36,12 @@ def __init__(self, client, data):
12
36
self .from_data (data )
13
37
endpoint = type (self ).__name__ .lower ()
14
38
self .url = f'{ client .BASE } /{ endpoint } /{ self .tag } '
15
-
16
- def __str__ (self ):
17
- return f'{ self .name } (#{ self .tag } )'
18
39
19
40
async def from_data (self ):
20
- raise NotImplementedError
41
+ return NotImplemented
21
42
22
43
async def update (self ):
23
-
44
+ '''Update an object with current info.'''
24
45
async with self .client .session .get (self .url ) as resp :
25
46
data = await resp .json ()
26
47
@@ -80,7 +101,8 @@ def __repr__(self):
80
101
return f'<Card id={ self .id } >'
81
102
82
103
class Member :
83
- def __init__ (self , client , data ):
104
+ def __init__ (self , client , clan , data ):
105
+ self .clan = clan
84
106
self .client = client
85
107
self .name = data .get ('name' )
86
108
self .arena = Arena (data .get ('arena' ))
@@ -93,6 +115,9 @@ def __init__(self, client, data):
93
115
self .crowns = data .get ('clanChestCrowns' )
94
116
self .tag = data .get ('tag' )
95
117
118
+ def __str__ (self ):
119
+ return f'{ self .name } (#{ self .tag } )'
120
+
96
121
def __repr__ (self ):
97
122
return f'<Member tag={ self .tag } >'
98
123
@@ -101,8 +126,8 @@ def get_profile(self):
101
126
102
127
class Alliance :
103
128
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' )
106
131
107
132
class Country :
108
133
def __init__ (self , data ):
@@ -149,6 +174,32 @@ def __init__(self, data):
149
174
def __repr__ (self ):
150
175
return f'<Card id={ self .id } >'
151
176
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
+
152
203
class Clan (Base ):
153
204
'''Represents a clan'''
154
205
@@ -163,15 +214,18 @@ def from_data(self, data):
163
214
self .type_name = data .get ('typeName' )
164
215
self .region = data .get ('region' ).get ('name' )
165
216
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' )]
167
218
168
219
@property
169
220
def badge_url (self ):
170
221
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
172
223
173
224
def __repr__ (self ):
174
225
return f'<Clan tag={ self .tag } >'
226
+
227
+ def __str__ (self ):
228
+ return f'{ self .name } (#{ self .tag } )'
175
229
176
230
class Profile (Base ):
177
231
'''Represents a player profile.
@@ -214,13 +268,16 @@ def clan_badge_url(self):
214
268
if not url :
215
269
return None
216
270
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 )
218
275
219
276
def __repr__ (self ):
220
277
return f'<Profile tag={ self .tag } >'
221
278
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 } )'
224
281
225
282
class Constants (Base ):
226
283
'''Represents the constants from cr-api'''
@@ -231,4 +288,8 @@ def from_data(self, data):
231
288
self .chest_cycle = [c for c in data .get ('chestCycle' ).get ('order' )]
232
289
self .country_codes = [Country (c ) for c in data .get ('countryCodes' )]
233
290
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