🌐 AI搜索 & 代理 主页
Skip to content

Commit b8acc57

Browse files
committed
Initial commit
0 parents  commit b8acc57

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

coderwall.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""
2+
This module provides a simple interface to the API at http://coderwall.com.
3+
4+
Uses the json module introduced in Python 2.6.
5+
6+
See the CoderWall class for a usage example. The module can also be used as a
7+
standalone script (i.e. 'python -m coderwall').
8+
"""
9+
10+
import json
11+
import urllib2
12+
13+
class CoderWall:
14+
15+
"""
16+
Represents a CoderWall user and provides access to all of the user's data.
17+
18+
Fields:
19+
20+
name
21+
location
22+
endorsements
23+
badges
24+
25+
Usage:
26+
27+
>>> cwc = CoderWall('cwc')
28+
>>> cwc.name
29+
u'Cameron Currie'
30+
>>> cwc.location
31+
u'Austin, TX'
32+
>>> cwc.endorsements
33+
0
34+
>>> cwc.badges
35+
[Charity: Fork and commit to someone's open source project in need,
36+
Python: Would you expect anything less? Have at least one original repo
37+
where Python is the dominant language, T-Rex: Have at least one original
38+
repo where C is the dominant language]
39+
>>> cwc.badges[0].image_uri
40+
http://cdn.coderwall.com/assets/badges/charity-bf61e713137d910534ff805f389bcffb.png
41+
>>> print cwc
42+
Cameron Currie (cwc), Austin, TX, Endorsed 0 times: [Charity: Fork and
43+
commit to someone's open source project in need, Python: Would you expect
44+
anything less? Have at least one original repo where Python is the dominant
45+
language, T-Rex: Have at least one original repo where C is the dominant
46+
language]
47+
"""
48+
49+
def __init__(self, username):
50+
self.username = username
51+
52+
data = parse_json_data(get_json_data(self.username))
53+
54+
if data is not None:
55+
self.name = data[0]
56+
self.location = data[1]
57+
self.endorsements = data[2]
58+
self.badges = parse_badges(data[3])
59+
else:
60+
raise NameError(self.username + ' does not appear to be a CoderWall user')
61+
62+
def __repr__(self):
63+
return self.__str__() # NOTE Does not conform to standards
64+
65+
def __str__(self):
66+
return self.name + ' (' + self.username + '), ' + self.location + ', Endorsed ' + str(self.endorsements) + ' times: ' + str(self.badges)
67+
68+
class Badge:
69+
70+
"""
71+
Represents a CoderWall badge and provides access to its attributes, such as
72+
the image URL.
73+
74+
Fields:
75+
76+
name
77+
description
78+
image_uri
79+
"""
80+
81+
def __init__(self, name, description, image_uri):
82+
self.name = name
83+
self.description = description
84+
self.image_uri = image_uri
85+
86+
def __repr__(self):
87+
return self.__str__() # NOTE Does not conform to standards
88+
89+
def __str__(self):
90+
return self.name + ': ' + self.description
91+
92+
def get_json_data(username):
93+
"""
94+
Connect to CoderWall and return the raw JSON data for the given
95+
username.
96+
"""
97+
98+
api_url = 'http://coderwall.com/' + username + '.json'
99+
100+
try:
101+
response = urllib2.urlopen(api_url, None, 5)
102+
except urllib2.URLError:
103+
return '' # TODO Better error handling
104+
105+
return response.read()
106+
107+
def parse_json_data(json_data):
108+
""" Parse the given JSON data and return data about the user. """
109+
110+
try:
111+
data = json.loads(json_data)
112+
except ValueError:
113+
return None # TODO Better error handling
114+
115+
name = data['name']
116+
location = data['location']
117+
endorsements = data['endorsements']
118+
badges = data['badges']
119+
120+
return (name, location, endorsements, badges)
121+
122+
def parse_badges(raw_badges):
123+
"""
124+
Parse the given list of dictionaries, interpret each as a
125+
CoderWall badge, and return a list of Badge objects.
126+
"""
127+
128+
badges = []
129+
for raw_badge in raw_badges:
130+
badges.append(Badge(raw_badge['name'],
131+
raw_badge['description'], raw_badge['badge']))
132+
133+
return badges
134+
135+
if __name__ == '__main__':
136+
import sys
137+
138+
if len(sys.argv) < 2:
139+
print 'Usage: ' + sys.argv[0] + ' USERNAME...'
140+
else:
141+
for username in sys.argv[1:]:
142+
print CoderWall(username)

0 commit comments

Comments
 (0)