From 2871bd32118cd0461893ff4927278afb33dbd971 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 28 Aug 2012 16:38:40 +1200 Subject: [PATCH] Switched to using is.gd for URL shortening --- plugins/lmgtfy.py | 9 +++------ plugins/shorten.py | 14 +++++--------- plugins/util/web.py | 13 ++++++++++--- plugins/wolframalpha.py | 12 +++++------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/plugins/lmgtfy.py b/plugins/lmgtfy.py index bd90396..0ad561b 100644 --- a/plugins/lmgtfy.py +++ b/plugins/lmgtfy.py @@ -1,5 +1,6 @@ + from util import hook -from util.web import bitly +from util.web import isgd from urllib import quote_plus @@ -7,10 +8,6 @@ from urllib import quote_plus @hook.command def lmgtfy(inp, bot=None): "lmgtfy [phrase] - Posts a google link for the specified phrase" - 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" url = "http://lmgtfy.com/?q=%s" % quote_plus(inp) - return bitly(url, api_user, api_key) \ No newline at end of file + return isgd(url, api_user, api_key) \ No newline at end of file diff --git a/plugins/shorten.py b/plugins/shorten.py index 7d57a0e..93493a9 100755 --- a/plugins/shorten.py +++ b/plugins/shorten.py @@ -2,18 +2,14 @@ from util import hook from urllib2 import HTTPError -from util.web import bitly, ShortenError +from util.web import isgd @hook.command -def shorten(inp, bot=None): - "shorten - 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" +def shorten(inp): + "shorten - Makes an is.gd shortlink to the url provided." try: - return bitly(inp, api_user, api_key) - except (HTTPError, ShortenError): + return isgd(inp) + except (HTTPError): return "Could not shorten '%s'!" % inp diff --git a/plugins/util/web.py b/plugins/util/web.py index f3d9700..7651ac4 100755 --- a/plugins/util/web.py +++ b/plugins/util/web.py @@ -1,6 +1,6 @@ """ web.py - handy functions for web services """ -import http +import http, urlnorm from urllib import urlencode @@ -12,13 +12,20 @@ class ShortenError(Exception): 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 """ - if not "://" in url: - url = "http://" + url + 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) \ No newline at end of file diff --git a/plugins/wolframalpha.py b/plugins/wolframalpha.py index 0f70eae..7d2ceda 100755 --- a/plugins/wolframalpha.py +++ b/plugins/wolframalpha.py @@ -1,10 +1,10 @@ import re -import urllib from util import hook, http +from urllib import quote_plus from urllib2 import HTTPError -from util.web import bitly, ShortenError +from util.web import isgd from util.text import truncate_words @@ -14,8 +14,6 @@ def wolframalpha(inp, bot=None): "wa -- Computes using Wolfram Alpha." api_key = bot.config.get("api_keys", {}).get("wolframalpha", None) - bitly_user = bot.config.get("api_keys", {}).get("bitly_user", None) - bitly_key = bot.config.get("api_keys", {}).get("bitly_api", None) if not api_key: return "error: missing api key" @@ -26,10 +24,10 @@ def wolframalpha(inp, bot=None): # get the URL for a user to view this query in a browser query_url = "http://www.wolframalpha.com/input/?i=" + \ - urllib.quote(inp.encode('utf-8')) + quote_plus(inp.encode('utf-8')) try: - short_url = bitly(query_url, bitly_user, bitly_key) - except (HTTPError, ShortenError): + short_url = isgd(query_url) + except (HTTPError): short_url = query_url pod_texts = []