This commit is contained in:
Luke Rogers 2012-03-23 13:32:48 +13:00
parent e7d69e63c0
commit 549bb70690
5 changed files with 21 additions and 27 deletions

View file

@ -1,4 +1,4 @@
# Plugin by Lukeroge
# Plugin by Lukeroge
# <lukeroge@gmail.com> <https://github.com/lukeroge/CloudBot/>
from util import hook, http
@ -6,6 +6,7 @@ from re import match
from urllib2 import urlopen, Request, HTTPError
from urllib import urlencode
class ShortenError(Exception):
def __init__(self, value):
self.value = value
@ -13,13 +14,15 @@ class ShortenError(Exception):
def __str__(self):
return repr(self.value)
def bitly(url, user, apikey):
try:
if url[:8] == "https://":
pass
elif url[:7] != "http://":
url = "http://" + url
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)
if j['status_code'] == 200:
return j['data']['url']
@ -27,20 +30,12 @@ def bitly(url, user, apikey):
except (HTTPError, ShortenError):
return "Could not shorten %s!" % url
@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."
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 bitly(inp, api_user, api_key)
@hook.command
def expand(inp, bot = None):
".expand <url> - Gets the original URL from a shortened link."
try:
url = http.get_url(inp)
except HTTPError, e:
return "Failed to expand URL."
return url