2016-03-29 15:20:50 +00:00
|
|
|
import re
|
2018-09-24 14:13:27 +00:00
|
|
|
from src import Utils
|
2016-03-29 15:20:50 +00:00
|
|
|
|
2018-09-09 20:47:28 +00:00
|
|
|
ISGD_API_URL = "https://is.gd/create.php"
|
2016-03-29 15:20:50 +00:00
|
|
|
REGEX_URL = re.compile("https?://", re.I)
|
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-03-29 15:20:50 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2018-09-09 15:07:39 +00:00
|
|
|
events.on("get.shortlink").hook(self.shortlink)
|
|
|
|
events.on("received.command.shorten").hook(self.shorten, min_args=1,
|
2018-09-09 20:47:28 +00:00
|
|
|
help="Shorten a URL using the is.gd service.", usage="<url>")
|
2016-03-29 15:20:50 +00:00
|
|
|
|
|
|
|
def shortlink(self, event):
|
2018-09-09 12:20:59 +00:00
|
|
|
url = event["url"]
|
2016-03-29 15:20:50 +00:00
|
|
|
if not re.match(REGEX_URL, url):
|
|
|
|
url = "http://%s" % url
|
2018-09-09 20:47:28 +00:00
|
|
|
data = Utils.get_url(ISGD_API_URL, get_params={
|
|
|
|
"format": "json",
|
|
|
|
"url": url
|
|
|
|
}, json=True)
|
|
|
|
|
|
|
|
if data and data["shorturl"]:
|
|
|
|
return data["shorturl"]
|
2016-03-29 15:20:50 +00:00
|
|
|
|
|
|
|
def shorten(self, event):
|
2018-09-09 15:07:39 +00:00
|
|
|
link = self.events.on("get.shortlink").call_for_result(
|
|
|
|
url=event["args"])
|
2016-03-29 15:20:50 +00:00
|
|
|
if link:
|
2018-09-09 20:47:28 +00:00
|
|
|
event["stdout"].write("Shortened URL: %s" % link)
|
2016-03-29 15:20:50 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Unable to shorten that URL.")
|