Tweaked plugins/shorten.py to return correct errors
This commit is contained in:
parent
f7b0d6f388
commit
98ff0ab723
|
@ -8,6 +8,12 @@ try:
|
||||||
|
|
||||||
except ImportError, e:
|
except ImportError, e:
|
||||||
raise Exception('Required module missing: %s' % e.args[0])
|
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):
|
def tiny(url, user, apikey):
|
||||||
try:
|
try:
|
||||||
|
@ -15,15 +21,13 @@ def tiny(url, user, apikey):
|
||||||
j = http.get_json("http://api.bit.ly/v3/shorten?%s" % params)
|
j = http.get_json("http://api.bit.ly/v3/shorten?%s" % params)
|
||||||
if j['status_code'] == 200:
|
if j['status_code'] == 200:
|
||||||
return j['data']['url']
|
return j['data']['url']
|
||||||
raise Exception('%s'%j['status_txt'])
|
raise ShortenError('%s'%j['status_txt'])
|
||||||
except HTTPError, e:
|
except (HTTPError, ShortenError):
|
||||||
return "Invalid URL!"
|
return "Could not shorten %s!" % url
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def shorten(inp, bot = None):
|
def shorten(inp, bot = None):
|
||||||
".shorten <url> - Makes an j.mp/bit.ly shortlink to the url provided"
|
".shorten <url> - Makes an j.mp/bit.ly shortlink to the url provided"
|
||||||
api_user = bot.config.get("api_keys", {}).get("bitly_user", None)
|
user = bot.config['api_keys']['bitly_user']
|
||||||
api_key = bot.config.get("api_keys", {}).get("bitly_api", None)
|
api = bot.config['api_keys']['bitly_api']
|
||||||
if api_key is None:
|
return tiny(inp, user, api)
|
||||||
return "error: no api key set"
|
|
||||||
return tiny(inp, api_user, api_key)
|
|
||||||
|
|
Reference in a new issue