diff options
| author | 2018-10-31 16:12:28 +0000 | |
|---|---|---|
| committer | 2018-10-31 16:12:28 +0000 | |
| commit | f073613848fb4112df64ac94d3c90dd3edb12f20 (patch) | |
| tree | 1a0d1d689fd2e4b1a420d5ca00a318a237c0e0ed | |
| parent | Fix some non-explicit None returns, add type hints to important variables (diff) | |
| signature | ||
Add first version of modules/imgur.py
| -rw-r--r-- | bot.conf.example | 2 | ||||
| -rw-r--r-- | modules/imgur.py | 37 |
2 files changed, 39 insertions, 0 deletions
diff --git a/bot.conf.example b/bot.conf.example index 5914d822..8531fe84 100644 --- a/bot.conf.example +++ b/bot.conf.example @@ -40,3 +40,5 @@ pushbullet-channel-tag = omdbapi-api-key = # https://www.virustotal.com/en/documentation/public-api/ virustotal-api-key = +# https://apidocs.imgur.com/ +imgur-api-key = diff --git a/modules/imgur.py b/modules/imgur.py new file mode 100644 index 00000000..d18029a6 --- /dev/null +++ b/modules/imgur.py @@ -0,0 +1,37 @@ +#--require-config imgur-api-key + +from src import ModuleManager, utils + +REGEX_IMAGE = "https?://(?:i\.)?imgur.com/(\w{7})" +URL_IMAGE = "https://api.imgur.com/3/image/%s" + +class Module(ModuleManager.BaseModule): + 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 = "%s: " % data["id"] + if data["nsfw"]: + text += "[NSFW] " + + 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() + + @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_IMAGE.match(event["args_split"][0]) + if match: + event["stdout"].write(self._image_info(match.group(1))) |
