From 75927c639bccf85da9ecec398c272ce3a6648788 Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 12 Jul 2020 00:35:16 +0100 Subject: [PATCH] Update imgur.py Unbreaks imgur.py re: Issue #246 --- modules/imgur.py | 54 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/modules/imgur.py b/modules/imgur.py index 35bb6329..af9392ed 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -14,11 +14,17 @@ URL_IMAGE = "https://api.imgur.com/3/image/%s" URL_GALLERY = "https://api.imgur.com/3/gallery/%s" +ARROW_UP = "↑" +ARROW_DOWN = "↓" + NSFW_TEXT = "(NSFW)" -@utils.export("channelset", utils.BoolSetting("auto-imgur", - "Disable/Enable automatically getting info from Imgur URLs")) + +@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"]: @@ -48,7 +54,9 @@ def _regex_gallery(self, event): 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() + headers={ + "Authorization": "Client-ID %s" % api_key + }).json() if result and result["success"]: data = result["data"] @@ -59,22 +67,31 @@ def _parse_gallery(self, hash): 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) + ups = utils.irc.color(str(data["ups"]) + ARROW_UP, utils.consts.GREEN) + downs = utils.irc.color(ARROW_DOWN + 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) - + 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() + headers={ + "Authorization": "Client-ID %s" % api_key + }).json() if result and result["success"]: data = result["data"] @@ -89,25 +106,25 @@ def _parse_image(self, hash): time = datetime.datetime.utcfromtimestamp(data["datetime"]).\ strftime("%e %b, %Y at %H:%M") - #%Y-%m-%d %H:%M:%S+00:00 (UTC) + #%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) + 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() + headers={ + "Authorization": "Client-ID %s" % api_key + }).json() 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"]) + text += "(%s %dx%d, %d views)" % (data["type"], data["width"], data["height"], data["views"]) if data["title"]: text += " %s" % data["title"] return text @@ -117,13 +134,14 @@ def _image_info(self, hash): 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() + headers={ + "Authorization": "Client-ID %s" % api_key + }).json() if result and result["success"]: data = result["data"] text = self._prefix(data) - text += "(%d views, %d▲▼%d)" % (data["views"], - data["ups"], data["downs"]) + text += "(%d views, %d??%d)" % (data["views"], data["ups"], data["downs"]) if data["title"]: text += " %s" % data["title"] return text