bitbot-3.11-fork/modules/isgd.py

35 lines
1,022 B
Python
Raw Normal View History

2016-03-29 15:20:50 +00:00
import re
from src import Utils
2016-03-29 15:20:50 +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):
def __init__(self, bot, events, exports):
self.events = events
2016-03-29 15:20:50 +00:00
@Utils.hook("get.shortlink")
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
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
@Utils.hook("received.command.shorten", min_args=1, usage="<url>")
2016-03-29 15:20:50 +00:00
def shorten(self, event):
"""
Shorten a given URL using the is.gd service
"""
link = self.events.on("get.shortlink").call_for_result(
url=event["args"])
2016-03-29 15:20:50 +00:00
if link:
event["stdout"].write("Shortened URL: %s" % link)
2016-03-29 15:20:50 +00:00
else:
event["stderr"].write("Unable to shorten that URL.")