Update plugins/shorten.py
This commit is contained in:
parent
dcdc3341e3
commit
d5c81f6079
1 changed files with 17 additions and 22 deletions
|
@ -2,14 +2,9 @@
|
||||||
# <lukeroge@gmail.com> <https://github.com/lukeroge/CloudBot/>
|
# <lukeroge@gmail.com> <https://github.com/lukeroge/CloudBot/>
|
||||||
|
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
from re import match
|
||||||
try:
|
from urllib2 import urlopen, Request, HTTPError
|
||||||
from re import match
|
from urllib import urlencode
|
||||||
from urllib2 import urlopen, Request, HTTPError
|
|
||||||
from urllib import urlencode
|
|
||||||
|
|
||||||
except ImportError, e:
|
|
||||||
raise Exception('Required module missing: %s' % e.args[0])
|
|
||||||
|
|
||||||
class ShortenError(Exception):
|
class ShortenError(Exception):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
|
@ -18,20 +13,20 @@ class ShortenError(Exception):
|
||||||
return repr(self.value)
|
return repr(self.value)
|
||||||
|
|
||||||
def bitly(url, user, apikey):
|
def bitly(url, user, apikey):
|
||||||
try:
|
try:
|
||||||
params = urlencode({'longUrl': url, 'login': user, 'apiKey': apikey, 'format': 'json'})
|
params = urlencode({'longUrl': url, 'login': user, 'apiKey': apikey, 'format': 'json'})
|
||||||
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 ShortenError('%s'%j['status_txt'])
|
raise ShortenError('%s'%j['status_txt'])
|
||||||
except (HTTPError, ShortenError):
|
except (HTTPError, ShortenError):
|
||||||
return "Could not shorten %s!" % 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)
|
api_user = bot.config.get("api_keys", {}).get("bitly_user", None)
|
||||||
api_key = bot.config.get("api_keys", {}).get("bitly_api", None)
|
api_key = bot.config.get("api_keys", {}).get("bitly_api", None)
|
||||||
if api_key is None:
|
if api_key is None:
|
||||||
return "error: no api key set"
|
return "error: no api key set"
|
||||||
return bitly(inp, api_user, api_key)
|
return bitly(inp, api_user, api_key)
|
Reference in a new issue