Merge pull request #22 from dngfx/master

Replace bit.ly with is.gd
This commit is contained in:
jesopo 2018-09-09 21:59:30 +01:00 committed by GitHub
commit b902b2fb0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,7 @@
#--require-config bitly-api-key
import re import re
import Utils import Utils
URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten" ISGD_API_URL = "https://is.gd/create.php"
REGEX_URL = re.compile("https?://", re.I) REGEX_URL = re.compile("https?://", re.I)
class Module(object): class Module(object):
@ -12,22 +10,24 @@ class Module(object):
self.events = events self.events = events
events.on("get.shortlink").hook(self.shortlink) events.on("get.shortlink").hook(self.shortlink)
events.on("received.command.shorten").hook(self.shorten, min_args=1, events.on("received.command.shorten").hook(self.shorten, min_args=1,
help="Shorten a URL.", usage="<url>") help="Shorten a URL using the is.gd service.", usage="<url>")
def shortlink(self, event): def shortlink(self, event):
url = event["url"] url = event["url"]
if not re.match(REGEX_URL, url): if not re.match(REGEX_URL, url):
url = "http://%s" % url url = "http://%s" % url
data = Utils.get_url(URL_BITLYSHORTEN, get_params={ data = Utils.get_url(ISGD_API_URL, get_params={
"access_token": self.bot.config["bitly-api-key"], "format": "json",
"longUrl": url}, json=True) "url": url
if data and data["data"]: }, json=True)
return data["data"]["url"]
if data and data["shorturl"]:
return data["shorturl"]
def shorten(self, event): def shorten(self, event):
link = self.events.on("get.shortlink").call_for_result( link = self.events.on("get.shortlink").call_for_result(
url=event["args"]) url=event["args"])
if link: if link:
event["stdout"].write("Short URL: %s" % link) event["stdout"].write("Shortened URL: %s" % link)
else: else:
event["stderr"].write("Unable to shorten that URL.") event["stderr"].write("Unable to shorten that URL.")