Switched to using is.gd for URL shortening
This commit is contained in:
parent
3fa8d5d312
commit
2871bd3211
4 changed files with 23 additions and 25 deletions
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
from util import hook
|
from util import hook
|
||||||
from util.web import bitly
|
from util.web import isgd
|
||||||
from urllib import quote_plus
|
from urllib import quote_plus
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,10 +8,6 @@ from urllib import quote_plus
|
||||||
@hook.command
|
@hook.command
|
||||||
def lmgtfy(inp, bot=None):
|
def lmgtfy(inp, bot=None):
|
||||||
"lmgtfy [phrase] - Posts a google link for the specified phrase"
|
"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)
|
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 util import hook
|
||||||
from urllib2 import HTTPError
|
from urllib2 import HTTPError
|
||||||
from util.web import bitly, ShortenError
|
from util.web import isgd
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def shorten(inp, bot=None):
|
def shorten(inp):
|
||||||
"shorten <url> - Makes an j.mp/bit.ly shortlink to the url provided."
|
"shorten <url> - Makes an is.gd 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"
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return bitly(inp, api_user, api_key)
|
return isgd(inp)
|
||||||
except (HTTPError, ShortenError):
|
except (HTTPError):
|
||||||
return "Could not shorten '%s'!" % inp
|
return "Could not shorten '%s'!" % inp
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
""" web.py - handy functions for web services """
|
""" web.py - handy functions for web services """
|
||||||
|
|
||||||
import http
|
import http, urlnorm
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,13 +12,20 @@ class ShortenError(Exception):
|
||||||
return repr(self.value)
|
return repr(self.value)
|
||||||
|
|
||||||
|
|
||||||
|
# this is not used in any plugins right now
|
||||||
def bitly(url, user, api_key):
|
def bitly(url, user, api_key):
|
||||||
""" shortens a URL with the bit.ly API """
|
""" shortens a URL with the bit.ly API """
|
||||||
if not "://" in url:
|
url = urlnorm.normalise(url)
|
||||||
url = "http://" + url
|
|
||||||
params = urlencode({'longUrl': url, 'login': user, 'apiKey': api_key,
|
params = urlencode({'longUrl': url, 'login': user, 'apiKey': api_key,
|
||||||
'format': 'json'})
|
'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'])
|
||||||
|
|
||||||
|
|
||||||
|
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 re
|
||||||
import urllib
|
|
||||||
|
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
from urllib import quote_plus
|
||||||
from urllib2 import HTTPError
|
from urllib2 import HTTPError
|
||||||
from util.web import bitly, ShortenError
|
from util.web import isgd
|
||||||
from util.text import truncate_words
|
from util.text import truncate_words
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,8 +14,6 @@ def wolframalpha(inp, bot=None):
|
||||||
"wa <query> -- Computes <query> using Wolfram Alpha."
|
"wa <query> -- Computes <query> using Wolfram Alpha."
|
||||||
|
|
||||||
api_key = bot.config.get("api_keys", {}).get("wolframalpha", None)
|
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:
|
if not api_key:
|
||||||
return "error: missing 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
|
# get the URL for a user to view this query in a browser
|
||||||
query_url = "http://www.wolframalpha.com/input/?i=" + \
|
query_url = "http://www.wolframalpha.com/input/?i=" + \
|
||||||
urllib.quote(inp.encode('utf-8'))
|
quote_plus(inp.encode('utf-8'))
|
||||||
try:
|
try:
|
||||||
short_url = bitly(query_url, bitly_user, bitly_key)
|
short_url = isgd(query_url)
|
||||||
except (HTTPError, ShortenError):
|
except (HTTPError):
|
||||||
short_url = query_url
|
short_url = query_url
|
||||||
|
|
||||||
pod_texts = []
|
pod_texts = []
|
||||||
|
|
Reference in a new issue