Suggested changes to support the history=1 parameter

9 views
Skip to first unread message

Nathan Wallace

unread,
Nov 10, 2009, 12:18:40 AM11/10/09
to Python Bitly
The bit.ly API allows for history=1 to be passed so that links are
added to the users bit.ly history. Unfortunately this is not supported
by python-bitly at this stage. The patches to two methods below will
enable a parameters option to be passed to shorten() supporting extra
fields like history.

Couldn't work out how else to best submit this change. Thanks for a
great tool!

Nathan

------------------

def shorten(self,longURL,params={}):
"""
Takes either:
A long URL string and returns shortened URL string
Or a list of long URL strings and returnes a list of
shortened URL strings.
"""
if not isinstance(longURL, list):
longURL = [longURL]

for index,url in enumerate(longURL):
if not url.startswith("http"):
longURL[index] = "http://" + url

request = self._getURL("shorten",longURL,params)
logging.error(request)
result = self._fetchUrl(request)
json = simplejson.loads(result)
self._CheckForError(json)

res = []
for item in json['results'].values():
if item['shortKeywordUrl'] == "":
res.append(item['shortUrl'])
else:
res.append(item['shortKeywordUrl'])

if len(res) == 1:
return res[0]
else:
return res

-------------------

def _getURL(self,verb,paramVal,_params={}):
if not isinstance(paramVal, list):
paramVal = [paramVal]

params = {
'version': BITLY_API_VERSION,
'format': 'json',
'login': self.login,
'apiKey': self.apikey,
}
params.update(_params)
params = params.items()

verbParam = VERBS_PARAM[verb]
if verbParam:
for val in paramVal:
params.append(( verbParam,val ))

encoded_params = urllib.urlencode(params)
return "%s%s?%s" % (BITLY_BASE_URL,verb,encoded_params)
Reply all
Reply to author
Forward
0 new messages