aboutsummaryrefslogtreecommitdiff
path: root/modules/isgd.py
blob: 3ceacaff591424724895adece2e096d3eb12dc71 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import re
from src 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.")