web.isgd() now throws an error when it cant shorten a URL

This commit is contained in:
Luke Rogers 2012-10-13 12:10:02 +13:00
parent 28345b9c56
commit 72ed3c504f
5 changed files with 36 additions and 15 deletions

View file

@ -11,16 +11,25 @@ yql_env = "http://datatables.org/alltables.env"
YQL = yql.Public()
def query(query, params={}):
""" runs a YQL query and returns the results """
return YQL.execute(query, params, env=yql_env)
class ShortenError(Exception):
def __init__(self, code, text):
self.code = code
self.text = text
def __str__(self):
return self.text
def isgd(url):
""" shortens a URL with the is.gd PAI """
url = urlnorm.normalize(url.encode('utf-8'), assume_scheme='http')
params = urllib.urlencode({'format': 'simple', 'url': url})
return http.get("http://is.gd/create.php?%s" % params)
params = urllib.urlencode({'format': 'json', 'url': url})
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):
@ -28,3 +37,8 @@ def haste(text):
page = http.get(paste_url + "/documents", post_data=text)
data = json.loads(page)
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)