27 lines
717 B
Python
Executable file
27 lines
717 B
Python
Executable file
""" web.py - handy functions for web services """
|
|
|
|
import http
|
|
from urllib import urlencode
|
|
|
|
|
|
class ShortenError(Exception):
|
|
def __init__(self, value):
|
|
self.value = value
|
|
|
|
def __str__(self):
|
|
return repr(self.value)
|
|
|
|
|
|
def bitly(url, user, api_key):
|
|
""" shortens a URL with the bit.ly API """
|
|
if url[:8] == "https://":
|
|
pass
|
|
elif url[:7] != "http://":
|
|
url = "http://" + url
|
|
params = urlencode({'longUrl': url, 'login': user, 'apiKey': api_key,
|
|
'format': 'json'})
|
|
j = http.get_json("http://api.bit.ly/v3/shorten?%s" % params)
|
|
if j['status_code'] == 200:
|
|
return j['data']['url']
|
|
raise ShortenError('%s' % j['status_txt'])
|