Skip to content

Commit ce0a33b

Browse files
committed
Added profile searching
1 parent 49768ab commit ce0a33b

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pyc

__init__.py

Whitespace-only changes.

sc2ranks.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, app_key):
99

1010
def api_fetch(self, path, params=''):
1111
"""
12-
Fetch some json from the API
12+
Fetch some JSON from the API
1313
1414
>>> client = Sc2Ranks('github.com/phleet/sc2ranks')
1515
>>> client.api_fetch('search/exact/us/phleet')
@@ -19,10 +19,13 @@ def api_fetch(self, path, params=''):
1919
return fetch_json(url,params)
2020

2121
def validate(self, data, exception):
22-
if data.has_key('error'):
22+
if type(data).__name__ == 'dict' and data.has_key('error'):
2323
raise exception
2424
else:
25-
return Sc2RanksResponse(data)
25+
if type(data).__name__ == 'dict':
26+
return Sc2RanksResponse(data)
27+
elif type(data).__name__ == 'list':
28+
return [Sc2RanksResponse(datum) for datum in data]
2629

2730
def search_for_character(self, region, name, search_type='exact'):
2831
"""
@@ -41,6 +44,37 @@ def search_for_character(self, region, name, search_type='exact'):
4144
data = self.api_fetch('search/%s/%s/%s' % (search_type,region,name)),
4245
exception = NoSuchCharacterException("Name: %s, region: %s" % (name,region))
4346
)
47+
48+
def search_for_profile(self, region, name, search_type='1t', search_subtype='division', value='Division'):
49+
"""
50+
Search for a character profile
51+
52+
>>> client = Sc2Ranks('github.com/phleet/sc2ranks')
53+
>>> client.search_for_profile(name='phleet', region='us')
54+
... #doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
55+
[<Sc2RanksResponse(bnet_id=299464,
56+
name=phleet,
57+
achievement_points=...,
58+
character_code=...,
59+
region=us,
60+
team={u'division_id': ...,
61+
u'division_name': ...,
62+
u'wins': ...,
63+
u'losses': ...,
64+
u'points': ...,
65+
u'id': ...},
66+
id=...)>]
67+
>>> client.search_for_profile(name='PleaseNobodyTakeThisUsername', region='us')
68+
Traceback (most recent call last):
69+
...
70+
NoSuchCharacterException: Name: PleaseNobodyTakeThisUsername, region: us
71+
"""
72+
73+
return self.validate(
74+
data = self.api_fetch('psearch/%s/%s/%s/%s/%s' % (region,name,search_type,search_subtype,value)),
75+
exception = NoSuchCharacterException("Name: %s, region: %s" % (name,region))
76+
)
77+
4478

4579
def fetch_base_character(self, region, name, code):
4680
"""

0 commit comments

Comments
 (0)