web.isgd() now throws an error when it cant shorten a URL
This commit is contained in:
parent
28345b9c56
commit
72ed3c504f
5 changed files with 36 additions and 15 deletions
|
@ -32,4 +32,9 @@ def fact(inp, say=False, nick=False):
|
||||||
attempts += 1
|
attempts += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return "%s - %s" % (fact, web.isgd(link))
|
try:
|
||||||
|
url = web.isgd(link)
|
||||||
|
except (web.ShortenError, http.HTTPError):
|
||||||
|
url = link
|
||||||
|
|
||||||
|
return "%s - %s" % (fact, url)
|
||||||
|
|
|
@ -6,5 +6,9 @@ from util import hook, web, http
|
||||||
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"
|
||||||
|
|
||||||
url = "http://lmgtfy.com/?q=%s" % http.quote_plus(inp)
|
link = "http://lmgtfy.com/?q=%s" % http.quote_plus(inp)
|
||||||
return web.isgd(url)
|
|
||||||
|
try:
|
||||||
|
return web.isgd(link)
|
||||||
|
except (web.ShortenError, http.HTTPError):
|
||||||
|
return link
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
# Plugin by Lukeroge
|
# Plugin by Lukeroge
|
||||||
|
|
||||||
from util import hook
|
from util import hook, http, web
|
||||||
from urllib2 import HTTPError
|
|
||||||
from util.web import isgd
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
|
@ -10,6 +8,6 @@ def shorten(inp):
|
||||||
"shorten <url> - Makes an is.gd shortlink to the url provided."
|
"shorten <url> - Makes an is.gd shortlink to the url provided."
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return isgd(inp)
|
return web.isgd(inp)
|
||||||
except (HTTPError):
|
except (web.ShortenError, http.HTTPError) as error:
|
||||||
return "Could not shorten '%s'!" % inp
|
return error
|
||||||
|
|
|
@ -11,16 +11,25 @@ yql_env = "http://datatables.org/alltables.env"
|
||||||
YQL = yql.Public()
|
YQL = yql.Public()
|
||||||
|
|
||||||
|
|
||||||
def query(query, params={}):
|
class ShortenError(Exception):
|
||||||
""" runs a YQL query and returns the results """
|
def __init__(self, code, text):
|
||||||
return YQL.execute(query, params, env=yql_env)
|
self.code = code
|
||||||
|
self.text = text
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.text
|
||||||
|
|
||||||
|
|
||||||
def isgd(url):
|
def isgd(url):
|
||||||
""" shortens a URL with the is.gd PAI """
|
""" shortens a URL with the is.gd PAI """
|
||||||
url = urlnorm.normalize(url.encode('utf-8'), assume_scheme='http')
|
url = urlnorm.normalize(url.encode('utf-8'), assume_scheme='http')
|
||||||
params = urllib.urlencode({'format': 'simple', 'url': url})
|
params = urllib.urlencode({'format': 'json', 'url': url})
|
||||||
return http.get("http://is.gd/create.php?%s" % params)
|
request = http.get_json("http://is.gd/create.php?%s" % params)
|
||||||
|
|
||||||
|
if "errorcode" in request:
|
||||||
|
raise ShortenError(request["errorcode"], request["errormessage"])
|
||||||
|
else:
|
||||||
|
return request["shorturl"]
|
||||||
|
|
||||||
|
|
||||||
def haste(text):
|
def haste(text):
|
||||||
|
@ -28,3 +37,8 @@ def haste(text):
|
||||||
page = http.get(paste_url + "/documents", post_data=text)
|
page = http.get(paste_url + "/documents", post_data=text)
|
||||||
data = json.loads(page)
|
data = json.loads(page)
|
||||||
return("%s/%s.txt" % (paste_url, data['key']))
|
return("%s/%s.txt" % (paste_url, data['key']))
|
||||||
|
|
||||||
|
|
||||||
|
def query(query, params={}):
|
||||||
|
""" runs a YQL query and returns the results """
|
||||||
|
return YQL.execute(query, params, env=yql_env)
|
||||||
|
|
|
@ -22,7 +22,7 @@ def wolframalpha(inp, bot=None):
|
||||||
http.quote_plus(inp.encode('utf-8'))
|
http.quote_plus(inp.encode('utf-8'))
|
||||||
try:
|
try:
|
||||||
short_url = web.isgd(query_url)
|
short_url = web.isgd(query_url)
|
||||||
except (http.HTTPError):
|
except (web.ShortenError, http.HTTPError):
|
||||||
short_url = query_url
|
short_url = query_url
|
||||||
|
|
||||||
pod_texts = []
|
pod_texts = []
|
||||||
|
|
Reference in a new issue