This repository has been archived on 2023-04-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
CloudBot/plugins/util/web.py
2012-08-28 16:38:40 +12:00

31 lines
No EOL
926 B
Python
Executable file

""" web.py - handy functions for web services """
import http, urlnorm
from urllib import urlencode
class ShortenError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
# this is not used in any plugins right now
def bitly(url, user, api_key):
""" shortens a URL with the bit.ly API """
url = urlnorm.normalise(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'])
def isgd(url):
""" shortens a URL with the is.gd PAI """
url = urlnorm.normalize(url.encode('utf-8'))
params = urlencode({'format': 'simple', 'url': url})
return http.get("http://is.gd/create.php?%s" % params)