Skip to content

Commit daf8669

Browse files
committed
Fix Python 3 compatibility
1 parent acc6794 commit daf8669

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

coderwall.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
standalone script (i.e. 'python -m coderwall').
88
"""
99

10+
import sys
1011
import json
11-
import urllib2
12+
13+
# Handle differences in urllib imports
14+
if sys.version_info[0] >= 3:
15+
import urllib.request as urllib_request, urllib.error as urllib_error
16+
else:
17+
import urllib2 as urllib_request
18+
urllib_error = urllib_request
1219

1320
class CoderWall:
1421

@@ -98,11 +105,11 @@ def get_json_data(username):
98105
api_url = 'http://coderwall.com/' + username + '.json'
99106

100107
try:
101-
response = urllib2.urlopen(api_url, None, 5)
102-
except urllib2.URLError:
108+
response = urllib_request.urlopen(api_url, None, 5)
109+
except urllib_error.URLError:
103110
return '' # TODO Better error handling
104111

105-
return response.read()
112+
return response.read().decode('utf-8')
106113

107114
def parse_json_data(json_data):
108115
""" Parse the given JSON data and return data about the user. """
@@ -136,7 +143,7 @@ def parse_badges(raw_badges):
136143
import sys
137144

138145
if len(sys.argv) < 2:
139-
print 'Usage: ' + sys.argv[0] + ' USERNAME...'
146+
print('Usage: ' + sys.argv[0] + ' USERNAME...')
140147
else:
141148
for username in sys.argv[1:]:
142-
print CoderWall(username)
149+
print(CoderWall(username))

0 commit comments

Comments
 (0)