aboutsummaryrefslogtreecommitdiff
path: root/modules/bitly.py
blob: f31ca3476705841c2f927d8e0fe1d7cb4656e71d (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
#--require-config bitly-api-key

import re
import Utils

URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten"
REGEX_URL = re.compile("https?://", re.I)

class Module(object):
    def __init__(self, bot):
        self.bot = bot
        bot.events.on("get").on("shortlink").hook(self.shortlink)
        bot.events.on("received").on("command").on("shorten"
            ).hook(self.shorten, min_args=1, help="Shorten a URL.",
            usage="<url>")

    def shortlink(self, event):
        url = event["url"]
        if not re.match(REGEX_URL, url):
            url = "http://%s" % url
        data = Utils.get_url(URL_BITLYSHORTEN, get_params={
            "access_token": self.bot.config["bitly-api-key"],
            "longUrl": url}, json=True)
        if data and data["data"]:
            return data["data"]["url"]

    def shorten(self, event):
        link = self.bot.events.on("get").on("shortlink"
            ).call_for_result(url=event["args"])
        if link:
            event["stdout"].write("Short URL: %s" % link)
        else:
            event["stderr"].write("Unable to shorten that URL.")