@@ -45,7 +45,7 @@ def __init__(self, login, apikey):
45
45
self .apikey = apikey
46
46
self ._urllib = urllib2
47
47
48
- def shorten (self ,longURLs ,params = {}):
48
+ def shorten (self , longURLs , params = {}):
49
49
"""
50
50
Takes either:
51
51
A long URL string and returns shortened URL string
@@ -55,39 +55,31 @@ def shorten(self,longURLs,params={}):
55
55
if not isinstance (longURLs , list ):
56
56
longURLs = [longURLs ]
57
57
want_result_list = False
58
-
59
- for index ,url in enumerate (longURLs ):
60
- if not '://' in url :
61
- longURLs [index ] = "http://" + url
62
58
63
- request = self ._getURL ("shorten" ,longURLs ,params )
59
+ request = self ._getURL ("shorten" , longURLs , params )
64
60
result = self ._fetchUrl (request )
65
61
json = simplejson .loads (result )
66
- self ._CheckForError (json )
62
+ Api ._CheckForError (json )
67
63
68
64
results = json ['results' ]
69
- res = [self ._extract_short_url (results [url ]) for url in longURLs ]
70
65
66
+ res = [results [url ].get ('shortUrl' , None ) for url in longURLs ]
67
+
71
68
if want_result_list :
72
69
return res
73
70
else :
74
71
return res [0 ]
75
72
76
- def _extract_short_url (self ,item ):
77
- if item ['shortKeywordUrl' ] == "" :
78
- return item ['shortUrl' ]
79
- else :
80
- return item ['shortKeywordUrl' ]
81
73
82
- def expand (self ,shortURL ,params = {}):
74
+ def expand (self , shortURL , params = {}):
83
75
""" Given a bit.ly url or hash, return long source url """
84
76
request = self ._getURL ("expand" ,shortURL ,params )
85
77
result = self ._fetchUrl (request )
86
78
json = simplejson .loads (result )
87
- self ._CheckForError (json )
79
+ Api ._CheckForError (json )
88
80
return json ['results' ][string .split (shortURL , '/' )[- 1 ]]['longUrl' ]
89
81
90
- def info (self ,shortURL ,params = {}):
82
+ def info (self , shortURL , params = {}):
91
83
"""
92
84
Given a bit.ly url or hash,
93
85
return information about that page,
@@ -96,23 +88,23 @@ def info(self,shortURL,params={}):
96
88
request = self ._getURL ("info" ,shortURL ,params )
97
89
result = self ._fetchUrl (request )
98
90
json = simplejson .loads (result )
99
- self ._CheckForError (json )
91
+ Api ._CheckForError (json )
100
92
return json ['results' ][string .split (shortURL , '/' )[- 1 ]]
101
93
102
- def stats (self ,shortURL ,params = {}):
94
+ def stats (self , shortURL , params = {}):
103
95
""" Given a bit.ly url or hash, return traffic and referrer data. """
104
96
request = self ._getURL ("stats" ,shortURL ,params )
105
97
result = self ._fetchUrl (request )
106
98
json = simplejson .loads (result )
107
- self ._CheckForError (json )
99
+ Api ._CheckForError (json )
108
100
return Stats .NewFromJsonDict (json ['results' ])
109
101
110
- def errors (self ,params = {}):
102
+ def errors (self , params = {}):
111
103
""" Get a list of bit.ly API error codes. """
112
104
request = self ._getURL ("errors" ,"" ,params )
113
105
result = self ._fetchUrl (request )
114
106
json = simplejson .loads (result )
115
- self ._CheckForError (json )
107
+ Api ._CheckForError (json )
116
108
return json ['results' ]
117
109
118
110
def setUrllib (self , urllib ):
@@ -123,7 +115,7 @@ def setUrllib(self, urllib):
123
115
'''
124
116
self ._urllib = urllib
125
117
126
- def _getURL (self ,verb ,paramVal ,more_params = {}):
118
+ def _getURL (self , verb , paramVal , more_params = {}):
127
119
if not isinstance (paramVal , list ):
128
120
paramVal = [paramVal ]
129
121
@@ -145,7 +137,7 @@ def _getURL(self,verb,paramVal,more_params={}):
145
137
encoded_params = urllib .urlencode (params )
146
138
return "%s%s?%s" % (BITLY_BASE_URL ,verb ,encoded_params )
147
139
148
- def _fetchUrl (self ,url ):
140
+ def _fetchUrl (self , url ):
149
141
'''Fetch a URL
150
142
151
143
Args:
@@ -159,7 +151,8 @@ def _fetchUrl(self,url):
159
151
url_data = self ._urllib .urlopen (url ).read ()
160
152
return url_data
161
153
162
- def _CheckForError (self , data ):
154
+ @classmethod
155
+ def _CheckForError (cls , data ):
163
156
"""Raises a BitlyError if bitly returns an error message.
164
157
165
158
Args:
@@ -171,7 +164,7 @@ def _CheckForError(self, data):
171
164
# to check first, rather than try and catch the exception
172
165
if 'ERROR' in data or data ['statusCode' ] == 'ERROR' :
173
166
raise BitlyError , data ['errorMessage' ]
174
- for key in data ['results' ]:
167
+ for key in data ['results' ]:
175
168
if type (data ['results' ]) is dict and type (data ['results' ][key ]) is dict :
176
169
if 'statusCode' in data ['results' ][key ] and data ['results' ][key ]['statusCode' ] == 'ERROR' :
177
170
raise BitlyError , data ['results' ][key ]['errorMessage' ]
@@ -202,8 +195,8 @@ def NewFromJsonDict(data):
202
195
203
196
204
197
if __name__ == '__main__' :
205
- testURL1 = "www.yahoo.com"
206
- testURL2 = "www.cnn.com"
198
+ testURL1 = "http:// www.yahoo.com"
199
+ testURL2 = "http:// www.cnn.com"
207
200
a = Api (login = "pythonbitly" ,apikey = "R_06871db6b7fd31a4242709acaf1b6648" )
208
201
short = a .shorten (testURL1 )
209
202
print "Short URL = %s" % short
@@ -220,6 +213,6 @@ def NewFromJsonDict(data):
220
213
print "User clicks %s, total clicks: %s" % (stats .user_clicks ,stats .total_clicks )
221
214
errors = a .errors ()
222
215
print "Errors: %s" % errors
223
- testURL3 = ["www.google.com" ]
216
+ testURL3 = ["http:// www.google.com" ]
224
217
short = a .shorten (testURL3 )
225
218
print "Short url in list = %s" % short
0 commit comments