aboutsummaryrefslogtreecommitdiff
path: root/modules/imgur.py
blob: 9c9e8c57acd7b9cfad034fae2df41d9bae9ba28e (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#--depends-on commands
#--depends-on config
#--require-config imgur-api-key

import re, datetime
from src import ModuleManager, utils, EventManager

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

GALLERY_FORMAT = "%s%s%sA gallery with %s image%s, %s views, posted %s (%s%s)%s"
IMAGE_FORMAT = "%s%s%sA %s image, %sx%s, with %s views, posted %s%s"

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

NSFW_TEXT = "(NSFW)"

@utils.export("channelset", utils.BoolSetting("auto-imgur",
    "Disable/Enable automatically getting info from Imgur URLs"))
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

    @utils.hook("command.regex")
    @utils.kwarg("ignore_action", False)
    @utils.kwarg("command", "imgur")
    @utils.kwarg("pattern", REGEX_IMAGE)
    def _regex_image(self, event):
        if event["target"].get_setting("auto-imgur", False):
            event["stdout"].write(self._parse_image(event["match"].group(1)))
            event.eat()

    @utils.hook("command.regex")
    @utils.kwarg("ignore_action", False)
    @utils.kwarg("command", "imgur")
    @utils.kwarg("pattern", REGEX_GALLERY)
    def _regex_gallery(self, event):
        if event["target"].get_setting("auto-imgur", False):
            event["stdout"].write(self._parse_gallery(event["match"].group(1)))
            event.eat()

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

        if result and result.data["success"]:
            data = result.data["data"]
            text = ""

            nsfw = utils.irc.bold(NSFW_TEXT) + " " if data["nsfw"] else ""
            title = data["title"] + " " if data["title"] else ""
            views = data["views"]
            time = datetime.datetime.utcfromtimestamp(data["datetime"]). \
                strftime("%e %b, %Y at %H:%M")
            ups = utils.irc.color(str(data["ups"]) + "▲", utils.consts.GREEN)
            downs = utils.irc.color("▼" + str(data["downs"]), utils.consts.RED)
            images = data["images_count"]
            image_plural = "" if images == 1 else "s"

            bracket_left = "(" if title or nsfw else ""
            bracket_right = ")" if title or nsfw else ""

            return GALLERY_FORMAT % (nsfw, title, bracket_left, images,
                image_plural, views, time, ups, downs, bracket_right)


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

        if result and result.data["success"]:
            data = result.data["data"]
            text = ""

            nsfw = utils.irc.bold(NSFW_TEXT) + " " if data["nsfw"] else ""
            title = data["title"] + " " if data["title"] else ""
            type = data["type"].split("/")[-1]
            width = data["width"]
            height = data["height"]
            views = data["views"]
            time = datetime.datetime.utcfromtimestamp(data["datetime"]).\
                strftime("%e %b, %Y at %H:%M")

           #%Y-%m-%d %H:%M:%S+00:00 (UTC)

            bracket_left = "(" if title or nsfw else ""
            bracket_right = ")" if title or nsfw else ""

        return IMAGE_FORMAT % (nsfw, title, bracket_left, type, width, height,
            views, time, bracket_right)

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

        if result and result.data["success"]:
            data = result.data["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:
            return None

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

        if result and result.data["success"]:
            data = result.data["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:
            return None

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

        result = None
        if image_match:
            result = self._image_info(image_match.group(1))
        else:
            gallery_match = REGEX_GALLERY.match(event["args_split"][0])
            if gallery_match:
                result = self._gallery_info(gallery_match.group(1))

        if result:
            event["stdout"].write(result)
        else:
            raise utils.EventsResultsError()