Switched to using is.gd for URL shortening
This commit is contained in:
parent
3fa8d5d312
commit
2871bd3211
|
@ -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)
|
||||
return isgd(url, api_user, api_key)
|
|
@ -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 <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"
|
||||
def shorten(inp):
|
||||
"shorten <url> - 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
|
||||
|
|
|
@ -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)
|
|
@ -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 <query> -- Computes <query> 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 = []
|
||||
|
|
Reference in a new issue