Tweaked plugins/shorten.py to return correct errors

This commit is contained in:
Luke Rogers 2012-02-26 00:37:16 +13:00
parent f7b0d6f388
commit 98ff0ab723

View file

@ -8,6 +8,12 @@ try:
except ImportError, e:
raise Exception('Required module missing: %s' % e.args[0])
class ShortenError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def tiny(url, user, apikey):
try:
@ -15,15 +21,13 @@ def tiny(url, user, apikey):
j = http.get_json("http://api.bit.ly/v3/shorten?%s" % params)
if j['status_code'] == 200:
return j['data']['url']
raise Exception('%s'%j['status_txt'])
except HTTPError, e:
return "Invalid URL!"
raise ShortenError('%s'%j['status_txt'])
except (HTTPError, ShortenError):
return "Could not shorten %s!" % url
@hook.command
def shorten(inp, bot = None):
".shorten <url> - Makes an j.mp/bit.ly shortlink to the url provided"
api_user = bot.config.get("api_keys", {}).get("bitly_user", None)
api_key = bot.config.get("api_keys", {}).get("bitly_api", None)
if api_key is None:
return "error: no api key set"
return tiny(inp, api_user, api_key)
user = bot.config['api_keys']['bitly_user']
api = bot.config['api_keys']['bitly_api']
return tiny(inp, user, api)