|
7 | 7 | standalone script (i.e. 'python -m coderwall').
|
8 | 8 | """
|
9 | 9 |
|
| 10 | +import sys |
10 | 11 | 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 |
12 | 19 |
|
13 | 20 | class CoderWall:
|
14 | 21 |
|
@@ -98,11 +105,11 @@ def get_json_data(username):
|
98 | 105 | api_url = 'http://coderwall.com/' + username + '.json'
|
99 | 106 |
|
100 | 107 | 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: |
103 | 110 | return '' # TODO Better error handling
|
104 | 111 |
|
105 |
| - return response.read() |
| 112 | + return response.read().decode('utf-8') |
106 | 113 |
|
107 | 114 | def parse_json_data(json_data):
|
108 | 115 | """ Parse the given JSON data and return data about the user. """
|
@@ -136,7 +143,7 @@ def parse_badges(raw_badges):
|
136 | 143 | import sys
|
137 | 144 |
|
138 | 145 | if len(sys.argv) < 2:
|
139 |
| - print 'Usage: ' + sys.argv[0] + ' USERNAME...' |
| 146 | + print('Usage: ' + sys.argv[0] + ' USERNAME...') |
140 | 147 | else:
|
141 | 148 | for username in sys.argv[1:]:
|
142 |
| - print CoderWall(username) |
| 149 | + print(CoderWall(username)) |
0 commit comments