aboutsummaryrefslogtreecommitdiff
path: root/modules/imgur.py
blob: 1b3bfadf19605fbd7061fcb257742b24b4e3bcd1 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#--require-config imgur-api-key

import re
from src import ModuleManager, utils

REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)")
REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)")

URL_IMAGE = "https://api.imgur.com/3/image/%s"
URL_GALLERY = "https://api.imgur.com/3/gallery/%s"

class Module(ModuleManager.BaseModule):
    def _prefix(self, data):
        text = "%s: " % data["id"]
        if data["nsfw"]:
            text += "[NSFW] "
        if data["account_url"]:
            text += "%s " % data["account_url"]
        return text

    def _image_info(self, hash):
        api_key = self.bot.config["imgur-api-key"]
        result = utils.http.get_url(URL_IMAGE % hash,
            headers={"Authorization": "Client-ID %s" % api_key},
            json=True)

        if result and result["success"]:
            data = result["data"]
            text = self._prefix(data)

            text += "(%s %dx%d, %d views)" % (data["type"], data["width"],
                data["height"], data["views"])
            if data["title"]:
                text += " %s" % data["title"]
            return text
        else:
            raise utils.EventsResultsError()

    def _gallery_info(self, hash):
        api_key = self.bot.config["imgur-api-key"]
        result = utils.http.get_url(URL_GALLERY % hash,
            headers={"Authorization": "Client-ID %s" % api_key},
            json=True)

        if result and result["success"]:
            data = result["data"]
            text = self._prefix(data)
            text += "(%d views, %d▲▼%d)" % (data["views"],
                data["ups"], data["downs"])
            if data["title"]:
                text += " %s" % data["title"]
            return text
        else:
            raise utils.EventsResultsError()

    @utils.hook("received.command.imgur", min_args=1)
    def imgur(self, event):
        """
        :help: Get information about a given imgur image URL
        :usage: <url>
        """
        match = REGEX_GALLERY.match(event["args_split"][0])
        if match:
            event["stdout"].write(self._gallery_info(match.group(1)))
            return
        match = REGEX_IMAGE.match(event["args_split"][0])
        if match:
            event["stdout"].write(self._image_info(match.group(1)))
            return