bitbot-3.11-fork/modules/isgd.py

41 lines
1.2 KiB
Python
Raw Normal View History

2016-03-29 15:20:50 +00:00
import re
from src import ModuleManager, utils
2016-03-29 15:20:50 +00:00
ISGD_API_URL = "https://is.gd/create.php"
REGEX_URL = re.compile("https?://\S+", re.I)
2016-03-29 15:20:50 +00:00
class Module(ModuleManager.BaseModule):
def on_load(self):
self.exports.add("shortlink", self._shortlink)
def _shortlink(self, url):
2016-03-29 15:20:50 +00:00
if not re.match(REGEX_URL, url):
url = "http://%s" % url
page = utils.http.request(ISGD_API_URL, get_params=
{"format": "json", "url": url}, json=True)
if page and page.data["shorturl"]:
return page.data["shorturl"]
2016-03-29 15:20:50 +00:00
@utils.hook("received.command.shorten")
2016-03-29 15:20:50 +00:00
def shorten(self, event):
"""
:help: Shorten a given URL using the is.gd service
:usage: <url>
"""
url = None
if len(event["args"]) > 0:
url = event["args_split"][0]
else:
url = event["target"].buffer.find(REGEX_URL)
if url:
url = re.search(REGEX_URL, url.message).group(0)
if not url:
raise utils.EventError("No URL provided/found.")
2019-04-24 14:37:14 +00:00
if url:
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.")