From 266cbbfe1ec53e82e27ba206e1413cc1b0ddb5e5 Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 24 Feb 2019 20:34:10 +0000 Subject: [PATCH 1/6] Update imgur.py Add auto-imgur for auto linking of imgur and make the linking a bit fancier. --- modules/imgur.py | 104 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/modules/imgur.py b/modules/imgur.py index bcfce572..2e198c8f 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -1,7 +1,8 @@ #--require-config imgur-api-key -import re -from src import ModuleManager, utils +import re, json +from src import ModuleManager, utils, EventManager, Logging +from datetime import datetime REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)") REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)") @@ -9,6 +10,9 @@ 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" +@utils.export("channelset", {"setting": "auto-imgur", + "help": "Disable/Enable automatically getting info from Imgur URLs", + "validate": utils.bool_or_none}) class Module(ModuleManager.BaseModule): def _prefix(self, data): text = "%s: " % data["id"] @@ -18,6 +22,97 @@ class Module(ModuleManager.BaseModule): text += "%s " % data["account_url"] return text + @utils.hook("received.message.channel", + priority=EventManager.PRIORITY_LOW) + def channel_message(self, event): + if not event["channel"].get_setting("auto-imgur", False): + return + + self._imgur(event) + + def _imgur(self, event): + msg = event["message"] + reply = "" + match_gallery = REGEX_GALLERY.match(msg) + match_image = REGEX_IMAGE.match(msg) + + if match_gallery: + reply = self._parse_gallery(match_gallery.group(1)) + + if match_image and not reply: + reply = self._parse_image(match_image.group(1)) + + if not reply: + return + + event.eat() + + self.events.on("send.stdout").call(target=event[ + "channel"], module_name="Imgur", server=event["server"], + message=reply) + return + + 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] ") if data["nsfw"] else "" + title = utils.irc.bold(data["title"]) + " " if data["title"] else "" + views = data["views"] + time = 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 is 1 else "s" + + bracket_left = "(" if title or nsfw else "" + bracket_right = ")" if title or nsfw else "" + + return "%s%s%sA gallery with %s image%s, %s views, posted %s (%s%s)%s" % \ + (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"]: + # Logging.Log.debug("%s", result.data["data"]) + + data = result.data["data"] + text = "" + + nsfw = utils.irc.bold("[NSFW] ") if data["nsfw"] else "" + title = utils.irc.bold(data["title"]) + " " if data["title"] else "" + type = data["type"].split("/")[-1] + width = data["width"] + height = data["height"] + views = data["views"] + time = 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 "%s%s%sA %s image, %sx%s, with %s views, posted %s%s" % \ + (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, @@ -59,10 +154,13 @@ class Module(ModuleManager.BaseModule): :help: Get information about a given imgur image URL :usage: """ - match = REGEX_GALLERY.match(event["args_split"][0]) + msg = event["args_split"][0] + + match = REGEX_GALLERY.match(msg) 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))) From 0444f9c0b91612d68681519c16fa20a30e0510fa Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 24 Feb 2019 20:37:17 +0000 Subject: [PATCH 2/6] Update imgur.py Remove debug logging cruft --- modules/imgur.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/imgur.py b/modules/imgur.py index 2e198c8f..31867cda 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -1,7 +1,7 @@ #--require-config imgur-api-key import re, json -from src import ModuleManager, utils, EventManager, Logging +from src import ModuleManager, utils, EventManager from datetime import datetime REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)") @@ -88,8 +88,6 @@ class Module(ModuleManager.BaseModule): json=True) if result and result.data["success"]: - # Logging.Log.debug("%s", result.data["data"]) - data = result.data["data"] text = "" From 4daba2b1ac3f8cce6cf8db6b5b252bbbb7664166 Mon Sep 17 00:00:00 2001 From: Dan <294904+dngfx@users.noreply.github.com> Date: Sun, 24 Feb 2019 21:10:25 +0000 Subject: [PATCH 3/6] Remove bold title, set NSFW_TEXT variable, fix bold spacing, fix PEP8 line breaks between definitions. Remove _imgur def and hook straight into code. --- modules/imgur.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/modules/imgur.py b/modules/imgur.py index 31867cda..a9e3bd62 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -10,6 +10,8 @@ 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" +NSFW_TEXT = "(NSFW)" + @utils.export("channelset", {"setting": "auto-imgur", "help": "Disable/Enable automatically getting info from Imgur URLs", "validate": utils.bool_or_none}) @@ -28,9 +30,6 @@ class Module(ModuleManager.BaseModule): if not event["channel"].get_setting("auto-imgur", False): return - self._imgur(event) - - def _imgur(self, event): msg = event["message"] reply = "" match_gallery = REGEX_GALLERY.match(msg) @@ -47,8 +46,7 @@ class Module(ModuleManager.BaseModule): event.eat() - self.events.on("send.stdout").call(target=event[ - "channel"], module_name="Imgur", server=event["server"], + self.events.on("send.stdout").call(target=event["channel"], module_name="Imgur", server=event["server"], message=reply) return @@ -62,15 +60,15 @@ class Module(ModuleManager.BaseModule): data = result.data["data"] text = "" - nsfw = utils.irc.bold("[NSFW] ") if data["nsfw"] else "" - title = utils.irc.bold(data["title"]) + " " if data["title"] else "" + nsfw = utils.irc.bold(NSFW_TEXT) + " " if data["nsfw"] else "" + title = data["title"] + " " if data["title"] else "" views = data["views"] time = 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 is 1 else "s" + image_plural = "" if images == 1 else "s" bracket_left = "(" if title or nsfw else "" bracket_right = ")" if title or nsfw else "" @@ -91,8 +89,8 @@ class Module(ModuleManager.BaseModule): data = result.data["data"] text = "" - nsfw = utils.irc.bold("[NSFW] ") if data["nsfw"] else "" - title = utils.irc.bold(data["title"]) + " " if data["title"] else "" + 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"] @@ -109,8 +107,6 @@ class Module(ModuleManager.BaseModule): (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, From ccd924d9e73b458b0b1f32715e7542dfa4e82aa4 Mon Sep 17 00:00:00 2001 From: Dan <294904+dngfx@users.noreply.github.com> Date: Sun, 24 Feb 2019 21:13:02 +0000 Subject: [PATCH 4/6] Remove superfluous json import and change datetime to match code standards. --- modules/imgur.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/imgur.py b/modules/imgur.py index a9e3bd62..2aaf97bb 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -1,8 +1,7 @@ #--require-config imgur-api-key -import re, json +import re, datetime from src import ModuleManager, utils, EventManager -from datetime import datetime REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)") REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)") @@ -63,7 +62,7 @@ class Module(ModuleManager.BaseModule): nsfw = utils.irc.bold(NSFW_TEXT) + " " if data["nsfw"] else "" title = data["title"] + " " if data["title"] else "" views = data["views"] - time = datetime.utcfromtimestamp(data["datetime"]). \ + 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) @@ -95,7 +94,7 @@ class Module(ModuleManager.BaseModule): width = data["width"] height = data["height"] views = data["views"] - time = datetime.utcfromtimestamp(data["datetime"]).\ + time = datetime.datetime.utcfromtimestamp(data["datetime"]).\ strftime("%e %b, %Y at %H:%M") #%Y-%m-%d %H:%M:%S+00:00 (UTC) From 6771c3fec19ae32367f7d6a6c71bb537fdacd70a Mon Sep 17 00:00:00 2001 From: Dan <294904+dngfx@users.noreply.github.com> Date: Sun, 24 Feb 2019 21:14:08 +0000 Subject: [PATCH 5/6] Switch to elif --- modules/imgur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imgur.py b/modules/imgur.py index 2aaf97bb..8967cce1 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -37,7 +37,7 @@ class Module(ModuleManager.BaseModule): if match_gallery: reply = self._parse_gallery(match_gallery.group(1)) - if match_image and not reply: + elif match_image: reply = self._parse_image(match_image.group(1)) if not reply: From 647136065608c600783f0a4d5a7ff586495dab2d Mon Sep 17 00:00:00 2001 From: dngfx Date: Mon, 25 Feb 2019 09:01:52 +0000 Subject: [PATCH 6/6] Update imgur.py --- modules/imgur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imgur.py b/modules/imgur.py index 8967cce1..6dac8890 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -40,7 +40,7 @@ class Module(ModuleManager.BaseModule): elif match_image: reply = self._parse_image(match_image.group(1)) - if not reply: + else: return event.eat()