2016-03-29 15:20:50 +00:00
|
|
|
import re
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, 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
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-02-07 10:07:11 +00:00
|
|
|
def on_load(self):
|
|
|
|
self.exports.add("shortlink", self._shortlink)
|
|
|
|
|
|
|
|
def _shortlink(self, url):
|
2019-04-24 14:46:54 +00:00
|
|
|
if not re.match(utils.http.REGEX_URL, url):
|
2016-03-29 15:20:50 +00:00
|
|
|
url = "http://%s" % url
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(ISGD_API_URL, get_params=
|
2018-10-03 12:22:37 +00:00
|
|
|
{"format": "json", "url": url}, json=True)
|
2018-09-09 20:47:28 +00:00
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
if page and page.data["shorturl"]:
|
|
|
|
return page.data["shorturl"]
|
2016-03-29 15:20:50 +00:00
|
|
|
|
2019-04-24 14:36:26 +00:00
|
|
|
@utils.hook("received.command.shorten")
|
2016-03-29 15:20:50 +00:00
|
|
|
def shorten(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Shorten a given URL using the is.gd service
|
|
|
|
:usage: <url>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-04-24 14:36:26 +00:00
|
|
|
url = None
|
|
|
|
if len(event["args"]) > 0:
|
|
|
|
url = event["args_split"][0]
|
|
|
|
else:
|
2019-04-24 14:46:54 +00:00
|
|
|
url = event["target"].buffer.find(utils.http.REGEX_URL)
|
2019-04-24 14:36:26 +00:00
|
|
|
if url:
|
2019-04-24 14:46:54 +00:00
|
|
|
url = re.search(utils.http.REGEX_URL, url.message).group(0)
|
2019-04-24 14:36:26 +00:00
|
|
|
if not url:
|
|
|
|
raise utils.EventError("No URL provided/found.")
|
|
|
|
|
2019-04-24 14:37:14 +00:00
|
|
|
if url:
|
2019-04-24 14:36:26 +00:00
|
|
|
event["stdout"].write("Shortened URL: %s" % self._shortlink(url))
|
2016-03-29 15:20:50 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Unable to shorten that URL.")
|