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

Commit 1361271

Browse files
committed
add support for multiple profiles
1 parent a3bf8f3 commit 1361271

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

crasync/core.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import aiohttp
22
import asyncio
3-
from .models import Profile, Clan, Clans, Constants
3+
from .models import Profile, Profiles, Clan, Clans, Constants
44

55

66
class Client:
@@ -19,19 +19,38 @@ def __init__(self, session=None):
1919
self.session = session or aiohttp.ClientSession()
2020

2121

22-
async def get_profile(self, tag):
23-
'''Get a profile object using a tag.'''
22+
async def get_profile(self, *, tags):
23+
'''Get a profile object using tag(s).'''
24+
tags = tags.split(',')
25+
26+
if len(tags) == 1:
27+
url = f'{self.BASE}/profile/{tags[0]}'
28+
29+
async with self.session.get(url) as resp:
30+
if resp.status == 200:
31+
data = await resp.json()
32+
else:
33+
print('API is down. Please be patient.')
34+
return None
35+
36+
return Profile(self, data)
2437

25-
url = f'{self.BASE}/profile/{tag}'
38+
taglist = ''
39+
i = 0
40+
for tag in tags:
41+
i += 1
42+
taglist += tag[1:]
43+
if i != len(tags):
44+
taglist += ','
2645

46+
url = f'{self.BASE}/profile/{taglist}'
2747
async with self.session.get(url) as resp:
2848
if resp.status == 200:
2949
data = await resp.json()
3050
else:
3151
print('API is down. Please be patient.')
3252
return None
33-
34-
return Profile(self, data)
53+
return Profiles(self, data)
3554

3655
async def get_clans(self, *, tags):
3756
'''Get a clan object using tag(s)'''

crasync/models.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@ def badge_url(self):
172172

173173
def __repr__(self):
174174
return f'<Clan tag={self.tag}>'
175-
176-
class Clans(Base):
177-
def from_data(self, data):
178-
self.clans = [Clan(self.client, c) for c in data]
179175

180176
class Profile(Base):
181177
'''Represents a player profile.
@@ -226,6 +222,14 @@ def __repr__(self):
226222
def get_clan(self):
227223
return self.client.get_clan(self.clan_tag)
228224

225+
class Clans(Base):
226+
def from_data(self, data):
227+
self.clans = [Clan(self.client, c) for c in data]
228+
229+
class Profiles(Base):
230+
def from_data(self, data):
231+
self.clans = [Profile(self.client, c) for c in data]
232+
229233
class Constants(Base):
230234
'''Represents the constants from cr-api'''
231235
def from_data(self, data):

0 commit comments

Comments
 (0)