13
13
14
14
# Handle differences in urllib imports
15
15
if sys .version_info [0 ] >= 3 :
16
- import urllib .request as urllib_request , urllib .error as urllib_error
16
+ import urllib .request as urllib_request
17
+ import urllib .error as urllib_error
17
18
else :
18
19
import urllib2 as urllib_request
19
20
urllib_error = urllib_request
20
21
22
+
21
23
class CoderWall (object ):
22
24
23
25
"""
@@ -40,17 +42,17 @@ class CoderWall(object):
40
42
>>> cwc.endorsements
41
43
0
42
44
>>> cwc.badges
43
- [Charity: Fork and commit to someone's open source project in need,
44
- Python: Would you expect anything less? Have at least one original repo
45
- where Python is the dominant language, T-Rex: Have at least one original
45
+ [Charity: Fork and commit to someone's open source project in need,
46
+ Python: Would you expect anything less? Have at least one original repo
47
+ where Python is the dominant language, T-Rex: Have at least one original
46
48
repo where C is the dominant language]
47
49
>>> cwc.badges[0].image_uri
48
50
http://cdn.coderwall.com/assets/badges/charity-bf61e713137d910534ff805f389bcffb.png
49
51
>>> print cwc
50
- Cameron Currie (cwc), Austin, TX, Endorsed 0 times: [Charity: Fork and
51
- commit to someone's open source project in need, Python: Would you expect
52
- anything less? Have at least one original repo where Python is the dominant
53
- language, T-Rex: Have at least one original repo where C is the dominant
52
+ Cameron Currie (cwc), Austin, TX, Endorsed 0 times: [Charity: Fork and
53
+ commit to someone's open source project in need, Python: Would you expect
54
+ anything less? Have at least one original repo where Python is the dominant
55
+ language, T-Rex: Have at least one original repo where C is the dominant
54
56
language]
55
57
"""
56
58
@@ -65,13 +67,15 @@ def __init__(self, username):
65
67
self .endorsements = data [2 ]
66
68
self .badges = parse_badges (data [3 ])
67
69
else :
68
- raise NameError (self .username + ' does not appear to be a CoderWall user' )
70
+ raise NameError (self .username +
71
+ ' does not appear to be a CoderWall user' )
69
72
70
73
def __repr__ (self ):
71
74
return "CoderWall(username=%r)" % (self .username )
72
75
73
- def __str__ (self ):
74
- return self .name + ' (' + self .username + '), ' + self .location + ', Endorsed ' + str (self .endorsements ) + ' times: ' + str (self .badges )
76
+ def __str__ (self ):
77
+ return '%s (%s), %s, Endorsed %s times: %s' % (self .name , self .username , self .location , str (self .endorsements ), str (self .badges ))
78
+
75
79
76
80
class Badge (object ):
77
81
@@ -84,8 +88,8 @@ class Badge(object):
84
88
name
85
89
description
86
90
image_uri
87
- """
88
-
91
+ """
92
+
89
93
def __init__ (self , name , description , image_uri ):
90
94
self .name = name
91
95
self .description = description
@@ -97,49 +101,56 @@ def __repr__(self):
97
101
def __str__ (self ):
98
102
return self .name + ': ' + self .description
99
103
104
+
100
105
def get_json_data (username ):
101
106
"""
102
- Connect to CoderWall and return the raw JSON data for the given
103
- username.
107
+ Connect to CoderWall and return the raw JSON data for the given
108
+ username.
104
109
"""
105
110
106
111
api_url = 'http://coderwall.com/' + username + '.json'
107
112
108
113
try :
109
114
response = urllib_request .urlopen (api_url , None , 5 )
110
115
except urllib_error .URLError :
111
- return '' # TODO Better error handling
116
+ return '' # TODO Better error handling
112
117
113
118
return response .read ().decode ('utf-8' )
114
119
120
+
115
121
def parse_json_data (json_data ):
116
122
""" Parse the given JSON data and return data about the user. """
117
123
118
124
try :
119
125
data = json .loads (json_data )
120
126
except ValueError :
121
- return None # TODO Better error handling
127
+ return None # TODO Better error handling
122
128
123
129
name = data ['name' ]
124
130
location = data ['location' ]
125
- endorsements = data ['endorsements' ]
131
+ endorsements = data ['endorsements' ]
126
132
badges = data ['badges' ]
127
133
128
134
return (name , location , endorsements , badges )
129
135
136
+
130
137
def parse_badges (raw_badges ):
131
138
"""
132
- Parse the given list of dictionaries, interpret each as a
139
+ Parse the given list of dictionaries, interpret each as a
133
140
CoderWall badge, and return a list of Badge objects.
134
141
"""
135
142
136
143
badges = []
137
144
for raw_badge in raw_badges :
138
- badges .append (Badge (raw_badge ['name' ],
139
- raw_badge ['description' ], raw_badge ['badge' ]))
145
+ badges .append (Badge (
146
+ raw_badge ['name' ],
147
+ raw_badge ['description' ],
148
+ raw_badge ['badge' ]
149
+ ))
140
150
141
151
return badges
142
152
153
+
143
154
if __name__ == '__main__' :
144
155
if len (sys .argv ) < 2 :
145
156
print ('Usage: ' + sys .argv [0 ] + ' <username> [username ...]' )
0 commit comments