aboutsummaryrefslogtreecommitdiff
path: root/modules/isgd.py
diff options
context:
space:
mode:
authorGravatar jesopo2018-09-09 21:59:30 +0100
committerGravatar GitHub2018-09-09 21:59:30 +0100
commitb902b2fb0c9d14b5cf358a752e9687cc6afbe8c9 (patch)
treedc04f0d4882ea55c67dc69a216335fcece6f4ebc /modules/isgd.py
parentMerge pull request #21 from dngfx/master (diff)
parentReplace bit.ly with is.gd. No API key, no beeping Bit. It's a win win! (diff)
Merge pull request #22 from dngfx/master
Replace bit.ly with is.gd
Diffstat (limited to 'modules/isgd.py')
-rw-r--r--modules/isgd.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/modules/isgd.py b/modules/isgd.py
new file mode 100644
index 00000000..86af7447
--- /dev/null
+++ b/modules/isgd.py
@@ -0,0 +1,33 @@
+import re
+import Utils
+
+ISGD_API_URL = "https://is.gd/create.php"
+REGEX_URL = re.compile("https?://", re.I)
+
+class Module(object):
+ def __init__(self, bot, events, exports):
+ self.bot = bot
+ self.events = events
+ events.on("get.shortlink").hook(self.shortlink)
+ events.on("received.command.shorten").hook(self.shorten, min_args=1,
+ help="Shorten a URL using the is.gd service.", usage="<url>")
+
+ def shortlink(self, event):
+ url = event["url"]
+ if not re.match(REGEX_URL, url):
+ url = "http://%s" % url
+ data = Utils.get_url(ISGD_API_URL, get_params={
+ "format": "json",
+ "url": url
+ }, json=True)
+
+ if data and data["shorturl"]:
+ return data["shorturl"]
+
+ def shorten(self, event):
+ link = self.events.on("get.shortlink").call_for_result(
+ url=event["args"])
+ if link:
+ event["stdout"].write("Shortened URL: %s" % link)
+ else:
+ event["stderr"].write("Unable to shorten that URL.")