Skip to content
This repository was archived by the owner on Aug 19, 2018. It is now read-only.

Commit 757ad3b

Browse files
committed
Added top clans support and licence
1 parent 784e125 commit 757ad3b

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

crasync/core.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
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+
25+
126
import aiohttp
227
import asyncio
3-
from .models import Profile, Clan, Constants
4-
28+
from .models import Profile, Clan, Constants, ClanInfo
529

630
class Client:
731

@@ -25,7 +49,6 @@ async def get_profile(self, *tags):
2549
raise SyntaxError("Read the docs please")
2650

2751
tags = ','.join(tags)
28-
2952
url = f'{self.BASE}/profile/{tags}'
3053

3154
async with self.session.get(url) as resp:
@@ -49,7 +72,6 @@ async def get_clan(self, *tags):
4972
raise SyntaxError("Read the docs please")
5073

5174
tags = ','.join(tags)
52-
5375
url = f'{self.BASE}/clan/{tags}'
5476

5577
async with self.session.get(url) as resp:
@@ -81,3 +103,17 @@ async def get_constants(self):
81103

82104
return Constants(self, data)
83105

106+
async def get_top_clans(self):
107+
'''Get a list of top clans, info is only brief,
108+
call get_clan() on each of the ClanInfo objects
109+
to get full clan info'''
110+
111+
url = f'{self.BASE}/top/clans'
112+
113+
async with self.session.get(url) as resp:
114+
if resp.status == 200:
115+
data = await resp.json()
116+
else:
117+
raise ConnectionError(f'API not responding: {resp.status}')
118+
119+
return [ClanInfo(self, c) for c in data.get('clans')]

crasync/models.py

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
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+
125
class 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

82103
class 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

102127
class 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

107132
class 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+
152203
class 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

176230
class 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

225282
class 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

Comments
 (0)